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

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