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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\SalesChannel;
  3. use Shopware\Core\Checkout\Cart\Delivery\Struct\ShippingLocation;
  4. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRule;
  5. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
  6. use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupEntity;
  7. use Shopware\Core\Checkout\Customer\CustomerEntity;
  8. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  9. use Shopware\Core\Checkout\Shipping\ShippingMethodEntity;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Pricing\CashRoundingConfig;
  12. use Shopware\Core\Framework\Struct\StateAwareTrait;
  13. use Shopware\Core\Framework\Struct\Struct;
  14. use Shopware\Core\System\Currency\CurrencyEntity;
  15. use Shopware\Core\System\SalesChannel\Exception\ContextPermissionsLockedException;
  16. use Shopware\Core\System\SalesChannel\Exception\ContextRulesLockedException;
  17. use Shopware\Core\System\Tax\Exception\TaxNotFoundException;
  18. use Shopware\Core\System\Tax\TaxCollection;
  19. class SalesChannelContext extends Struct
  20. {
  21.     use StateAwareTrait;
  22.     /**
  23.      * Unique token for context, e.g. stored in session or provided in request headers
  24.      *
  25.      * @var string
  26.      */
  27.     protected $token;
  28.     /**
  29.      * @var CustomerGroupEntity
  30.      */
  31.     protected $currentCustomerGroup;
  32.     /**
  33.      * @var CustomerGroupEntity
  34.      */
  35.     protected $fallbackCustomerGroup;
  36.     /**
  37.      * @var CurrencyEntity
  38.      */
  39.     protected $currency;
  40.     /**
  41.      * @var SalesChannelEntity
  42.      */
  43.     protected $salesChannel;
  44.     /**
  45.      * @var TaxCollection
  46.      */
  47.     protected $taxRules;
  48.     /**
  49.      * @var CustomerEntity|null
  50.      */
  51.     protected $customer;
  52.     /**
  53.      * @var PaymentMethodEntity
  54.      */
  55.     protected $paymentMethod;
  56.     /**
  57.      * @var ShippingMethodEntity
  58.      */
  59.     protected $shippingMethod;
  60.     /**
  61.      * @var ShippingLocation
  62.      */
  63.     protected $shippingLocation;
  64.     /**
  65.      * @var array
  66.      */
  67.     protected $rulesIds;
  68.     /**
  69.      * @var bool
  70.      */
  71.     protected $rulesLocked false;
  72.     /**
  73.      * @var array
  74.      */
  75.     protected $permissions = [];
  76.     /**
  77.      * @var bool
  78.      */
  79.     protected $permisionsLocked false;
  80.     /**
  81.      * @var Context
  82.      */
  83.     protected $context;
  84.     /**
  85.      * @var CashRoundingConfig
  86.      */
  87.     private $itemRounding;
  88.     /**
  89.      * @var CashRoundingConfig
  90.      */
  91.     private $totalRounding;
  92.     /**
  93.      * @var string|null
  94.      */
  95.     private $domainId;
  96.     /**
  97.      * @deprecated tag:v6.5.0 - Parameter $fallbackCustomerGroup is deprecated and will be removed
  98.      */
  99.     public function __construct(
  100.         Context $baseContext,
  101.         string $token,
  102.         ?string $domainId,
  103.         SalesChannelEntity $salesChannel,
  104.         CurrencyEntity $currency,
  105.         CustomerGroupEntity $currentCustomerGroup,
  106.         CustomerGroupEntity $fallbackCustomerGroup,
  107.         TaxCollection $taxRules,
  108.         PaymentMethodEntity $paymentMethod,
  109.         ShippingMethodEntity $shippingMethod,
  110.         ShippingLocation $shippingLocation,
  111.         ?CustomerEntity $customer,
  112.         CashRoundingConfig $itemRounding,
  113.         CashRoundingConfig $totalRounding,
  114.         array $rulesIds = []
  115.     ) {
  116.         $this->currentCustomerGroup $currentCustomerGroup;
  117.         $this->fallbackCustomerGroup $fallbackCustomerGroup;
  118.         $this->currency $currency;
  119.         $this->salesChannel $salesChannel;
  120.         $this->taxRules $taxRules;
  121.         $this->customer $customer;
  122.         $this->paymentMethod $paymentMethod;
  123.         $this->shippingMethod $shippingMethod;
  124.         $this->shippingLocation $shippingLocation;
  125.         $this->rulesIds $rulesIds;
  126.         $this->token $token;
  127.         $this->context $baseContext;
  128.         $this->itemRounding $itemRounding;
  129.         $this->totalRounding $totalRounding;
  130.         $this->domainId $domainId;
  131.     }
  132.     public function getCurrentCustomerGroup(): CustomerGroupEntity
  133.     {
  134.         return $this->currentCustomerGroup;
  135.     }
  136.     /**
  137.      * @deprecated tag:v6.5.0 - Fallback customer group is deprecated and will be removed, use getCurrentCustomerGroup instead
  138.      */
  139.     public function getFallbackCustomerGroup(): CustomerGroupEntity
  140.     {
  141.         return $this->fallbackCustomerGroup;
  142.     }
  143.     public function getCurrency(): CurrencyEntity
  144.     {
  145.         return $this->currency;
  146.     }
  147.     public function getSalesChannel(): SalesChannelEntity
  148.     {
  149.         return $this->salesChannel;
  150.     }
  151.     public function getTaxRules(): TaxCollection
  152.     {
  153.         return $this->taxRules;
  154.     }
  155.     /**
  156.      * Get the tax rules depend on the customer billing address
  157.      * respectively the shippingLocation if there is no customer
  158.      */
  159.     public function buildTaxRules(string $taxId): TaxRuleCollection
  160.     {
  161.         $tax $this->taxRules->get($taxId);
  162.         if ($tax === null || $tax->getRules() === null) {
  163.             throw new TaxNotFoundException($taxId);
  164.         }
  165.         if ($tax->getRules()->first() !== null) {
  166.             return new TaxRuleCollection([
  167.                 new TaxRule($tax->getRules()->first()->getTaxRate(), 100),
  168.             ]);
  169.         }
  170.         return new TaxRuleCollection([
  171.             new TaxRule($tax->getTaxRate(), 100),
  172.         ]);
  173.     }
  174.     public function getCustomer(): ?CustomerEntity
  175.     {
  176.         return $this->customer;
  177.     }
  178.     public function getPaymentMethod(): PaymentMethodEntity
  179.     {
  180.         return $this->paymentMethod;
  181.     }
  182.     public function getShippingMethod(): ShippingMethodEntity
  183.     {
  184.         return $this->shippingMethod;
  185.     }
  186.     public function getShippingLocation(): ShippingLocation
  187.     {
  188.         return $this->shippingLocation;
  189.     }
  190.     public function getContext(): Context
  191.     {
  192.         return $this->context;
  193.     }
  194.     public function getRuleIds(): array
  195.     {
  196.         return $this->rulesIds;
  197.     }
  198.     public function setRuleIds(array $ruleIds): void
  199.     {
  200.         if ($this->rulesLocked) {
  201.             throw new ContextRulesLockedException();
  202.         }
  203.         $this->rulesIds array_filter(array_values($ruleIds));
  204.         $this->getContext()->setRuleIds($this->rulesIds);
  205.     }
  206.     public function lockRules(): void
  207.     {
  208.         $this->rulesLocked true;
  209.     }
  210.     public function lockPermissions(): void
  211.     {
  212.         $this->permisionsLocked true;
  213.     }
  214.     public function getToken(): string
  215.     {
  216.         return $this->token;
  217.     }
  218.     public function getTaxState(): string
  219.     {
  220.         return $this->context->getTaxState();
  221.     }
  222.     public function setTaxState(string $taxState): void
  223.     {
  224.         $this->context->setTaxState($taxState);
  225.     }
  226.     public function getTaxCalculationType(): string
  227.     {
  228.         return $this->getSalesChannel()->getTaxCalculationType();
  229.     }
  230.     public function getPermissions(): array
  231.     {
  232.         return $this->permissions;
  233.     }
  234.     public function setPermissions(array $permissions): void
  235.     {
  236.         if ($this->permisionsLocked) {
  237.             throw new ContextPermissionsLockedException();
  238.         }
  239.         $this->permissions array_filter($permissions);
  240.     }
  241.     public function getApiAlias(): string
  242.     {
  243.         return 'sales_channel_context';
  244.     }
  245.     public function hasPermission(string $permission): bool
  246.     {
  247.         return $this->permissions[$permission] ?? false;
  248.     }
  249.     public function getSalesChannelId(): string
  250.     {
  251.         return $this->getSalesChannel()->getId();
  252.     }
  253.     public function addState(string ...$states): void
  254.     {
  255.         $this->context->addState(...$states);
  256.     }
  257.     public function removeState(string $state): void
  258.     {
  259.         $this->context->removeState($state);
  260.     }
  261.     public function hasState(string ...$states): bool
  262.     {
  263.         return $this->context->hasState(...$states);
  264.     }
  265.     public function getStates(): array
  266.     {
  267.         return $this->context->getStates();
  268.     }
  269.     public function getDomainId(): ?string
  270.     {
  271.         return $this->domainId;
  272.     }
  273.     public function getLanguageIdChain(): array
  274.     {
  275.         return $this->context->getLanguageIdChain();
  276.     }
  277.     public function getLanguageId(): string
  278.     {
  279.         return $this->context->getLanguageId();
  280.     }
  281.     public function getVersionId(): string
  282.     {
  283.         return $this->context->getVersionId();
  284.     }
  285.     public function considerInheritance(): bool
  286.     {
  287.         return $this->context->considerInheritance();
  288.     }
  289.     public function getTotalRounding(): CashRoundingConfig
  290.     {
  291.         return $this->totalRounding;
  292.     }
  293.     public function setTotalRounding(CashRoundingConfig $totalRounding): void
  294.     {
  295.         $this->totalRounding $totalRounding;
  296.     }
  297.     public function getItemRounding(): CashRoundingConfig
  298.     {
  299.         return $this->itemRounding;
  300.     }
  301.     public function setItemRounding(CashRoundingConfig $itemRounding): void
  302.     {
  303.         $this->itemRounding $itemRounding;
  304.     }
  305.     public function getCurrencyId(): string
  306.     {
  307.         return $this->getCurrency()->getId();
  308.     }
  309. }