vendor/sylius/sylius/src/Sylius/Component/Core/Locale/Context/StorageBasedLocaleContext.php line 44

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\Component\Core\Locale\Context;
  12. use Sylius\Component\Channel\Context\ChannelContextInterface;
  13. use Sylius\Component\Channel\Context\ChannelNotFoundException;
  14. use Sylius\Component\Core\Locale\LocaleStorageInterface;
  15. use Sylius\Component\Locale\Context\LocaleContextInterface;
  16. use Sylius\Component\Locale\Context\LocaleNotFoundException;
  17. use Sylius\Component\Locale\Provider\LocaleProviderInterface;
  18. final class StorageBasedLocaleContext implements LocaleContextInterface
  19. {
  20.     /** @var ChannelContextInterface */
  21.     private $channelContext;
  22.     /** @var LocaleStorageInterface */
  23.     private $localeStorage;
  24.     /** @var LocaleProviderInterface */
  25.     private $localeProvider;
  26.     public function __construct(
  27.         ChannelContextInterface $channelContext,
  28.         LocaleStorageInterface $localeStorage,
  29.         LocaleProviderInterface $localeProvider
  30.     ) {
  31.         $this->channelContext $channelContext;
  32.         $this->localeStorage $localeStorage;
  33.         $this->localeProvider $localeProvider;
  34.     }
  35.     public function getLocaleCode(): string
  36.     {
  37.         $availableLocalesCodes $this->localeProvider->getAvailableLocalesCodes();
  38.         try {
  39.             $localeCode $this->localeStorage->get($this->channelContext->getChannel());
  40.         } catch (ChannelNotFoundException $exception) {
  41.             throw new LocaleNotFoundException(null$exception);
  42.         }
  43.         if (!in_array($localeCode$availableLocalesCodestrue)) {
  44.             throw LocaleNotFoundException::notAvailable($localeCode$availableLocalesCodes);
  45.         }
  46.         return $localeCode;
  47.     }
  48. }