src/EventSubscriber/EasyAdminSubscriber.php line 31

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\Security\Core\Security;
  8. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  9. class EasyAdminSubscriber implements EventSubscriberInterface
  10. {
  11.  
  12.     private $security;
  13.     private $passwordEncoder;
  14.     public function __construct(Security $security,UserPasswordEncoderInterface $passwordEncoder)
  15. {
  16.         $this->security $security;
  17.         $this->passwordEncoder $passwordEncoder;
  18.     }
  19.     public static function getSubscribedEvents()
  20.     {
  21.         return [
  22.             BeforeEntityPersistedEvent::class => ['beforePersist'],
  23.         ];
  24.     }
  25.     public function beforePersist(BeforeEntityPersistedEvent $event)
  26.     {
  27.         $entity $event->getEntityInstance();
  28.         
  29.                 if ($entity instanceof User) {
  30.                     
  31.             $entity->setPassword(
  32.                    $this->passwordEncoder->encodePassword(
  33.                             $entity,
  34.                             $entity->getPassword()
  35.                 ));
  36.                 
  37.             
  38.             
  39.             
  40.                 
  41.         }
  42.     }
  43. }