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

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.  * @final
  21.  */
  22. class LastLoginListener implements EventSubscriberInterface
  23. {
  24.     protected $userManager;
  25.     /**
  26.      * LastLoginListener constructor.
  27.      */
  28.     public function __construct(UserManagerInterface $userManager)
  29.     {
  30.         $this->userManager $userManager;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             FOSUserEvents::SECURITY_IMPLICIT_LOGIN => 'onImplicitLogin',
  36.             SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
  37.         ];
  38.     }
  39.     public function onImplicitLogin(UserEvent $event)
  40.     {
  41.         $user $event->getUser();
  42.         $user->setLastLogin(new \DateTime());
  43.         $this->userManager->updateUser($user);
  44.     }
  45.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
  46.     {
  47.         $user $event->getAuthenticationToken()->getUser();
  48.         if ($user instanceof UserInterface) {
  49.             $user->setLastLogin(new \DateTime());
  50.             $this->userManager->updateUser($user);
  51.         }
  52.     }
  53. }