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