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

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.     /**
  33.      * @return array
  34.      */
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             FOSUserEvents::SECURITY_IMPLICIT_LOGIN => 'onImplicitLogin',
  39.             SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
  40.         ];
  41.     }
  42.     public function onImplicitLogin(UserEvent $event)
  43.     {
  44.         $user $event->getUser();
  45.         $user->setLastLogin(new \DateTime());
  46.         $this->userManager->updateUser($user);
  47.     }
  48.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
  49.     {
  50.         $user $event->getAuthenticationToken()->getUser();
  51.         if ($user instanceof UserInterface) {
  52.             $user->setLastLogin(new \DateTime());
  53.             $this->userManager->updateUser($user);
  54.         }
  55.     }
  56. }