vendor/sylius/sylius/src/Sylius/Component/Channel/Context/RequestBased/ChannelContext.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\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.     public function getChannel(): ChannelInterface
  29.     {
  30.         try {
  31.             return $this->getChannelForRequest($this->getMasterRequest());
  32.         } catch (\UnexpectedValueException $exception) {
  33.             throw new ChannelNotFoundException($exception);
  34.         }
  35.     }
  36.     private function getChannelForRequest(Request $request): ChannelInterface
  37.     {
  38.         $channel $this->requestResolver->findChannel($request);
  39.         $this->assertChannelWasFound($channel);
  40.         return $channel;
  41.     }
  42.     private function getMasterRequest(): Request
  43.     {
  44.         $masterRequest $this->requestStack->getMasterRequest();
  45.         if (null === $masterRequest) {
  46.             throw new \UnexpectedValueException('There are not any requests on request stack');
  47.         }
  48.         return $masterRequest;
  49.     }
  50.     private function assertChannelWasFound(?ChannelInterface $channel): void
  51.     {
  52.         if (null === $channel) {
  53.             throw new \UnexpectedValueException('Channel was not found for given request');
  54.         }
  55.     }
  56. }