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

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\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. public const CURRENCY_ID = 'currencyId';
  15. public const LANGUAGE_ID = 'languageId';
  16. public const CUSTOMER_ID = 'customerId';
  17. public const CUSTOMER_GROUP_ID = 'customerGroupId';
  18. public const BILLING_ADDRESS_ID = 'billingAddressId';
  19. public const SHIPPING_ADDRESS_ID = 'shippingAddressId';
  20. public const PAYMENT_METHOD_ID = 'paymentMethodId';
  21. public const SHIPPING_METHOD_ID = 'shippingMethodId';
  22. public const COUNTRY_ID = 'countryId';
  23. public const COUNTRY_STATE_ID = 'countryStateId';
  24. public const VERSION_ID = 'version-id';
  25. public const PERMISSIONS = 'permissions';
  26. public const DOMAIN_ID = 'domainId';
  27. public const ORIGINAL_CONTEXT = 'originalContext';
  28. private AbstractSalesChannelContextFactory $factory;
  29. private CartRuleLoader $ruleLoader;
  30. private SalesChannelContextPersister $contextPersister;
  31. private CartService $cartService;
  32. private EventDispatcherInterface $eventDispatcher;
  33. /**
  34. * @internal
  35. */
  36. public function __construct(
  37. AbstractSalesChannelContextFactory $factory,
  38. CartRuleLoader $ruleLoader,
  39. SalesChannelContextPersister $contextPersister,
  40. CartService $cartService,
  41. EventDispatcherInterface $eventDispatcher
  42. ) {
  43. $this->factory = $factory;
  44. $this->ruleLoader = $ruleLoader;
  45. $this->contextPersister = $contextPersister;
  46. $this->cartService = $cartService;
  47. $this->eventDispatcher = $eventDispatcher;
  48. }
  49. public function get(SalesChannelContextServiceParameters $parameters): SalesChannelContext
  50. {
  51. return Profiler::trace('sales-channel-context', function () use ($parameters) {
  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. $this->eventDispatcher->dispatch(new SalesChannelContextCreatedEvent($context, $token));
  74. $result = $this->ruleLoader->loadByToken($context, $token);
  75. $this->cartService->setCart($result->getCart());
  76. return $context;
  77. });
  78. }
  79. }