Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

src/EventSubscriber/UserAdminSubscriber.php line 30

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