src/Core/Framework/Context.php line 17

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework;
  3. use Shopware\Core\Checkout\Cart\Price\Struct\CartPrice;
  4. use Shopware\Core\Defaults;
  5. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  6. use Shopware\Core\Framework\Api\Context\ContextSource;
  7. use Shopware\Core\Framework\Api\Context\SystemSource;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Pricing\CashRoundingConfig;
  9. use Shopware\Core\Framework\Log\Package;
  10. use Shopware\Core\Framework\Struct\StateAwareTrait;
  11. use Shopware\Core\Framework\Struct\Struct;
  12. use Shopware\Core\System\SalesChannel\Exception\ContextRulesLockedException;
  13. #[Package('core')]
  14. class Context extends Struct
  15. {
  16.     use StateAwareTrait;
  17.     final public const SYSTEM_SCOPE 'system';
  18.     final public const USER_SCOPE 'user';
  19.     final public const CRUD_API_SCOPE 'crud';
  20.     final public const SKIP_TRIGGER_FLOW 'skipTriggerFlow';
  21.     /**
  22.      * @var non-empty-array<string>
  23.      */
  24.     protected array $languageIdChain;
  25.     protected string $scope self::USER_SCOPE;
  26.     protected bool $rulesLocked false;
  27.     /**
  28.      * @param array<string> $languageIdChain
  29.      * @param array<string> $ruleIds
  30.      */
  31.     public function __construct(
  32.         protected ContextSource $source,
  33.         protected array $ruleIds = [],
  34.         protected string $currencyId Defaults::CURRENCY,
  35.         array $languageIdChain = [Defaults::LANGUAGE_SYSTEM],
  36.         protected string $versionId Defaults::LIVE_VERSION,
  37.         protected float $currencyFactor 1.0,
  38.         protected bool $considerInheritance false,
  39.         /**
  40.          * @see CartPrice::TAX_STATE_GROSS, CartPrice::TAX_STATE_NET, CartPrice::TAX_STATE_FREE
  41.          */
  42.         protected string $taxState CartPrice::TAX_STATE_GROSS,
  43.         protected CashRoundingConfig $rounding = new CashRoundingConfig(20.01true)
  44.     ) {
  45.         if ($source instanceof SystemSource) {
  46.             $this->scope self::SYSTEM_SCOPE;
  47.         }
  48.         if (empty($languageIdChain)) {
  49.             throw new \InvalidArgumentException('Argument languageIdChain must not be empty');
  50.         }
  51.         /** @var non-empty-array<string> $chain */
  52.         $chain array_keys(array_flip(array_filter($languageIdChain)));
  53.         $this->languageIdChain $chain;
  54.     }
  55.     /**
  56.      * @internal
  57.      */
  58.     public static function createDefaultContext(?ContextSource $source null): self
  59.     {
  60.         $source ??= new SystemSource();
  61.         return new self($source);
  62.     }
  63.     public function getSource(): ContextSource
  64.     {
  65.         return $this->source;
  66.     }
  67.     public function getVersionId(): string
  68.     {
  69.         return $this->versionId;
  70.     }
  71.     public function getLanguageId(): string
  72.     {
  73.         return $this->languageIdChain[0];
  74.     }
  75.     public function getCurrencyId(): string
  76.     {
  77.         return $this->currencyId;
  78.     }
  79.     public function getCurrencyFactor(): float
  80.     {
  81.         return $this->currencyFactor;
  82.     }
  83.     /**
  84.      * @return array<string>
  85.      */
  86.     public function getRuleIds(): array
  87.     {
  88.         return $this->ruleIds;
  89.     }
  90.     /**
  91.      * @return non-empty-array<string>
  92.      */
  93.     public function getLanguageIdChain(): array
  94.     {
  95.         return $this->languageIdChain;
  96.     }
  97.     public function createWithVersionId(string $versionId): self
  98.     {
  99.         $context = new self(
  100.             $this->source,
  101.             $this->ruleIds,
  102.             $this->currencyId,
  103.             $this->languageIdChain,
  104.             $versionId,
  105.             $this->currencyFactor,
  106.             $this->considerInheritance,
  107.             $this->taxState,
  108.             $this->rounding
  109.         );
  110.         $context->scope $this->scope;
  111.         foreach ($this->getExtensions() as $key => $extension) {
  112.             $context->addExtension($key$extension);
  113.         }
  114.         return $context;
  115.     }
  116.     /**
  117.      * @return mixed the return value of the provided callback function
  118.      */
  119.     public function scope(string $scope, callable $callback)
  120.     {
  121.         $currentScope $this->getScope();
  122.         $this->scope $scope;
  123.         try {
  124.             $result $callback($this);
  125.         } finally {
  126.             $this->scope $currentScope;
  127.         }
  128.         return $result;
  129.     }
  130.     public function getScope(): string
  131.     {
  132.         return $this->scope;
  133.     }
  134.     public function considerInheritance(): bool
  135.     {
  136.         return $this->considerInheritance;
  137.     }
  138.     public function setConsiderInheritance(bool $considerInheritance): void
  139.     {
  140.         $this->considerInheritance $considerInheritance;
  141.     }
  142.     public function getTaxState(): string
  143.     {
  144.         return $this->taxState;
  145.     }
  146.     public function setTaxState(string $taxState): void
  147.     {
  148.         $this->taxState $taxState;
  149.     }
  150.     public function isAllowed(string $privilege): bool
  151.     {
  152.         if ($this->source instanceof AdminApiSource) {
  153.             return $this->source->isAllowed($privilege);
  154.         }
  155.         return true;
  156.     }
  157.     /**
  158.      * @param array<string> $ruleIds
  159.      */
  160.     public function setRuleIds(array $ruleIds): void
  161.     {
  162.         if ($this->rulesLocked) {
  163.             throw new ContextRulesLockedException();
  164.         }
  165.         $this->ruleIds array_filter(array_values($ruleIds));
  166.     }
  167.     /**
  168.      * @return mixed
  169.      */
  170.     public function enableInheritance(callable $function)
  171.     {
  172.         $previous $this->considerInheritance;
  173.         $this->considerInheritance true;
  174.         $result $function($this);
  175.         $this->considerInheritance $previous;
  176.         return $result;
  177.     }
  178.     /**
  179.      * @return mixed
  180.      */
  181.     public function disableInheritance(callable $function)
  182.     {
  183.         $previous $this->considerInheritance;
  184.         $this->considerInheritance false;
  185.         $result $function($this);
  186.         $this->considerInheritance $previous;
  187.         return $result;
  188.     }
  189.     public function getApiAlias(): string
  190.     {
  191.         return 'context';
  192.     }
  193.     public function getRounding(): CashRoundingConfig
  194.     {
  195.         return $this->rounding;
  196.     }
  197.     public function setRounding(CashRoundingConfig $rounding): void
  198.     {
  199.         $this->rounding $rounding;
  200.     }
  201.     public function lockRules(): void
  202.     {
  203.         $this->rulesLocked true;
  204.     }
  205. }