vendor/shopware/core/System/SalesChannel/Context/SalesChannelContextService.php line 109

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\SalesChannel\Context;
  3. use Shopware\Core\Checkout\Cart\CartRuleLoader;
  4. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  5. use Shopware\Core\Framework\Util\Random;
  6. use Shopware\Core\Profiling\Profiler;
  7. use Shopware\Core\System\SalesChannel\Event\SalesChannelContextCreatedEvent;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. class SalesChannelContextService implements SalesChannelContextServiceInterface
  11. {
  12.     public const CURRENCY_ID 'currencyId';
  13.     public const LANGUAGE_ID 'languageId';
  14.     public const CUSTOMER_ID 'customerId';
  15.     public const CUSTOMER_GROUP_ID 'customerGroupId';
  16.     public const BILLING_ADDRESS_ID 'billingAddressId';
  17.     public const SHIPPING_ADDRESS_ID 'shippingAddressId';
  18.     public const PAYMENT_METHOD_ID 'paymentMethodId';
  19.     public const SHIPPING_METHOD_ID 'shippingMethodId';
  20.     public const COUNTRY_ID 'countryId';
  21.     public const COUNTRY_STATE_ID 'countryStateId';
  22.     public const VERSION_ID 'version-id';
  23.     public const PERMISSIONS 'permissions';
  24.     public const DOMAIN_ID 'domainId';
  25.     public const ORIGINAL_CONTEXT 'originalContext';
  26.     private AbstractSalesChannelContextFactory $factory;
  27.     private CartRuleLoader $ruleLoader;
  28.     private SalesChannelContextPersister $contextPersister;
  29.     private CartService $cartService;
  30.     private EventDispatcherInterface $eventDispatcher;
  31.     /**
  32.      * @internal
  33.      */
  34.     public function __construct(
  35.         AbstractSalesChannelContextFactory $factory,
  36.         CartRuleLoader $ruleLoader,
  37.         SalesChannelContextPersister $contextPersister,
  38.         CartService $cartService,
  39.         EventDispatcherInterface $eventDispatcher
  40.     ) {
  41.         $this->factory $factory;
  42.         $this->ruleLoader $ruleLoader;
  43.         $this->contextPersister $contextPersister;
  44.         $this->cartService $cartService;
  45.         $this->eventDispatcher $eventDispatcher;
  46.     }
  47.     public function get(SalesChannelContextServiceParameters $parameters): SalesChannelContext
  48.     {
  49.         return Profiler::trace('sales-channel-context', function () use ($parameters) {
  50.             $token $parameters->getToken();
  51.             $session $this->contextPersister->load($token$parameters->getSalesChannelId());
  52.             if ($session['expired'] ?? false) {
  53.                 $token Random::getAlphanumericString(32);
  54.             }
  55.             if ($parameters->getLanguageId() !== null) {
  56.                 $session[self::LANGUAGE_ID] = $parameters->getLanguageId();
  57.             }
  58.             if ($parameters->getCurrencyId() !== null && !\array_key_exists(self::CURRENCY_ID$session)) {
  59.                 $session[self::CURRENCY_ID] = $parameters->getCurrencyId();
  60.             }
  61.             if ($parameters->getDomainId() !== null) {
  62.                 $session[self::DOMAIN_ID] = $parameters->getDomainId();
  63.             }
  64.             if ($parameters->getOriginalContext() !== null) {
  65.                 $session[self::ORIGINAL_CONTEXT] = $parameters->getOriginalContext();
  66.             }
  67.             if ($parameters->getCustomerId() !== null) {
  68.                 $session[self::CUSTOMER_ID] = $parameters->getCustomerId();
  69.             }
  70.             $context $this->factory->create($token$parameters->getSalesChannelId(), $session);
  71.             $this->eventDispatcher->dispatch(new SalesChannelContextCreatedEvent($context$token));
  72.             $result $this->ruleLoader->loadByToken($context$token);
  73.             $this->cartService->setCart($result->getCart());
  74.             return $context;
  75.         });
  76.     }
  77. }