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

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