vendor/sylius/sylius/src/Sylius/Bundle/ShopBundle/Controller/LocaleSwitchController.php line 54

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\Controller;
  12. use Sylius\Bundle\ShopBundle\Locale\LocaleSwitcherInterface;
  13. use Sylius\Component\Locale\Context\LocaleContextInterface;
  14. use Sylius\Component\Locale\Provider\LocaleProviderInterface;
  15. use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Exception\HttpException;
  19. use Twig\Environment;
  20. final class LocaleSwitchController
  21. {
  22.     /** @var EngineInterface|Environment */
  23.     private $templatingEngine;
  24.     /** @var LocaleContextInterface */
  25.     private $localeContext;
  26.     /** @var LocaleProviderInterface */
  27.     private $localeProvider;
  28.     /** @var LocaleSwitcherInterface */
  29.     private $localeSwitcher;
  30.     /**
  31.      * @param EngineInterface|Environment $templatingEngine
  32.      */
  33.     public function __construct(
  34.         object $templatingEngine,
  35.         LocaleContextInterface $localeContext,
  36.         LocaleProviderInterface $localeProvider,
  37.         LocaleSwitcherInterface $localeSwitcher
  38.     ) {
  39.         $this->templatingEngine $templatingEngine;
  40.         $this->localeContext $localeContext;
  41.         $this->localeProvider $localeProvider;
  42.         $this->localeSwitcher $localeSwitcher;
  43.     }
  44.     public function renderAction(): Response
  45.     {
  46.         return new Response($this->templatingEngine->render('@SyliusShop/Menu/_localeSwitch.html.twig', [
  47.             'active' => $this->localeContext->getLocaleCode(),
  48.             'locales' => $this->localeProvider->getAvailableLocalesCodes(),
  49.         ]));
  50.     }
  51.     public function switchAction(Request $request, ?string $code null): Response
  52.     {
  53.         if (null === $code) {
  54.             $code $this->localeProvider->getDefaultLocaleCode();
  55.         }
  56.         if (!in_array($code$this->localeProvider->getAvailableLocalesCodes(), true)) {
  57.             throw new HttpException(
  58.                 Response::HTTP_NOT_ACCEPTABLE,
  59.                 sprintf('The locale code "%s" is invalid.'$code)
  60.             );
  61.         }
  62.         return $this->localeSwitcher->handle($request$code);
  63.     }
  64. }