<?php
namespace App\EventListener;
use App\Entity\OA2User;
use App\Utils\TranslatorHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Translation\TranslatorInterface;
class ControllerListener implements EventSubscriberInterface
{
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @var ContainerInterface
*/
private $container;
public function __construct(
TranslatorInterface $translator,
ContainerInterface $container
) {
$this->translator = $translator;
$this->container = $container;
}
public function onKernelController(FilterControllerEvent $event)
{
TranslatorHelper::init($this->translator);
if (!$this->container->has('security.token_storage')) {
return;
}
/** @var TokenStorageInterface $tokenStorage */
$tokenStorage = $this->container->get('security.token_storage');
$token = $tokenStorage->getToken();
if ($token === null) {
return;
}
/** @var OA2User $user */
$user = $token->getUser();
if (!$user instanceof OA2User) {
return;
}
if (
$user->isBankUser()
|| !$user->getClientUser()
|| !($person = $user->getClientUser()->getPerson())
|| !($personSettings = $person->getPersonSettings())
) {
return;
}
$locale = $personSettings->getLanguage();
$event->getRequest()->setLocale($locale);
$this->translator->setLocale($locale);
}
public static function getSubscribedEvents()
{
return [
KernelEvents::CONTROLLER => ['onKernelController', -100],
];
}
}