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

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