src/EventSubscriber/Api/UserLocaleSubscriber.php line 81

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Api;
  3. use App\Entity\Api\CoreModule\User;
  4. use App\Helper\Api\JsonFormatter\Throwable\Formatter;
  5. use App\Helper\Api\Translator\ApiTranslator;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  8. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  9. use Symfony\Component\Security\Http\SecurityEvents;
  10. /**
  11.  * Subscriber to store locale from user options in session after user logged in.
  12.  *
  13.  * @package API
  14.  * @api
  15.  */
  16. class UserLocaleSubscriber extends AbstractSubscriber
  17. {
  18.     /**
  19.      * Stack that controls the lifecycle of requests
  20.      *
  21.      * @var RequestStack
  22.      */
  23.     protected RequestStack $requestStack;
  24.     /**
  25.      * Returns an array of event names this subscriber wants to listen to.
  26.      *
  27.      * The array keys are event names and the value can be:
  28.      *
  29.      *  * The method name to call (priority defaults to 0)
  30.      *  * An array composed of the method name to call and the priority
  31.      *  * An array of arrays composed of the method names to call and respective
  32.      *    priorities, or 0 if unset
  33.      *
  34.      * For instance:
  35.      *
  36.      *  * ['eventName' => 'methodName']
  37.      *  * ['eventName' => ['methodName', $priority]]
  38.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  39.      *
  40.      * The code must not depend on runtime state as it will only be called at compile time.
  41.      * All logic depending on runtime state must be put into the individual methods handling the events.
  42.      *
  43.      * @noinspection PhpArrayShapeAttributeCanBeAddedInspection
  44.      * @noinspection PhpUnused
  45.      */
  46.     public static function getSubscribedEvents(): array
  47.     {
  48.         return [
  49.             SecurityEvents::INTERACTIVE_LOGIN => [
  50.                 ['onInteractiveLogin'0]
  51.             ]
  52.         ];
  53.     }
  54.     /**
  55.      * Constructor.
  56.      *
  57.      * @param Formatter $formatter
  58.      * @param ApiTranslator $translator
  59.      * @param RequestStack $requestStack
  60.      * @noinspection PhpPureAttributeCanBeAddedInspection
  61.      */
  62.     public function __construct(Formatter $formatterApiTranslator $translatorRequestStack $requestStack)
  63.     {
  64.         parent::__construct($formatter$translator);
  65.         $this->requestStack $requestStack;
  66.     }
  67.     /**
  68.      * Store locale from user options in session after user logged in.
  69.      *
  70.      * @param InteractiveLoginEvent $event
  71.      * @return void
  72.      * @noinspection PhpUnused
  73.      */
  74.     public function onInteractiveLogin(InteractiveLoginEvent $event): void
  75.     {
  76.         $token $event->getAuthenticationToken();
  77.         /* @var UsernamePasswordToken $token */
  78.         // only handle for API routes and main firewall
  79.         if (preg_match('/^\/api\//'$event->getRequest()->getRequestUri()) &&
  80.             $token->getFirewallName() === 'main') {
  81.             $user $token->getUser();
  82.             /* @var User $user */
  83.             $this->requestStack->getSession()->set('_locale'$user->getLocale());
  84.         }
  85.     }
  86. }