vendor/sonata-project/user-bundle/src/Listener/LastLoginListener.php line 41

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\UserBundle\Listener;
  12. use Sonata\UserBundle\Model\UserInterface;
  13. use Sonata\UserBundle\Model\UserManagerInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  16. use Symfony\Component\Security\Http\SecurityEvents;
  17. /**
  18.  * @internal
  19.  */
  20. final class LastLoginListener implements EventSubscriberInterface
  21. {
  22.     private UserManagerInterface $userManager;
  23.     public function __construct(UserManagerInterface $userManager)
  24.     {
  25.         $this->userManager $userManager;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
  31.         ];
  32.     }
  33.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event): void
  34.     {
  35.         $user $event->getAuthenticationToken()->getUser();
  36.         if (!$user instanceof UserInterface) {
  37.             return;
  38.         }
  39.         $user->setLastLogin(new \DateTime());
  40.         $this->userManager->save($user);
  41.     }
  42. }