vendor/sylius/sylius/src/Sylius/Component/Channel/Context/RequestBased/ChannelContext.php line 45

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.     private RequestResolverInterface $requestResolver;
  20.     private RequestStack $requestStack;
  21.     public function __construct(RequestResolverInterface $requestResolverRequestStack $requestStack)
  22.     {
  23.         $this->requestResolver $requestResolver;
  24.         $this->requestStack $requestStack;
  25.     }
  26.     public function getChannel(): ChannelInterface
  27.     {
  28.         try {
  29.             return $this->getChannelForRequest($this->getMasterRequest());
  30.         } catch (\UnexpectedValueException $exception) {
  31.             throw new ChannelNotFoundException(null$exception);
  32.         }
  33.     }
  34.     private function getChannelForRequest(Request $request): ChannelInterface
  35.     {
  36.         $channel $this->requestResolver->findChannel($request);
  37.         $this->assertChannelWasFound($channel);
  38.         return $channel;
  39.     }
  40.     private function getMasterRequest(): Request
  41.     {
  42.         $masterRequest $this->requestStack->getMasterRequest();
  43.         if (null === $masterRequest) {
  44.             throw new \UnexpectedValueException('There are not any requests on request stack');
  45.         }
  46.         return $masterRequest;
  47.     }
  48.     private function assertChannelWasFound(?ChannelInterface $channel): void
  49.     {
  50.         if (null === $channel) {
  51.             throw new \UnexpectedValueException('Channel was not found for given request');
  52.         }
  53.     }
  54. }