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

  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(private readonly AbstractSalesChannelContextFactory $factory, private readonly CartRuleLoader $ruleLoader, private readonly SalesChannelContextPersister $contextPersister, private readonly CartService $cartService, private readonly EventDispatcherInterface $eventDispatcher)
  32.     {
  33.     }
  34.     public function get(SalesChannelContextServiceParameters $parameters): SalesChannelContext
  35.     {
  36.         return Profiler::trace('sales-channel-context', function () use ($parameters) {
  37.             $token $parameters->getToken();
  38.             $session $this->contextPersister->load($token$parameters->getSalesChannelId());
  39.             if ($session['expired'] ?? false) {
  40.                 $token Random::getAlphanumericString(32);
  41.             }
  42.             if ($parameters->getLanguageId() !== null) {
  43.                 $session[self::LANGUAGE_ID] = $parameters->getLanguageId();
  44.             }
  45.             if ($parameters->getCurrencyId() !== null && !\array_key_exists(self::CURRENCY_ID$session)) {
  46.                 $session[self::CURRENCY_ID] = $parameters->getCurrencyId();
  47.             }
  48.             if ($parameters->getDomainId() !== null) {
  49.                 $session[self::DOMAIN_ID] = $parameters->getDomainId();
  50.             }
  51.             if ($parameters->getOriginalContext() !== null) {
  52.                 $session[self::ORIGINAL_CONTEXT] = $parameters->getOriginalContext();
  53.             }
  54.             if ($parameters->getCustomerId() !== null) {
  55.                 $session[self::CUSTOMER_ID] = $parameters->getCustomerId();
  56.             }
  57.             $context $this->factory->create($token$parameters->getSalesChannelId(), $session);
  58.             $this->eventDispatcher->dispatch(new SalesChannelContextCreatedEvent($context$token));
  59.             $result $this->ruleLoader->loadByToken($context$token);
  60.             $this->cartService->setCart($result->getCart());
  61.             return $context;
  62.         });
  63.     }
  64. }