src/EventSubscriber/LoginUserSubscriber.php line 28

  1. <?php
  2. namespace App\EventSubscriber;
  3. use DateTimeImmutable;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  7. use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
  8. class LoginUserSubscriber implements EventSubscriberInterface
  9. {
  10.     private $em;
  11.     private $environment;
  12.     public function __construct(EntityManagerInterface $emstring $environment)
  13.     {
  14.         $this->em $em;
  15.         $this->environment $environment;
  16.     }
  17.     
  18.     public static function getSubscribedEvents():array
  19.     {
  20.         return [
  21.             LoginSuccessEvent::class => 'onLoginSuccess'
  22.         ];
  23.     }
  24.     public function onLoginSuccess(LoginSuccessEvent $event)
  25.     {
  26.         $user $event->getUser();
  27.         $user->setConnectedAt(new DateTimeImmutable());
  28.         $this->em->flush();
  29.         
  30.     }
  31. }