vendor/sylius/sylius/src/Sylius/Component/Channel/Context/CachedPerRequestChannelContext.php line 43

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;
  12. use Sylius\Component\Channel\Model\ChannelInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. final class CachedPerRequestChannelContext implements ChannelContextInterface
  16. {
  17.     /** @var ChannelContextInterface */
  18.     private $decoratedChannelContext;
  19.     /** @var RequestStack */
  20.     private $requestStack;
  21.     /** @var \SplObjectStorage<Request, ChannelInterface> */
  22.     private $requestToChannelMap;
  23.     /** @var \SplObjectStorage<Request, ChannelNotFoundException> */
  24.     private $requestToExceptionMap;
  25.     public function __construct(ChannelContextInterface $decoratedChannelContextRequestStack $requestStack)
  26.     {
  27.         $this->decoratedChannelContext $decoratedChannelContext;
  28.         $this->requestStack $requestStack;
  29.         $this->requestToChannelMap = new \SplObjectStorage();
  30.         $this->requestToExceptionMap = new \SplObjectStorage();
  31.     }
  32.     public function getChannel(): ChannelInterface
  33.     {
  34.         $objectIdentifier $this->requestStack->getMasterRequest();
  35.         if (null === $objectIdentifier) {
  36.             return $this->decoratedChannelContext->getChannel();
  37.         }
  38.         if (isset($this->requestToExceptionMap[$objectIdentifier])) {
  39.             throw $this->requestToExceptionMap[$objectIdentifier];
  40.         }
  41.         try {
  42.             if (!isset($this->requestToChannelMap[$objectIdentifier])) {
  43.                 $this->requestToChannelMap[$objectIdentifier] = $this->decoratedChannelContext->getChannel();
  44.             }
  45.             return $this->requestToChannelMap[$objectIdentifier];
  46.         } catch (ChannelNotFoundException $exception) {
  47.             $this->requestToExceptionMap[$objectIdentifier] = $exception;
  48.             throw $exception;
  49.         }
  50.     }
  51. }