vendor/sylius/sylius/src/Sylius/Bundle/ShopBundle/EventListener/NonChannelLocaleListener.php line 60

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ShopBundle\EventListener;
  12. use Sylius\Component\Locale\Provider\LocaleProviderInterface;
  13. use Symfony\Bundle\SecurityBundle\Security\FirewallConfig;
  14. use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpKernel\Event\RequestEvent;
  17. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  18. use Symfony\Component\Routing\RouterInterface;
  19. use Webmozart\Assert\Assert;
  20. final class NonChannelLocaleListener
  21. {
  22.     /** @var RouterInterface */
  23.     private $router;
  24.     /** @var LocaleProviderInterface */
  25.     private $channelBasedLocaleProvider;
  26.     /** @var FirewallMap */
  27.     private $firewallMap;
  28.     /** @var string[] */
  29.     private $firewallNames;
  30.     /**
  31.      * @param string[] $firewallNames
  32.      */
  33.     public function __construct(
  34.         RouterInterface $router,
  35.         LocaleProviderInterface $channelBasedLocaleProvider,
  36.         FirewallMap $firewallMap,
  37.         array $firewallNames
  38.     ) {
  39.         Assert::notEmpty($firewallNames);
  40.         Assert::allString($firewallNames);
  41.         $this->channelBasedLocaleProvider $channelBasedLocaleProvider;
  42.         $this->firewallMap $firewallMap;
  43.         $this->firewallNames $firewallNames;
  44.         $this->router $router;
  45.     }
  46.     /**
  47.      * @throws NotFoundHttpException
  48.      */
  49.     public function restrictRequestLocale(RequestEvent $event): void
  50.     {
  51.         if (!$event->isMasterRequest()) {
  52.             return;
  53.         }
  54.         $request $event->getRequest();
  55.         /** @psalm-suppress RedundantConditionGivenDocblockType Symfony docblock is not always true */
  56.         if ($request->attributes && in_array($request->attributes->get('_route'), ['_wdt''_profiler''_profiler_search''_profiler_search_results'])) {
  57.             return;
  58.         }
  59.         $currentFirewall $this->firewallMap->getFirewallConfig($request);
  60.         if (!$this->isFirewallSupported($currentFirewall)) {
  61.             return;
  62.         }
  63.         $requestLocale $request->getLocale();
  64.         if (!in_array($requestLocale$this->channelBasedLocaleProvider->getAvailableLocalesCodes(), true)) {
  65.             $event->setResponse(
  66.                 new RedirectResponse(
  67.                     $this->router->generate(
  68.                         'sylius_shop_homepage',
  69.                         ['_locale' => $this->channelBasedLocaleProvider->getDefaultLocaleCode()]
  70.                     )
  71.                 )
  72.             );
  73.         }
  74.     }
  75.     private function isFirewallSupported(?FirewallConfig $firewall null): bool
  76.     {
  77.         return
  78.             null !== $firewall &&
  79.             in_array($firewall->getName(), $this->firewallNames)
  80.         ;
  81.     }
  82. }