vendor/sylius/sylius/src/Sylius/Bundle/CoreBundle/Theme/ChannelBasedThemeContext.php line 41

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.     public function getTheme(): ?ThemeInterface
  30.     {
  31.         try {
  32.             /** @var ChannelInterface $channel */
  33.             $channel $this->channelContext->getChannel();
  34.             $themeName $channel->getThemeName();
  35.             if (null === $themeName) {
  36.                 return null;
  37.             }
  38.             return $this->themeRepository->findOneByName($themeName);
  39.         } catch (ChannelNotFoundException $exception) {
  40.             return null;
  41.         } catch (\Exception $exception) {
  42.             return null;
  43.         }
  44.     }
  45. }