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