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

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