vendor/shopware/platform/src/Core/System/SalesChannel/Context/SalesChannelContextService.php line 102

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