vendor/friendsofsymfony/user-bundle/EventListener/LastLoginListener.php line 55

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSUserBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace FOS\UserBundle\EventListener;
  11. use FOS\UserBundle\Event\UserEvent;
  12. use FOS\UserBundle\FOSUserEvents;
  13. use FOS\UserBundle\Model\UserInterface;
  14. use FOS\UserBundle\Model\UserManagerInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  17. use Symfony\Component\Security\Http\SecurityEvents;
  18. /**
  19.  * @internal
  20.  *
  21.  * @final
  22.  */
  23. class LastLoginListener implements EventSubscriberInterface
  24. {
  25.     protected $userManager;
  26.     /**
  27.      * LastLoginListener constructor.
  28.      */
  29.     public function __construct(UserManagerInterface $userManager)
  30.     {
  31.         $this->userManager $userManager;
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             FOSUserEvents::SECURITY_IMPLICIT_LOGIN => 'onImplicitLogin',
  37.             SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
  38.         ];
  39.     }
  40.     public function onImplicitLogin(UserEvent $event)
  41.     {
  42.         $user $event->getUser();
  43.         $user->setLastLogin(new \DateTime());
  44.         $this->userManager->updateUser($user);
  45.     }
  46.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
  47.     {
  48.         $user $event->getAuthenticationToken()->getUser();
  49.         if ($user instanceof UserInterface) {
  50.             $user->setLastLogin(new \DateTime());
  51.             $this->userManager->updateUser($user);
  52.         }
  53.     }
  54. }