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