vendor/sylius/sylius/src/Sylius/Bundle/LocaleBundle/Context/RequestBasedLocaleContext.php line 35

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\LocaleBundle\Context;
  12. use Sylius\Component\Locale\Context\LocaleContextInterface;
  13. use Sylius\Component\Locale\Context\LocaleNotFoundException;
  14. use Sylius\Component\Locale\Provider\LocaleProviderInterface;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. final class RequestBasedLocaleContext implements LocaleContextInterface
  17. {
  18.     private RequestStack $requestStack;
  19.     private LocaleProviderInterface $localeProvider;
  20.     public function __construct(RequestStack $requestStackLocaleProviderInterface $localeProvider)
  21.     {
  22.         $this->requestStack $requestStack;
  23.         $this->localeProvider $localeProvider;
  24.     }
  25.     public function getLocaleCode(): string
  26.     {
  27.         $request $this->requestStack->getMasterRequest();
  28.         if (null === $request) {
  29.             throw new LocaleNotFoundException('No master request available.');
  30.         }
  31.         $localeCode $request->attributes->get('_locale');
  32.         if (null === $localeCode) {
  33.             throw new LocaleNotFoundException('No locale attribute is set on the master request.');
  34.         }
  35.         $availableLocalesCodes $this->localeProvider->getAvailableLocalesCodes();
  36.         if (!in_array($localeCode$availableLocalesCodestrue)) {
  37.             throw LocaleNotFoundException::notAvailable($localeCode$availableLocalesCodes);
  38.         }
  39.         return $localeCode;
  40.     }
  41. }