vendor/sylius/sylius/src/Sylius/Component/Channel/Context/RequestBased/ChannelContext.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\Component\Channel\Context\RequestBased;
  12. use Sylius\Component\Channel\Context\ChannelContextInterface;
  13. use Sylius\Component\Channel\Context\ChannelNotFoundException;
  14. use Sylius\Component\Channel\Model\ChannelInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. final class ChannelContext implements ChannelContextInterface
  18. {
  19.     /** @var RequestResolverInterface */
  20.     private $requestResolver;
  21.     /** @var RequestStack */
  22.     private $requestStack;
  23.     public function __construct(RequestResolverInterface $requestResolverRequestStack $requestStack)
  24.     {
  25.         $this->requestResolver $requestResolver;
  26.         $this->requestStack $requestStack;
  27.     }
  28.     /**
  29.      * {@inheritdoc}
  30.      */
  31.     public function getChannel(): ChannelInterface
  32.     {
  33.         try {
  34.             return $this->getChannelForRequest($this->getMasterRequest());
  35.         } catch (\UnexpectedValueException $exception) {
  36.             throw new ChannelNotFoundException($exception);
  37.         }
  38.     }
  39.     private function getChannelForRequest(Request $request): ChannelInterface
  40.     {
  41.         $channel $this->requestResolver->findChannel($request);
  42.         $this->assertChannelWasFound($channel);
  43.         return $channel;
  44.     }
  45.     private function getMasterRequest(): Request
  46.     {
  47.         $masterRequest $this->requestStack->getMasterRequest();
  48.         if (null === $masterRequest) {
  49.             throw new \UnexpectedValueException('There are not any requests on request stack');
  50.         }
  51.         return $masterRequest;
  52.     }
  53.     private function assertChannelWasFound(?ChannelInterface $channel): void
  54.     {
  55.         if (null === $channel) {
  56.             throw new \UnexpectedValueException('Channel was not found for given request');
  57.         }
  58.     }
  59. }