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

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.     public function __construct(
  21.         private ChannelContextInterface $channelContext,
  22.         private LocaleStorageInterface $localeStorage,
  23.         private LocaleProviderInterface $localeProvider,
  24.     ) {
  25.     }
  26.     public function getLocaleCode(): string
  27.     {
  28.         $availableLocalesCodes $this->localeProvider->getAvailableLocalesCodes();
  29.         try {
  30.             $localeCode $this->localeStorage->get($this->channelContext->getChannel());
  31.         } catch (ChannelNotFoundException $exception) {
  32.             throw new LocaleNotFoundException(null$exception);
  33.         }
  34.         if (!in_array($localeCode$availableLocalesCodestrue)) {
  35.             throw LocaleNotFoundException::notAvailable($localeCode$availableLocalesCodes);
  36.         }
  37.         return $localeCode;
  38.     }
  39. }