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

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.     /** @var ChannelContextInterface */
  20.     private $channelContext;
  21.     /** @var string */
  22.     private $defaultLocaleCode;
  23.     public function __construct(ChannelContextInterface $channelContextstring $defaultLocaleCode)
  24.     {
  25.         $this->channelContext $channelContext;
  26.         $this->defaultLocaleCode $defaultLocaleCode;
  27.     }
  28.     public function getAvailableLocalesCodes(): array
  29.     {
  30.         try {
  31.             /** @var ChannelInterface $channel */
  32.             $channel $this->channelContext->getChannel();
  33.             return $channel
  34.                 ->getLocales()
  35.                 ->map(function (LocaleInterface $locale) {
  36.                     return (string) $locale->getCode();
  37.                 })
  38.                 ->toArray()
  39.             ;
  40.         } catch (ChannelNotFoundException $exception) {
  41.             return [$this->defaultLocaleCode];
  42.         }
  43.     }
  44.     public function getDefaultLocaleCode(): string
  45.     {
  46.         try {
  47.             /** @var ChannelInterface $channel */
  48.             $channel $this->channelContext->getChannel();
  49.             return $channel->getDefaultLocale()->getCode();
  50.         } catch (ChannelNotFoundException $exception) {
  51.             return $this->defaultLocaleCode;
  52.         }
  53.     }
  54. }