src/EventSubscriber/LdapPasswordSubscriber.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Doctrine\App\History;
  4. use App\Entity\Doctrine\App\User;
  5. use App\Event\LdapPasswordUpdateEvent;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  9. /**
  10.  * Class LdapPasswordSubscriber
  11.  * @package App\EventSubscriber
  12.  */
  13. class LdapPasswordSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var EntityManagerInterface
  17.      */
  18.     private $em;
  19.     /**
  20.      * @var TokenStorageInterface
  21.      */
  22.     private $tokenStorage;
  23.     /**
  24.      * ApiLdapPasswordSubscriber constructor.
  25.      * @param EntityManagerInterface $em
  26.      * @param TokenStorageInterface $tokenStorage
  27.      */
  28.     public function __construct(EntityManagerInterface $emTokenStorageInterface $tokenStorage)
  29.     {
  30.         $this->em $em;
  31.         $this->tokenStorage $tokenStorage;
  32.     }
  33.     /**
  34.      * @return array
  35.      */
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             LdapPasswordUpdateEvent::class => ['postUpdate']
  40.         ];
  41.     }
  42.     /**
  43.      * @param LdapPasswordUpdateEvent $event
  44.      */
  45.     public function postUpdate(LdapPasswordUpdateEvent $event)
  46.     {
  47.         $user $event->getUser();
  48.         if (!$user instanceof User) {
  49.             return;
  50.         }
  51.         $history = new History();
  52.         $history->setUser($this->tokenStorage->getToken()->getUser())
  53.             ->setCommon($user)
  54.             ->setDate(new \DateTime())
  55.             ->setAction(\App\Entity\History::ACTION_USER_LDAP_PASSWORD_UPDATED);
  56.         $this->em->persist($history);
  57.         $this->em->flush();
  58.     }
  59. }