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