vendor/appaydin/pd-user/Listener/LoginListener.php line 34

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the pd-admin pd-user package.
  4.  *
  5.  * @package     pd-user
  6.  * @license     LICENSE
  7.  * @author      Ramazan APAYDIN <apaydin541@gmail.com>
  8.  * @link        https://github.com/appaydin/pd-user
  9.  */
  10. namespace Pd\UserBundle\Listener;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Pd\UserBundle\Model\UserInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  15. use Symfony\Component\Security\Http\SecurityEvents;
  16. /**
  17.  * Listener set to user defined Language.
  18.  *
  19.  * @author Ramazan APAYDIN <apaydin541@gmail.com>
  20.  */
  21. class LoginListener implements EventSubscriberInterface
  22. {
  23.     public function __construct(private EntityManagerInterface $entityManager)
  24.     {
  25.     }
  26.     /**
  27.      * On Login Event.
  28.      */
  29.     public function onLogin(InteractiveLoginEvent $event): void
  30.     {
  31.         // Get User
  32.         $user $event->getAuthenticationToken()->getUser();
  33.         if ($user instanceof UserInterface) {
  34.             // Change Site Language to User
  35.             if ($user->getLanguage()) {
  36.                 $event->getRequest()->getSession()->set('_locale'$user->getLanguage());
  37.             }
  38.             // Set Last Login
  39.             $user->setLastLogin(new \DateTime());
  40.             // Save
  41.             $this->entityManager->persist($user);
  42.             $this->entityManager->flush();
  43.         }
  44.     }
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             SecurityEvents::INTERACTIVE_LOGIN => 'onLogin',
  49.         ];
  50.     }
  51. }