src/EventSubscriber/EasyAdminSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. # src/EventSubscriber/EasyAdminSubscriber.php
  3. namespace App\EventSubscriber;
  4. use App\Entity\User;
  5. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  8. class EasyAdminSubscriber implements EventSubscriberInterface
  9. {
  10.     private $passwordHasher;
  11.     public function __construct(UserPasswordHasherInterface $passwordHasher)
  12.     {
  13.         $this->passwordHasher $passwordHasher;
  14.     }
  15.     public static function getSubscribedEvents()
  16.     {
  17.         return [
  18.             BeforeEntityPersistedEvent::class => ['rwHashPassword'],
  19.         ];
  20.     }
  21.     public function rwHashPassword(BeforeEntityPersistedEvent $event)
  22.     {
  23.         $entity $event->getEntityInstance();
  24.         if (!($entity instanceof User)) {
  25.             return;
  26.         }
  27.         $plainTextPassword $entity->getPlainTextPassword();
  28.         $password $this->passwordHasher->hashPassword($entity$plainTextPassword);
  29.         $entity->setPassword($password);
  30.     }
  31. }