vendor/sylius/sylius/src/Sylius/Component/Core/Provider/ChannelBasedLocaleProvider.php line 38

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\Provider;
  12. use Sylius\Component\Channel\Context\ChannelContextInterface;
  13. use Sylius\Component\Channel\Context\ChannelNotFoundException;
  14. use Sylius\Component\Core\Model\ChannelInterface;
  15. use Sylius\Component\Locale\Model\LocaleInterface;
  16. use Sylius\Component\Locale\Provider\LocaleProviderInterface;
  17. final class ChannelBasedLocaleProvider implements LocaleProviderInterface
  18. {
  19.     private ChannelContextInterface $channelContext;
  20.     private string $defaultLocaleCode;
  21.     public function __construct(ChannelContextInterface $channelContextstring $defaultLocaleCode)
  22.     {
  23.         $this->channelContext $channelContext;
  24.         $this->defaultLocaleCode $defaultLocaleCode;
  25.     }
  26.     public function getAvailableLocalesCodes(): array
  27.     {
  28.         try {
  29.             /** @var ChannelInterface $channel */
  30.             $channel $this->channelContext->getChannel();
  31.             return $channel
  32.                 ->getLocales()
  33.                 ->map(function (LocaleInterface $locale) {
  34.                     return (string) $locale->getCode();
  35.                 })
  36.                 ->toArray()
  37.             ;
  38.         } catch (ChannelNotFoundException $exception) {
  39.             return [$this->defaultLocaleCode];
  40.         }
  41.     }
  42.     public function getDefaultLocaleCode(): string
  43.     {
  44.         try {
  45.             /** @var ChannelInterface $channel */
  46.             $channel $this->channelContext->getChannel();
  47.             return $channel->getDefaultLocale()->getCode();
  48.         } catch (ChannelNotFoundException $exception) {
  49.             return $this->defaultLocaleCode;
  50.         }
  51.     }
  52. }