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

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 \SplObjectStorage $requestToChannelMap;
  18.     private \SplObjectStorage $requestToExceptionMap;
  19.     public function __construct(private ChannelContextInterface $decoratedChannelContext, private RequestStack $requestStack)
  20.     {
  21.         $this->requestToChannelMap = new \SplObjectStorage();
  22.         $this->requestToExceptionMap = new \SplObjectStorage();
  23.     }
  24.     public function getChannel(): ChannelInterface
  25.     {
  26.         if (\method_exists($this->requestStack'getMainRequest')) {
  27.             $objectIdentifier $this->requestStack->getMainRequest();
  28.         } else {
  29.             $objectIdentifier $this->requestStack->getMasterRequest();
  30.         }
  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. }