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

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\Struct\Struct;
  12. use Shopware\Core\System\Currency\CurrencyEntity;
  13. use Shopware\Core\System\SalesChannel\Exception\ContextPermissionsLockedException;
  14. use Shopware\Core\System\SalesChannel\Exception\ContextRulesLockedException;
  15. use Shopware\Core\System\Tax\Exception\TaxNotFoundException;
  16. use Shopware\Core\System\Tax\TaxCollection;
  17. class SalesChannelContext extends Struct
  18. {
  19.     /**
  20.      * Unique token for context, e.g. stored in session or provided in request headers
  21.      *
  22.      * @var string
  23.      */
  24.     protected $token;
  25.     /**
  26.      * @var CustomerGroupEntity
  27.      */
  28.     protected $currentCustomerGroup;
  29.     /**
  30.      * @var CustomerGroupEntity
  31.      */
  32.     protected $fallbackCustomerGroup;
  33.     /**
  34.      * @var CurrencyEntity
  35.      */
  36.     protected $currency;
  37.     /**
  38.      * @var SalesChannelEntity
  39.      */
  40.     protected $salesChannel;
  41.     /**
  42.      * @var TaxCollection
  43.      */
  44.     protected $taxRules;
  45.     /**
  46.      * @var CustomerEntity|null
  47.      */
  48.     protected $customer;
  49.     /**
  50.      * @var PaymentMethodEntity
  51.      */
  52.     protected $paymentMethod;
  53.     /**
  54.      * @var ShippingMethodEntity
  55.      */
  56.     protected $shippingMethod;
  57.     /**
  58.      * @var ShippingLocation
  59.      */
  60.     protected $shippingLocation;
  61.     /**
  62.      * @var array
  63.      */
  64.     protected $rulesIds;
  65.     /**
  66.      * @var bool
  67.      */
  68.     protected $rulesLocked false;
  69.     /**
  70.      * @var array
  71.      */
  72.     protected $permissions = [];
  73.     /**
  74.      * @var bool
  75.      */
  76.     protected $permisionsLocked false;
  77.     /**
  78.      * @var Context
  79.      */
  80.     private $context;
  81.     public function __construct(
  82.         Context $baseContext,
  83.         string $token,
  84.         SalesChannelEntity $salesChannel,
  85.         CurrencyEntity $currency,
  86.         CustomerGroupEntity $currentCustomerGroup,
  87.         CustomerGroupEntity $fallbackCustomerGroup,
  88.         TaxCollection $taxRules,
  89.         PaymentMethodEntity $paymentMethod,
  90.         ShippingMethodEntity $shippingMethod,
  91.         ShippingLocation $shippingLocation,
  92.         ?CustomerEntity $customer,
  93.         array $rulesIds = []
  94.     ) {
  95.         $this->currentCustomerGroup $currentCustomerGroup;
  96.         $this->fallbackCustomerGroup $fallbackCustomerGroup;
  97.         $this->currency $currency;
  98.         $this->salesChannel $salesChannel;
  99.         $this->taxRules $taxRules;
  100.         $this->customer $customer;
  101.         $this->paymentMethod $paymentMethod;
  102.         $this->shippingMethod $shippingMethod;
  103.         $this->shippingLocation $shippingLocation;
  104.         $this->rulesIds $rulesIds;
  105.         $this->token $token;
  106.         $this->context $baseContext;
  107.     }
  108.     public function getCurrentCustomerGroup(): CustomerGroupEntity
  109.     {
  110.         return $this->currentCustomerGroup;
  111.     }
  112.     public function getFallbackCustomerGroup(): CustomerGroupEntity
  113.     {
  114.         return $this->fallbackCustomerGroup;
  115.     }
  116.     public function getCurrency(): CurrencyEntity
  117.     {
  118.         return $this->currency;
  119.     }
  120.     public function getSalesChannel(): SalesChannelEntity
  121.     {
  122.         return $this->salesChannel;
  123.     }
  124.     public function getTaxRules(): TaxCollection
  125.     {
  126.         return $this->taxRules;
  127.     }
  128.     /**
  129.      * Get the tax rules depend on the customer billing address
  130.      * respectively the shippingLocation if there is no customer
  131.      */
  132.     public function buildTaxRules(string $taxId): TaxRuleCollection
  133.     {
  134.         $tax $this->taxRules->get($taxId);
  135.         if ($tax === null || $tax->getRules() === null) {
  136.             throw new TaxNotFoundException($taxId);
  137.         }
  138.         if ($tax->getRules()->first() !== null) {
  139.             return new TaxRuleCollection([
  140.                 new TaxRule($tax->getRules()->first()->getTaxRate(), 100),
  141.             ]);
  142.         }
  143.         return new TaxRuleCollection([
  144.             new TaxRule($tax->getTaxRate(), 100),
  145.         ]);
  146.     }
  147.     public function getCustomer(): ?CustomerEntity
  148.     {
  149.         return $this->customer;
  150.     }
  151.     public function getPaymentMethod(): PaymentMethodEntity
  152.     {
  153.         return $this->paymentMethod;
  154.     }
  155.     public function getShippingMethod(): ShippingMethodEntity
  156.     {
  157.         return $this->shippingMethod;
  158.     }
  159.     public function getShippingLocation(): ShippingLocation
  160.     {
  161.         return $this->shippingLocation;
  162.     }
  163.     public function getContext(): Context
  164.     {
  165.         return $this->context;
  166.     }
  167.     public function getRuleIds(): array
  168.     {
  169.         return $this->rulesIds;
  170.     }
  171.     public function setRuleIds(array $ruleIds): void
  172.     {
  173.         if ($this->rulesLocked) {
  174.             throw new ContextRulesLockedException();
  175.         }
  176.         $this->rulesIds array_filter(array_values($ruleIds));
  177.         $this->getContext()->setRuleIds($this->rulesIds);
  178.     }
  179.     public function lockRules(): void
  180.     {
  181.         $this->rulesLocked true;
  182.     }
  183.     public function lockPermissions(): void
  184.     {
  185.         $this->permisionsLocked true;
  186.     }
  187.     public function getToken(): string
  188.     {
  189.         return $this->token;
  190.     }
  191.     public function getTaxState(): string
  192.     {
  193.         return $this->context->getTaxState();
  194.     }
  195.     public function setTaxState(string $taxState): void
  196.     {
  197.         $this->context->setTaxState($taxState);
  198.     }
  199.     public function getPermissions(): array
  200.     {
  201.         return $this->permissions;
  202.     }
  203.     public function setPermissions(array $permissions): void
  204.     {
  205.         if ($this->permisionsLocked) {
  206.             throw new ContextPermissionsLockedException();
  207.         }
  208.         $this->permissions array_filter($permissions);
  209.     }
  210.     public function hasPermission(string $permission): bool
  211.     {
  212.         return $this->permissions[$permission] ?? false;
  213.     }
  214.     public function getApiAlias(): string
  215.     {
  216.         return 'sales_channel_context';
  217.     }
  218. }