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.             return new TaxRuleCollection([
  174.                 new TaxRule($tax->getRules()->first()->getTaxRate(), 100),
  175.             ]);
  176.         }
  177.         return new TaxRuleCollection([
  178.             new TaxRule($tax->getTaxRate(), 100),
  179.         ]);
  180.     }
  181.     public function getCustomer(): ?CustomerEntity
  182.     {
  183.         return $this->customer;
  184.     }
  185.     public function getPaymentMethod(): PaymentMethodEntity
  186.     {
  187.         return $this->paymentMethod;
  188.     }
  189.     public function getShippingMethod(): ShippingMethodEntity
  190.     {
  191.         return $this->shippingMethod;
  192.     }
  193.     public function getShippingLocation(): ShippingLocation
  194.     {
  195.         return $this->shippingLocation;
  196.     }
  197.     public function getContext(): Context
  198.     {
  199.         return $this->context;
  200.     }
  201.     public function getRuleIds(): array
  202.     {
  203.         return $this->rulesIds;
  204.     }
  205.     public function setRuleIds(array $ruleIds): void
  206.     {
  207.         if ($this->rulesLocked) {
  208.             throw new ContextRulesLockedException();
  209.         }
  210.         $this->rulesIds array_filter(array_values($ruleIds));
  211.         $this->getContext()->setRuleIds($this->rulesIds);
  212.     }
  213.     public function lockRules(): void
  214.     {
  215.         $this->rulesLocked true;
  216.     }
  217.     public function lockPermissions(): void
  218.     {
  219.         $this->permisionsLocked true;
  220.     }
  221.     public function getToken(): string
  222.     {
  223.         return $this->token;
  224.     }
  225.     public function getTaxState(): string
  226.     {
  227.         return $this->context->getTaxState();
  228.     }
  229.     public function setTaxState(string $taxState): void
  230.     {
  231.         $this->context->setTaxState($taxState);
  232.     }
  233.     public function getTaxCalculationType(): string
  234.     {
  235.         return $this->getSalesChannel()->getTaxCalculationType();
  236.     }
  237.     public function getPermissions(): array
  238.     {
  239.         return $this->permissions;
  240.     }
  241.     public function setPermissions(array $permissions): void
  242.     {
  243.         if ($this->permisionsLocked) {
  244.             throw new ContextPermissionsLockedException();
  245.         }
  246.         $this->permissions array_filter($permissions);
  247.     }
  248.     public function getApiAlias(): string
  249.     {
  250.         return 'sales_channel_context';
  251.     }
  252.     public function hasPermission(string $permission): bool
  253.     {
  254.         return $this->permissions[$permission] ?? false;
  255.     }
  256.     public function getSalesChannelId(): string
  257.     {
  258.         return $this->getSalesChannel()->getId();
  259.     }
  260.     public function addState(string ...$states): void
  261.     {
  262.         $this->context->addState(...$states);
  263.     }
  264.     public function removeState(string $state): void
  265.     {
  266.         $this->context->removeState($state);
  267.     }
  268.     public function hasState(string ...$states): bool
  269.     {
  270.         return $this->context->hasState(...$states);
  271.     }
  272.     public function getStates(): array
  273.     {
  274.         return $this->context->getStates();
  275.     }
  276.     public function getDomainId(): ?string
  277.     {
  278.         return $this->domainId;
  279.     }
  280.     public function setDomainId(?string $domainId): void
  281.     {
  282.         $this->domainId $domainId;
  283.     }
  284.     public function getLanguageIdChain(): array
  285.     {
  286.         return $this->context->getLanguageIdChain();
  287.     }
  288.     public function getLanguageId(): string
  289.     {
  290.         return $this->context->getLanguageId();
  291.     }
  292.     public function getVersionId(): string
  293.     {
  294.         return $this->context->getVersionId();
  295.     }
  296.     public function considerInheritance(): bool
  297.     {
  298.         return $this->context->considerInheritance();
  299.     }
  300.     public function getTotalRounding(): CashRoundingConfig
  301.     {
  302.         return $this->totalRounding;
  303.     }
  304.     public function setTotalRounding(CashRoundingConfig $totalRounding): void
  305.     {
  306.         $this->totalRounding $totalRounding;
  307.     }
  308.     public function getItemRounding(): CashRoundingConfig
  309.     {
  310.         return $this->itemRounding;
  311.     }
  312.     public function setItemRounding(CashRoundingConfig $itemRounding): void
  313.     {
  314.         $this->itemRounding $itemRounding;
  315.     }
  316.     public function getCurrencyId(): string
  317.     {
  318.         return $this->getCurrency()->getId();
  319.     }
  320.     public function ensureLoggedIn(bool $allowGuest true): void
  321.     {
  322.         if ($this->customer === null) {
  323.             throw new CustomerNotLoggedInException();
  324.         }
  325.         if (!$allowGuest && $this->customer->getGuest()) {
  326.             throw new CustomerNotLoggedInException();
  327.         }
  328.     }
  329.     public function getCustomerId(): ?string
  330.     {
  331.         return $this->customer $this->customer->getId() : null;
  332.     }
  333. }