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

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