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

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 string[] */
  23.     private $firewallNames;
  24.     /**
  25.      * @param string[] $firewallNames
  26.      */
  27.     public function __construct(
  28.         private RouterInterface $router,
  29.         private LocaleProviderInterface $channelBasedLocaleProvider,
  30.         private FirewallMap $firewallMap,
  31.         array $firewallNames,
  32.     ) {
  33.         Assert::notEmpty($firewallNames);
  34.         Assert::allString($firewallNames);
  35.         $this->firewallNames $firewallNames;
  36.     }
  37.     /**
  38.      * @throws NotFoundHttpException
  39.      */
  40.     public function restrictRequestLocale(RequestEvent $event): void
  41.     {
  42.         if (\method_exists($event'isMainRequest')) {
  43.             $isMainRequest $event->isMainRequest();
  44.         } else {
  45.             $isMainRequest $event->isMasterRequest();
  46.         }
  47.         if (!$isMainRequest) {
  48.             return;
  49.         }
  50.         $request $event->getRequest();
  51.         /** @psalm-suppress RedundantConditionGivenDocblockType Symfony docblock is not always true */
  52.         if ($request->attributes && in_array($request->attributes->get('_route'), ['_wdt''_profiler''_profiler_search''_profiler_search_results'])) {
  53.             return;
  54.         }
  55.         $currentFirewall $this->firewallMap->getFirewallConfig($request);
  56.         if (!$this->isFirewallSupported($currentFirewall)) {
  57.             return;
  58.         }
  59.         $requestLocale $request->getLocale();
  60.         if (!in_array($requestLocale$this->channelBasedLocaleProvider->getAvailableLocalesCodes(), true)) {
  61.             $event->setResponse(
  62.                 new RedirectResponse(
  63.                     $this->router->generate(
  64.                         'sylius_shop_homepage',
  65.                         ['_locale' => $this->channelBasedLocaleProvider->getDefaultLocaleCode()],
  66.                     ),
  67.                 ),
  68.             );
  69.         }
  70.     }
  71.     private function isFirewallSupported(?FirewallConfig $firewall null): bool
  72.     {
  73.         return
  74.             null !== $firewall &&
  75.             in_array($firewall->getName(), $this->firewallNames)
  76.         ;
  77.     }
  78. }