vendor/kunstmaan/admin-bundle/EventSubscriber/LoginEventSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. namespace Kunstmaan\AdminBundle\EventSubscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Kunstmaan\AdminBundle\Entity\UserInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  7. use Symfony\Component\Security\Http\SecurityEvents;
  8. final class LoginEventSubscriber implements EventSubscriberInterface
  9. {
  10.     /** @var EntityManagerInterface */
  11.     private $em;
  12.     public function __construct(EntityManagerInterface $em)
  13.     {
  14.         $this->em $em;
  15.     }
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             SecurityEvents::INTERACTIVE_LOGIN => 'setLastLoginDate',
  20.         ];
  21.     }
  22.     public function setLastLoginDate(InteractiveLoginEvent $event)
  23.     {
  24.         $user $event->getAuthenticationToken()->getUser();
  25.         if (!$user instanceof UserInterface) {
  26.             return;
  27.         }
  28.         $user->setLastLogin(new \DateTime());
  29.         $this->em->flush();
  30.     }
  31. }