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

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\SalesChannelContext;
  8. class SalesChannelContextService implements SalesChannelContextServiceInterface
  9. {
  10.     public const CURRENCY_ID = 'currencyId';
  11.     public const LANGUAGE_ID = 'languageId';
  12.     public const CUSTOMER_ID = 'customerId';
  13.     public const CUSTOMER_GROUP_ID = 'customerGroupId';
  14.     public const BILLING_ADDRESS_ID = 'billingAddressId';
  15.     public const SHIPPING_ADDRESS_ID = 'shippingAddressId';
  16.     public const PAYMENT_METHOD_ID = 'paymentMethodId';
  17.     public const SHIPPING_METHOD_ID = 'shippingMethodId';
  18.     public const COUNTRY_ID = 'countryId';
  19.     public const COUNTRY_STATE_ID = 'countryStateId';
  20.     public const VERSION_ID = 'version-id';
  21.     public const PERMISSIONS = 'permissions';
  22.     public const DOMAIN_ID = 'domainId';
  23.     public const ORIGINAL_CONTEXT = 'originalContext';
  24.     private AbstractSalesChannelContextFactory $factory;
  25.     private CartRuleLoader $ruleLoader;
  26.     private SalesChannelContextPersister $contextPersister;
  27.     private CartService $cartService;
  28.     /**
  29.      * @internal
  30.      */
  31.     public function __construct(
  32.         AbstractSalesChannelContextFactory $factory,
  33.         CartRuleLoader $ruleLoader,
  34.         SalesChannelContextPersister $contextPersister,
  35.         CartService $cartService
  36.     ) {
  37.         $this->factory = $factory;
  38.         $this->ruleLoader = $ruleLoader;
  39.         $this->contextPersister = $contextPersister;
  40.         $this->cartService = $cartService;
  41.     }
  42.     public function get(SalesChannelContextServiceParameters $parameters): SalesChannelContext
  43.     {
  44.         return Profiler::trace('sales-channel-context', function () use ($parameters) {
  45.             $token = $parameters->getToken();
  46.             $session = $this->contextPersister->load($token, $parameters->getSalesChannelId());
  47.             if ($session['expired'] ?? false) {
  48.                 $token = Random::getAlphanumericString(32);
  49.             }
  50.             if ($parameters->getLanguageId() !== null) {
  51.                 $session[self::LANGUAGE_ID] = $parameters->getLanguageId();
  52.             }
  53.             if ($parameters->getCurrencyId() !== null && !\array_key_exists(self::CURRENCY_ID, $session)) {
  54.                 $session[self::CURRENCY_ID] = $parameters->getCurrencyId();
  55.             }
  56.             if ($parameters->getDomainId() !== null) {
  57.                 $session[self::DOMAIN_ID] = $parameters->getDomainId();
  58.             }
  59.             if ($parameters->getOriginalContext() !== null) {
  60.                 $session[self::ORIGINAL_CONTEXT] = $parameters->getOriginalContext();
  61.             }
  62.             if ($parameters->getCustomerId() !== null) {
  63.                 $session[self::CUSTOMER_ID] = $parameters->getCustomerId();
  64.             }
  65.             $context = $this->factory->create($token, $parameters->getSalesChannelId(), $session);
  66.             $result = $this->ruleLoader->loadByToken($context, $token);
  67.             $this->cartService->setCart($result->getCart());
  68.             return $context;
  69.         });
  70.     }
  71. }