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