vendor/shopware/core/System/SalesChannel/SalesChannelContext.php line 24

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\SalesChannel;
  3. use Shopware\Core\Checkout\Cart\CartException;
  4. use Shopware\Core\Checkout\Cart\Delivery\Struct\ShippingLocation;
  5. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRule;
  6. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
  7. use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupEntity;
  8. use Shopware\Core\Checkout\Customer\CustomerEntity;
  9. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  10. use Shopware\Core\Checkout\Shipping\ShippingMethodEntity;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Pricing\CashRoundingConfig;
  13. use Shopware\Core\Framework\Feature;
  14. use Shopware\Core\Framework\Log\Package;
  15. use Shopware\Core\Framework\Struct\StateAwareTrait;
  16. use Shopware\Core\Framework\Struct\Struct;
  17. use Shopware\Core\System\Currency\CurrencyEntity;
  18. use Shopware\Core\System\SalesChannel\Exception\ContextPermissionsLockedException;
  19. use Shopware\Core\System\SalesChannel\Exception\ContextRulesLockedException;
  20. use Shopware\Core\System\Tax\Exception\TaxNotFoundException;
  21. use Shopware\Core\System\Tax\TaxCollection;
  22. #[Package('core')]
  23. class SalesChannelContext extends Struct
  24. {
  25.     use StateAwareTrait;
  26.     /**
  27.      * Unique token for context, e.g. stored in session or provided in request headers
  28.      *
  29.      * @var string
  30.      */
  31.     protected $token;
  32.     /**
  33.      * @var CustomerGroupEntity
  34.      */
  35.     protected $currentCustomerGroup;
  36.     /**
  37.      * @var CustomerGroupEntity
  38.      */
  39.     protected $fallbackCustomerGroup;
  40.     /**
  41.      * @var CurrencyEntity
  42.      */
  43.     protected $currency;
  44.     /**
  45.      * @var SalesChannelEntity
  46.      */
  47.     protected $salesChannel;
  48.     /**
  49.      * @var TaxCollection
  50.      */
  51.     protected $taxRules;
  52.     /**
  53.      * @var CustomerEntity|null
  54.      */
  55.     protected $customer;
  56.     /**
  57.      * @var PaymentMethodEntity
  58.      */
  59.     protected $paymentMethod;
  60.     /**
  61.      * @var ShippingMethodEntity
  62.      */
  63.     protected $shippingMethod;
  64.     /**
  65.      * @var ShippingLocation
  66.      */
  67.     protected $shippingLocation;
  68.     /**
  69.      * @var string[]
  70.      */
  71.     protected $rulesIds;
  72.     /**
  73.      * @var array<string, string[]>
  74.      */
  75.     protected array $areaRuleIds;
  76.     /**
  77.      * @var bool
  78.      */
  79.     protected $rulesLocked false;
  80.     /**
  81.      * @var mixed[]
  82.      */
  83.     protected $permissions = [];
  84.     /**
  85.      * @var bool
  86.      */
  87.     protected $permisionsLocked false;
  88.     /**
  89.      * @var Context
  90.      */
  91.     protected $context;
  92.     /**
  93.      * @var CashRoundingConfig
  94.      */
  95.     private $itemRounding;
  96.     /**
  97.      * @var CashRoundingConfig
  98.      */
  99.     private $totalRounding;
  100.     /**
  101.      * @var string|null
  102.      */
  103.     private $domainId;
  104.     /**
  105.      * @deprecated tag:v6.5.0 - __construct will be internal, use context factory to create a new context
  106.      * @deprecated tag:v6.5.0 - Parameter $fallbackCustomerGroup is deprecated and will be removed
  107.      *
  108.      * @param array<string> $rulesIds
  109.      * @param array<string, string[]> $areaRuleIds
  110.      */
  111.     public function __construct(
  112.         Context $baseContext,
  113.         string $token,
  114.         ?string $domainId,
  115.         SalesChannelEntity $salesChannel,
  116.         CurrencyEntity $currency,
  117.         CustomerGroupEntity $currentCustomerGroup,
  118.         CustomerGroupEntity $fallbackCustomerGroup,
  119.         TaxCollection $taxRules,
  120.         PaymentMethodEntity $paymentMethod,
  121.         ShippingMethodEntity $shippingMethod,
  122.         ShippingLocation $shippingLocation,
  123.         ?CustomerEntity $customer,
  124.         CashRoundingConfig $itemRounding,
  125.         CashRoundingConfig $totalRounding,
  126.         array $rulesIds = [],
  127.         array $areaRuleIds = []
  128.     ) {
  129.         $this->currentCustomerGroup $currentCustomerGroup;
  130.         $this->fallbackCustomerGroup $fallbackCustomerGroup;
  131.         $this->currency $currency;
  132.         $this->salesChannel $salesChannel;
  133.         $this->taxRules $taxRules;
  134.         $this->customer $customer;
  135.         $this->paymentMethod $paymentMethod;
  136.         $this->shippingMethod $shippingMethod;
  137.         $this->shippingLocation $shippingLocation;
  138.         $this->rulesIds $rulesIds;
  139.         $this->areaRuleIds $areaRuleIds;
  140.         $this->token $token;
  141.         $this->context $baseContext;
  142.         $this->itemRounding $itemRounding;
  143.         $this->totalRounding $totalRounding;
  144.         $this->domainId $domainId;
  145.     }
  146.     public function getCurrentCustomerGroup(): CustomerGroupEntity
  147.     {
  148.         return $this->currentCustomerGroup;
  149.     }
  150.     /**
  151.      * @deprecated tag:v6.5.0 - Fallback customer group is deprecated and will be removed, use getCurrentCustomerGroup instead
  152.      */
  153.     public function getFallbackCustomerGroup(): CustomerGroupEntity
  154.     {
  155.         Feature::triggerDeprecationOrThrow(
  156.             'v6.5.0.0',
  157.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0''getCurrentCustomerGroup()')
  158.         );
  159.         return $this->fallbackCustomerGroup;
  160.     }
  161.     public function getCurrency(): CurrencyEntity
  162.     {
  163.         return $this->currency;
  164.     }
  165.     public function getSalesChannel(): SalesChannelEntity
  166.     {
  167.         return $this->salesChannel;
  168.     }
  169.     public function getTaxRules(): TaxCollection
  170.     {
  171.         return $this->taxRules;
  172.     }
  173.     /**
  174.      * Get the tax rules depend on the customer billing address
  175.      * respectively the shippingLocation if there is no customer
  176.      */
  177.     public function buildTaxRules(string $taxId): TaxRuleCollection
  178.     {
  179.         $tax $this->taxRules->get($taxId);
  180.         if ($tax === null || $tax->getRules() === null) {
  181.             throw new TaxNotFoundException($taxId);
  182.         }
  183.         if ($tax->getRules()->first() !== null) {
  184.             // NEXT-21735 - This is covered randomly
  185.             // @codeCoverageIgnoreStart
  186.             return new TaxRuleCollection([
  187.                 new TaxRule($tax->getRules()->first()->getTaxRate(), 100),
  188.             ]);
  189.             // @codeCoverageIgnoreEnd
  190.         }
  191.         return new TaxRuleCollection([
  192.             new TaxRule($tax->getTaxRate(), 100),
  193.         ]);
  194.     }
  195.     public function getCustomer(): ?CustomerEntity
  196.     {
  197.         return $this->customer;
  198.     }
  199.     public function getPaymentMethod(): PaymentMethodEntity
  200.     {
  201.         return $this->paymentMethod;
  202.     }
  203.     public function getShippingMethod(): ShippingMethodEntity
  204.     {
  205.         return $this->shippingMethod;
  206.     }
  207.     public function getShippingLocation(): ShippingLocation
  208.     {
  209.         return $this->shippingLocation;
  210.     }
  211.     public function getContext(): Context
  212.     {
  213.         return $this->context;
  214.     }
  215.     /**
  216.      * @return string[]
  217.      */
  218.     public function getRuleIds(): array
  219.     {
  220.         return $this->rulesIds;
  221.     }
  222.     /**
  223.      * @param array<string> $ruleIds
  224.      */
  225.     public function setRuleIds(array $ruleIds): void
  226.     {
  227.         if ($this->rulesLocked) {
  228.             throw new ContextRulesLockedException();
  229.         }
  230.         $this->rulesIds array_filter(array_values($ruleIds));
  231.         $this->getContext()->setRuleIds($this->rulesIds);
  232.     }
  233.     /**
  234.      * @internal
  235.      *
  236.      * @return array<string, string[]>
  237.      */
  238.     public function getAreaRuleIds(): array
  239.     {
  240.         return $this->areaRuleIds;
  241.     }
  242.     /**
  243.      * @internal
  244.      *
  245.      * @param string[] $areas
  246.      *
  247.      * @return string[]
  248.      */
  249.     public function getRuleIdsByAreas(array $areas): array
  250.     {
  251.         $ruleIds = [];
  252.         foreach ($areas as $area) {
  253.             if (empty($this->areaRuleIds[$area])) {
  254.                 continue;
  255.             }
  256.             $ruleIds array_unique(array_merge($ruleIds$this->areaRuleIds[$area]));
  257.         }
  258.         return array_values($ruleIds);
  259.     }
  260.     /**
  261.      * @internal
  262.      *
  263.      * @param array<string, string[]> $areaRuleIds
  264.      */
  265.     public function setAreaRuleIds(array $areaRuleIds): void
  266.     {
  267.         $this->areaRuleIds $areaRuleIds;
  268.     }
  269.     public function lockRules(): void
  270.     {
  271.         $this->rulesLocked true;
  272.     }
  273.     public function lockPermissions(): void
  274.     {
  275.         $this->permisionsLocked true;
  276.     }
  277.     public function getToken(): string
  278.     {
  279.         return $this->token;
  280.     }
  281.     public function getTaxState(): string
  282.     {
  283.         return $this->context->getTaxState();
  284.     }
  285.     public function setTaxState(string $taxState): void
  286.     {
  287.         $this->context->setTaxState($taxState);
  288.     }
  289.     public function getTaxCalculationType(): string
  290.     {
  291.         return $this->getSalesChannel()->getTaxCalculationType();
  292.     }
  293.     /**
  294.      * @return mixed[]
  295.      */
  296.     public function getPermissions(): array
  297.     {
  298.         return $this->permissions;
  299.     }
  300.     /**
  301.      * @param mixed[] $permissions
  302.      */
  303.     public function setPermissions(array $permissions): void
  304.     {
  305.         if ($this->permisionsLocked) {
  306.             throw new ContextPermissionsLockedException();
  307.         }
  308.         $this->permissions array_filter($permissions);
  309.     }
  310.     public function getApiAlias(): string
  311.     {
  312.         return 'sales_channel_context';
  313.     }
  314.     public function hasPermission(string $permission): bool
  315.     {
  316.         return \array_key_exists($permission$this->permissions) && (bool) $this->permissions[$permission];
  317.     }
  318.     public function getSalesChannelId(): string
  319.     {
  320.         return $this->getSalesChannel()->getId();
  321.     }
  322.     public function addState(string ...$states): void
  323.     {
  324.         $this->context->addState(...$states);
  325.     }
  326.     public function removeState(string $state): void
  327.     {
  328.         $this->context->removeState($state);
  329.     }
  330.     public function hasState(string ...$states): bool
  331.     {
  332.         return $this->context->hasState(...$states);
  333.     }
  334.     /**
  335.      * @return string[]
  336.      */
  337.     public function getStates(): array
  338.     {
  339.         return $this->context->getStates();
  340.     }
  341.     public function getDomainId(): ?string
  342.     {
  343.         return $this->domainId;
  344.     }
  345.     public function setDomainId(?string $domainId): void
  346.     {
  347.         $this->domainId $domainId;
  348.     }
  349.     /**
  350.      * @return string[]
  351.      */
  352.     public function getLanguageIdChain(): array
  353.     {
  354.         return $this->context->getLanguageIdChain();
  355.     }
  356.     public function getLanguageId(): string
  357.     {
  358.         return $this->context->getLanguageId();
  359.     }
  360.     public function getVersionId(): string
  361.     {
  362.         return $this->context->getVersionId();
  363.     }
  364.     public function considerInheritance(): bool
  365.     {
  366.         return $this->context->considerInheritance();
  367.     }
  368.     public function getTotalRounding(): CashRoundingConfig
  369.     {
  370.         return $this->totalRounding;
  371.     }
  372.     public function setTotalRounding(CashRoundingConfig $totalRounding): void
  373.     {
  374.         $this->totalRounding $totalRounding;
  375.     }
  376.     public function getItemRounding(): CashRoundingConfig
  377.     {
  378.         return $this->itemRounding;
  379.     }
  380.     public function setItemRounding(CashRoundingConfig $itemRounding): void
  381.     {
  382.         $this->itemRounding $itemRounding;
  383.     }
  384.     public function getCurrencyId(): string
  385.     {
  386.         return $this->getCurrency()->getId();
  387.     }
  388.     public function ensureLoggedIn(bool $allowGuest true): void
  389.     {
  390.         if ($this->customer === null) {
  391.             throw CartException::customerNotLoggedIn();
  392.         }
  393.         if (!$allowGuest && $this->customer->getGuest()) {
  394.             throw CartException::customerNotLoggedIn();
  395.         }
  396.     }
  397.     public function getCustomerId(): ?string
  398.     {
  399.         return $this->customer $this->customer->getId() : null;
  400.     }
  401. }