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 array
  68.      */
  69.     protected $rulesIds;
  70.     /**
  71.      * @var bool
  72.      */
  73.     protected $rulesLocked false;
  74.     /**
  75.      * @var array
  76.      */
  77.     protected $permissions = [];
  78.     /**
  79.      * @var bool
  80.      */
  81.     protected $permisionsLocked false;
  82.     /**
  83.      * @var Context
  84.      */
  85.     protected $context;
  86.     /**
  87.      * @var CashRoundingConfig
  88.      */
  89.     private $itemRounding;
  90.     /**
  91.      * @var CashRoundingConfig
  92.      */
  93.     private $totalRounding;
  94.     /**
  95.      * @var string|null
  96.      */
  97.     private $domainId;
  98.     /**
  99.      * @deprecated tag:v6.5.0 - __construct will be internal, use context factory to create a new context
  100.      * @deprecated tag:v6.5.0 - Parameter $fallbackCustomerGroup is deprecated and will be removed
  101.      *
  102.      * @param array<string> $rulesIds
  103.      */
  104.     public function __construct(
  105.         Context $baseContext,
  106.         string $token,
  107.         ?string $domainId,
  108.         SalesChannelEntity $salesChannel,
  109.         CurrencyEntity $currency,
  110.         CustomerGroupEntity $currentCustomerGroup,
  111.         CustomerGroupEntity $fallbackCustomerGroup,
  112.         TaxCollection $taxRules,
  113.         PaymentMethodEntity $paymentMethod,
  114.         ShippingMethodEntity $shippingMethod,
  115.         ShippingLocation $shippingLocation,
  116.         ?CustomerEntity $customer,
  117.         CashRoundingConfig $itemRounding,
  118.         CashRoundingConfig $totalRounding,
  119.         array $rulesIds = []
  120.     ) {
  121.         $this->currentCustomerGroup $currentCustomerGroup;
  122.         $this->fallbackCustomerGroup $fallbackCustomerGroup;
  123.         $this->currency $currency;
  124.         $this->salesChannel $salesChannel;
  125.         $this->taxRules $taxRules;
  126.         $this->customer $customer;
  127.         $this->paymentMethod $paymentMethod;
  128.         $this->shippingMethod $shippingMethod;
  129.         $this->shippingLocation $shippingLocation;
  130.         $this->rulesIds $rulesIds;
  131.         $this->token $token;
  132.         $this->context $baseContext;
  133.         $this->itemRounding $itemRounding;
  134.         $this->totalRounding $totalRounding;
  135.         $this->domainId $domainId;
  136.     }
  137.     public function getCurrentCustomerGroup(): CustomerGroupEntity
  138.     {
  139.         return $this->currentCustomerGroup;
  140.     }
  141.     /**
  142.      * @deprecated tag:v6.5.0 - Fallback customer group is deprecated and will be removed, use getCurrentCustomerGroup instead
  143.      */
  144.     public function getFallbackCustomerGroup(): CustomerGroupEntity
  145.     {
  146.         Feature::triggerDeprecationOrThrow(
  147.             'v6.5.0.0',
  148.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0''getCurrentCustomerGroup()')
  149.         );
  150.         return $this->fallbackCustomerGroup;
  151.     }
  152.     public function getCurrency(): CurrencyEntity
  153.     {
  154.         return $this->currency;
  155.     }
  156.     public function getSalesChannel(): SalesChannelEntity
  157.     {
  158.         return $this->salesChannel;
  159.     }
  160.     public function getTaxRules(): TaxCollection
  161.     {
  162.         return $this->taxRules;
  163.     }
  164.     /**
  165.      * Get the tax rules depend on the customer billing address
  166.      * respectively the shippingLocation if there is no customer
  167.      */
  168.     public function buildTaxRules(string $taxId): TaxRuleCollection
  169.     {
  170.         $tax $this->taxRules->get($taxId);
  171.         if ($tax === null || $tax->getRules() === null) {
  172.             throw new TaxNotFoundException($taxId);
  173.         }
  174.         if ($tax->getRules()->first() !== null) {
  175.             // NEXT-21735 - This is covered randomly
  176.             // @codeCoverageIgnoreStart
  177.             return new TaxRuleCollection([
  178.                 new TaxRule($tax->getRules()->first()->getTaxRate(), 100),
  179.             ]);
  180.             // @codeCoverageIgnoreEnd
  181.         }
  182.         return new TaxRuleCollection([
  183.             new TaxRule($tax->getTaxRate(), 100),
  184.         ]);
  185.     }
  186.     public function getCustomer(): ?CustomerEntity
  187.     {
  188.         return $this->customer;
  189.     }
  190.     public function getPaymentMethod(): PaymentMethodEntity
  191.     {
  192.         return $this->paymentMethod;
  193.     }
  194.     public function getShippingMethod(): ShippingMethodEntity
  195.     {
  196.         return $this->shippingMethod;
  197.     }
  198.     public function getShippingLocation(): ShippingLocation
  199.     {
  200.         return $this->shippingLocation;
  201.     }
  202.     public function getContext(): Context
  203.     {
  204.         return $this->context;
  205.     }
  206.     public function getRuleIds(): array
  207.     {
  208.         return $this->rulesIds;
  209.     }
  210.     /**
  211.      * @param array<string> $ruleIds
  212.      */
  213.     public function setRuleIds(array $ruleIds): void
  214.     {
  215.         if ($this->rulesLocked) {
  216.             throw new ContextRulesLockedException();
  217.         }
  218.         $this->rulesIds array_filter(array_values($ruleIds));
  219.         $this->getContext()->setRuleIds($this->rulesIds);
  220.     }
  221.     public function lockRules(): void
  222.     {
  223.         $this->rulesLocked true;
  224.     }
  225.     public function lockPermissions(): void
  226.     {
  227.         $this->permisionsLocked true;
  228.     }
  229.     public function getToken(): string
  230.     {
  231.         return $this->token;
  232.     }
  233.     public function getTaxState(): string
  234.     {
  235.         return $this->context->getTaxState();
  236.     }
  237.     public function setTaxState(string $taxState): void
  238.     {
  239.         $this->context->setTaxState($taxState);
  240.     }
  241.     public function getTaxCalculationType(): string
  242.     {
  243.         return $this->getSalesChannel()->getTaxCalculationType();
  244.     }
  245.     public function getPermissions(): array
  246.     {
  247.         return $this->permissions;
  248.     }
  249.     public function setPermissions(array $permissions): void
  250.     {
  251.         if ($this->permisionsLocked) {
  252.             throw new ContextPermissionsLockedException();
  253.         }
  254.         $this->permissions array_filter($permissions);
  255.     }
  256.     public function getApiAlias(): string
  257.     {
  258.         return 'sales_channel_context';
  259.     }
  260.     public function hasPermission(string $permission): bool
  261.     {
  262.         return $this->permissions[$permission] ?? false;
  263.     }
  264.     public function getSalesChannelId(): string
  265.     {
  266.         return $this->getSalesChannel()->getId();
  267.     }
  268.     public function addState(string ...$states): void
  269.     {
  270.         $this->context->addState(...$states);
  271.     }
  272.     public function removeState(string $state): void
  273.     {
  274.         $this->context->removeState($state);
  275.     }
  276.     public function hasState(string ...$states): bool
  277.     {
  278.         return $this->context->hasState(...$states);
  279.     }
  280.     public function getStates(): array
  281.     {
  282.         return $this->context->getStates();
  283.     }
  284.     public function getDomainId(): ?string
  285.     {
  286.         return $this->domainId;
  287.     }
  288.     public function setDomainId(?string $domainId): void
  289.     {
  290.         $this->domainId $domainId;
  291.     }
  292.     public function getLanguageIdChain(): array
  293.     {
  294.         return $this->context->getLanguageIdChain();
  295.     }
  296.     public function getLanguageId(): string
  297.     {
  298.         return $this->context->getLanguageId();
  299.     }
  300.     public function getVersionId(): string
  301.     {
  302.         return $this->context->getVersionId();
  303.     }
  304.     public function considerInheritance(): bool
  305.     {
  306.         return $this->context->considerInheritance();
  307.     }
  308.     public function getTotalRounding(): CashRoundingConfig
  309.     {
  310.         return $this->totalRounding;
  311.     }
  312.     public function setTotalRounding(CashRoundingConfig $totalRounding): void
  313.     {
  314.         $this->totalRounding $totalRounding;
  315.     }
  316.     public function getItemRounding(): CashRoundingConfig
  317.     {
  318.         return $this->itemRounding;
  319.     }
  320.     public function setItemRounding(CashRoundingConfig $itemRounding): void
  321.     {
  322.         $this->itemRounding $itemRounding;
  323.     }
  324.     public function getCurrencyId(): string
  325.     {
  326.         return $this->getCurrency()->getId();
  327.     }
  328.     public function ensureLoggedIn(bool $allowGuest true): void
  329.     {
  330.         if ($this->customer === null) {
  331.             throw CartException::customerNotLoggedIn();
  332.         }
  333.         if (!$allowGuest && $this->customer->getGuest()) {
  334.             throw CartException::customerNotLoggedIn();
  335.         }
  336.     }
  337.     public function getCustomerId(): ?string
  338.     {
  339.         return $this->customer $this->customer->getId() : null;
  340.     }
  341. }