src/EventListener/NonChannelLocaleListener.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Service\System\ShortUrlService;
  4. use Sylius\Component\Locale\Provider\LocaleProviderInterface;
  5. use Symfony\Bundle\SecurityBundle\Security\FirewallConfig;
  6. use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. use Webmozart\Assert\Assert;
  11. final class NonChannelLocaleListener
  12. {
  13.     /** @var LocaleProviderInterface */
  14.     private $channelBasedLocaleProvider;
  15.     /** @var FirewallMap */
  16.     private $firewallMap;
  17.     /** @var string[] */
  18.     private $firewallNames;
  19.     /** @var ShortUrlService */
  20.     private $shortUrlService;
  21.     /**
  22.      * @param string[] $firewallNames
  23.      */
  24.     public function __construct(
  25.         LocaleProviderInterface $channelBasedLocaleProvider,
  26.         FirewallMap $firewallMap,
  27.         array $firewallNames,
  28.         ShortUrlService $shortUrlService
  29.     ) {
  30.         Assert::notEmpty($firewallNames);
  31.         Assert::allString($firewallNames);
  32.         $this->channelBasedLocaleProvider $channelBasedLocaleProvider;
  33.         $this->firewallMap $firewallMap;
  34.         $this->firewallNames $firewallNames;
  35.         $this->shortUrlService $shortUrlService;
  36.     }
  37.     /**
  38.      * @throws NotFoundHttpException
  39.      */
  40.     public function restrictRequestLocale(GetResponseEvent $event): void
  41.     {
  42.         if (!$event->isMasterRequest()) {
  43.             return;
  44.         }
  45.         $request $event->getRequest();
  46.         if ($request->attributes && in_array($request->attributes->get('_route'), ['_wdt''_profiler''_profiler_search''_profiler_search_results'])) {
  47.             return;
  48.         }
  49.         $currentFirewall $this->firewallMap->getFirewallConfig($request);
  50.         if (!$this->isFirewallSupported($currentFirewall)) {
  51.             return;
  52.         }
  53.         $requestLocale 'ro';
  54.         $session $request->getSession();
  55.         if ($session->has('localeCode')) {
  56.             $requestLocale $session->get('localeCode');
  57.         }
  58.         if (!in_array($requestLocale$this->channelBasedLocaleProvider->getAvailableLocalesCodes(), true)) {
  59.             //try to find if the locale is not a short url cod
  60.             if (!$this->shortUrlService->checkIfRequestedParamIsShortUrlCode($requestLocale$event)) {
  61.                 $response = new RedirectResponse('/');
  62.                 $event->setResponse($response);
  63.             } else {
  64.                 $requestLocale 'ro';
  65.                 $session->set('localeCode'$requestLocale);
  66.             }
  67.         }
  68.     }
  69.     private function isFirewallSupported(?FirewallConfig $firewall null): bool
  70.     {
  71.         return
  72.             null !== $firewall &&
  73.             in_array($firewall->getName(), $this->firewallNames)
  74.             ;
  75.     }
  76. }