<?php
namespace App\EventSubscriber;
use App\Entity\Main\Administrator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;
/**
* Stores the locale of the user in the session after the
* login. This can be used by the LocaleSubscriber afterwards.
*
* Class UserLocaleSubscriber
* @package App\EventSubscriber
*/
class UserSessionLoginSubscriber implements EventSubscriberInterface
{
/**
* @var SessionInterface
*/
private $session;
/**
* UserLocaleSubscriber constructor.
* @param SessionInterface $session
*/
public function __construct(SessionInterface $session)
{
$this->session = $session;
}
/**
* @param InteractiveLoginEvent $event
*/
public function onInteractiveLogin(InteractiveLoginEvent $event)
{
/* @var $administrator Administrator */
$administrator = $event->getAuthenticationToken()->getUser();
$this->session->set('_locale', $administrator->getLocale());
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
];
}
}