src/EventSubscriber/UserSessionLoginSubscriber.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Main\Administrator;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  6. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  7. use Symfony\Component\Security\Http\SecurityEvents;
  8. /**
  9.  * Stores the locale of the user in the session after the
  10.  * login. This can be used by the LocaleSubscriber afterwards.
  11.  *
  12.  * Class UserLocaleSubscriber
  13.  * @package App\EventSubscriber
  14.  */
  15. class UserSessionLoginSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var SessionInterface
  19.      */
  20.     private $session;
  21.     /**
  22.      * UserLocaleSubscriber constructor.
  23.      * @param SessionInterface $session
  24.      */
  25.     public function __construct(SessionInterface $session)
  26.     {
  27.         $this->session $session;
  28.     }
  29.     /**
  30.      * @param InteractiveLoginEvent $event
  31.      */
  32.     public function onInteractiveLogin(InteractiveLoginEvent $event)
  33.     {
  34.         /* @var $administrator Administrator */
  35.         $administrator $event->getAuthenticationToken()->getUser();
  36.         $this->session->set('_locale'$administrator->getLocale());
  37.     }
  38.     /**
  39.      * @return array
  40.      */
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return [
  44.             SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
  45.         ];
  46.     }
  47. }