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

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.     private ChannelContextInterface $decoratedChannelContext;
  17.     private RequestStack $requestStack;
  18.     private \SplObjectStorage $requestToChannelMap;
  19.     private \SplObjectStorage $requestToExceptionMap;
  20.     public function __construct(ChannelContextInterface $decoratedChannelContextRequestStack $requestStack)
  21.     {
  22.         $this->decoratedChannelContext $decoratedChannelContext;
  23.         $this->requestStack $requestStack;
  24.         $this->requestToChannelMap = new \SplObjectStorage();
  25.         $this->requestToExceptionMap = new \SplObjectStorage();
  26.     }
  27.     public function getChannel(): ChannelInterface
  28.     {
  29.         $objectIdentifier $this->requestStack->getMasterRequest();
  30.         if (null === $objectIdentifier) {
  31.             return $this->decoratedChannelContext->getChannel();
  32.         }
  33.         if (isset($this->requestToExceptionMap[$objectIdentifier])) {
  34.             throw $this->requestToExceptionMap[$objectIdentifier];
  35.         }
  36.         try {
  37.             if (!isset($this->requestToChannelMap[$objectIdentifier])) {
  38.                 $this->requestToChannelMap[$objectIdentifier] = $this->decoratedChannelContext->getChannel();
  39.             }
  40.             return $this->requestToChannelMap[$objectIdentifier];
  41.         } catch (ChannelNotFoundException $exception) {
  42.             $this->requestToExceptionMap[$objectIdentifier] = $exception;
  43.             throw $exception;
  44.         }
  45.     }
  46. }