vendor/sylius/sylius/src/Sylius/Bundle/CoreBundle/Theme/ChannelBasedThemeContext.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\Bundle\CoreBundle\Theme;
  12. use Sylius\Bundle\ThemeBundle\Context\ThemeContextInterface;
  13. use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
  14. use Sylius\Bundle\ThemeBundle\Repository\ThemeRepositoryInterface;
  15. use Sylius\Component\Channel\Context\ChannelContextInterface;
  16. use Sylius\Component\Channel\Context\ChannelNotFoundException;
  17. use Sylius\Component\Core\Model\ChannelInterface;
  18. final class ChannelBasedThemeContext implements ThemeContextInterface
  19. {
  20.     /** @var ChannelContextInterface */
  21.     private $channelContext;
  22.     /** @var ThemeRepositoryInterface */
  23.     private $themeRepository;
  24.     public function __construct(ChannelContextInterface $channelContextThemeRepositoryInterface $themeRepository)
  25.     {
  26.         $this->channelContext $channelContext;
  27.         $this->themeRepository $themeRepository;
  28.     }
  29.     /**
  30.      * {@inheritdoc}
  31.      */
  32.     public function getTheme(): ?ThemeInterface
  33.     {
  34.         try {
  35.             /** @var ChannelInterface $channel */
  36.             $channel $this->channelContext->getChannel();
  37.             $themeName $channel->getThemeName();
  38.             if (null === $themeName) {
  39.                 return null;
  40.             }
  41.             return $this->themeRepository->findOneByName($themeName);
  42.         } catch (ChannelNotFoundException $exception) {
  43.             return null;
  44.         } catch (\Exception $exception) {
  45.             return null;
  46.         }
  47.     }
  48. }