src/EventSubscriber/UserLocaleSubscriber.php line 37

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the CosaVostra, TrackPay package.
  4.  *
  5.  * (c) Mohamed Radhi GUENNICHI <rg@mate.tn> <+216 50 711 816>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\EventSubscriber;
  12. use App\Entity\User;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  16. use Symfony\Component\Security\Http\SecurityEvents;
  17. /**
  18.  * Stores the locale of the user in the session after the
  19.  * login.
  20.  */
  21. class UserLocaleSubscriber implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var SessionInterface
  25.      */
  26.     protected $session;
  27.     public function __construct(SessionInterface $session)
  28.     {
  29.         $this->session $session;
  30.     }
  31.     public function onInteractiveLogin(InteractiveLoginEvent $event): void
  32.     {
  33.         /** @var User $user */
  34.         $user $event->getAuthenticationToken()->getUser();
  35.         if (null !== $user->getLocale()) {
  36.             $this->session->set('_locale'$user->getLocale());
  37.         }
  38.     }
  39.     /**
  40.      * @inheritDoc
  41.      */
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return [
  45.             SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin'
  46.         ];
  47.     }
  48. }