src/Application/EventSubscriber/LastLoginSubscriber.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Application\EventSubscriber;
  3. use Athome\UserBundle\Model\UserInterface;
  4. use Athome\UserBundle\Model\UserManagerInterface;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  8. use Symfony\Component\Security\Http\SecurityEvents;
  9. class LastLoginSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @param EntityManagerInterface $entityManager
  13.      */
  14.     public function __construct(private EntityManagerInterface $entityManager)
  15.     {
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
  21.         ];
  22.     }
  23.     /**
  24.      * @param InteractiveLoginEvent $event
  25.      * @throws \Exception
  26.      */
  27.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event): void
  28.     {
  29.         $user $event->getAuthenticationToken()->getUser();
  30.         if ($user instanceof UserInterface) {
  31.             $user->setLastLogin(new \DateTime());
  32.             $this->entityManager->persist($user);
  33.             $this->entityManager->flush();
  34.         }
  35.     }
  36. }