vendor/store.shopware.com/swagb2bplatform/components/Shop/BridgePlatform/SalesChannelContextFactoryDecorator.php line 27

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\B2B\Shop\BridgePlatform;
  3. use Shopware\B2B\Common\IdValue;
  4. use Shopware\B2B\SalesRepresentative\Framework\SalesRepresentativeIdentity;
  5. use Shopware\B2B\StoreFrontAuthentication\BridgePlatform\B2bIdentityIdStruct;
  6. use Shopware\B2B\StoreFrontAuthentication\BridgePlatform\SalesChannelContextAuthStorageAdapter;
  7. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. use function is_string;
  11. use function unserialize;
  12. class SalesChannelContextFactoryDecorator extends SalesChannelContextFactory
  13. {
  14.     private SalesChannelContextFactory $decorated;
  15.     private ContainerInterface $container;
  16.     public function __construct(SalesChannelContextFactory $decoratedContainerInterface $container)
  17.     {
  18.         $this->decorated $decorated;
  19.         $this->container $container;
  20.     }
  21.     public function create(string $tokenstring $salesChannelId, array $options = []): SalesChannelContext
  22.     {
  23.         $context $this->decorated->create($token$salesChannelId$options);
  24.         if (isset($options[SalesChannelContextAuthStorageAdapter::IDENTITY_ID_KEY])) {
  25.             $this->addB2bExtension(
  26.                 $context,
  27.                 $this->createIdValue($options[SalesChannelContextAuthStorageAdapter::IDENTITY_ID_KEY]),
  28.                 SalesChannelContextAuthStorageAdapter::IDENTITY_ID_KEY
  29.             );
  30.         }
  31.         if (isset($options[SalesRepresentativeIdentity::AUTH_ID_KEY])) {
  32.             $this->addB2bExtension(
  33.                 $context,
  34.                 $this->createIdValue($options[SalesRepresentativeIdentity::AUTH_ID_KEY]),
  35.                 SalesRepresentativeIdentity::AUTH_ID_KEY
  36.             );
  37.         }
  38.         if (!$this->isSalesChannelContextProviderInitialized()) {
  39.             return $context;
  40.         }
  41.         $salesChannelContextProvider $this->getSalesChannelContextProvider();
  42.         if (!$salesChannelContextProvider->hasSalesChannelContext()) {
  43.             $salesChannelContextProvider->setSalesChannelContext($context);
  44.         }
  45.         return $context;
  46.     }
  47.     /**
  48.      * @internal
  49.      */
  50.     protected function getSalesChannelContextProvider(): ContextProvider
  51.     {
  52.         return $this->container->get('b2b_shop.context_provider');
  53.     }
  54.     /**
  55.      * @internal
  56.      */
  57.     protected function isSalesChannelContextProviderInitialized(): bool
  58.     {
  59.         return $this->container->initialized('b2b_shop.context_provider');
  60.     }
  61.     /**
  62.      * @internal
  63.      */
  64.     protected function addB2bExtension(SalesChannelContext $context$identityIdstring $name): void
  65.     {
  66.         $context->addExtension(
  67.             $name,
  68.             new B2bIdentityIdStruct(IdValue::create($identityId))
  69.         );
  70.     }
  71.     /**
  72.      * @internal
  73.      */
  74.     protected function createIdValue($value): IdValue
  75.     {
  76.         if (is_string($value)) {
  77.             $identity unserialize($value);
  78.             if ($identity instanceof B2bIdentityIdStruct) {
  79.                 return $identity->getIdentityId();
  80.             }
  81.             if ($identity instanceof IdValue) {
  82.                 return $identity;
  83.             }
  84.         }
  85.         return IdValue::create($value);
  86.     }
  87. }