vendor/shopware/core/Framework/Context.php line 14

Open in your IDE?
  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\Struct\StateAwareTrait;
  10. use Shopware\Core\Framework\Struct\Struct;
  11. class Context extends Struct
  12. {
  13.     use StateAwareTrait;
  14.     public const SYSTEM_SCOPE 'system';
  15.     public const USER_SCOPE 'user';
  16.     public const CRUD_API_SCOPE 'crud';
  17.     /**
  18.      * @deprecated tag:v6.5.0 - Use `\Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria::STATE_ELASTICSEARCH_AWARE` on Criteria instead
  19.      */
  20.     public const STATE_ELASTICSEARCH_AWARE 'elasticsearchAware';
  21.     public const SKIP_TRIGGER_FLOW 'skipTriggerFlow';
  22.     /**
  23.      * @var non-empty-array<string>
  24.      *
  25.      * @deprecated tag:v6.5.0 prop will be natively typed as `array` in future versions
  26.      */
  27.     protected $languageIdChain;
  28.     /**
  29.      * @var string
  30.      *
  31.      * @deprecated tag:v6.5.0 prop will be natively typed as `string` in future versions
  32.      */
  33.     protected $versionId;
  34.     /**
  35.      * @var string
  36.      *
  37.      * @deprecated tag:v6.5.0 prop will be natively typed as `string` in future versions
  38.      */
  39.     protected $currencyId;
  40.     /**
  41.      * @var float
  42.      *
  43.      * @deprecated tag:v6.5.0 prop will be natively typed as `float` in future versions
  44.      */
  45.     protected $currencyFactor;
  46.     /**
  47.      * @var string
  48.      *
  49.      * @deprecated tag:v6.5.0 prop will be natively typed as `string` in future versions
  50.      */
  51.     protected $scope self::USER_SCOPE;
  52.     /**
  53.      * @var array<string>
  54.      *
  55.      * @deprecated tag:v6.5.0 prop will be natively typed as `array` in future versions
  56.      */
  57.     protected $ruleIds;
  58.     /**
  59.      * @var ContextSource
  60.      *
  61.      * @deprecated tag:v6.5.0 prop will be natively typed as `ContextSource` in future versions
  62.      */
  63.     protected $source;
  64.     /**
  65.      * @var bool
  66.      *
  67.      * @deprecated tag:v6.5.0 prop will be natively typed as `bool` in future versions
  68.      */
  69.     protected $considerInheritance;
  70.     /**
  71.      * @see CartPrice::TAX_STATE_GROSS, CartPrice::TAX_STATE_NET, CartPrice::TAX_STATE_FREE
  72.      *
  73.      * @var string
  74.      *
  75.      * @deprecated tag:v6.5.0 prop will be natively typed as `string` in future versions
  76.      */
  77.     protected $taxState CartPrice::TAX_STATE_GROSS;
  78.     /**
  79.      * @var CashRoundingConfig
  80.      *
  81.      * @deprecated tag:v6.5.0 prop will be natively typed as `CashRoundingConfig` in future versions
  82.      */
  83.     protected $rounding;
  84.     /**
  85.      * @param array<string> $languageIdChain
  86.      * @param array<string> $ruleIds
  87.      */
  88.     public function __construct(
  89.         ContextSource $source,
  90.         array $ruleIds = [],
  91.         string $currencyId Defaults::CURRENCY,
  92.         array $languageIdChain = [Defaults::LANGUAGE_SYSTEM],
  93.         string $versionId Defaults::LIVE_VERSION,
  94.         float $currencyFactor 1.0,
  95.         bool $considerInheritance false,
  96.         string $taxState CartPrice::TAX_STATE_GROSS,
  97.         ?CashRoundingConfig $rounding null
  98.     ) {
  99.         $this->source $source;
  100.         if ($source instanceof SystemSource) {
  101.             $this->scope self::SYSTEM_SCOPE;
  102.         }
  103.         $this->ruleIds $ruleIds;
  104.         $this->currencyId $currencyId;
  105.         $this->versionId $versionId;
  106.         $this->currencyFactor $currencyFactor;
  107.         if (empty($languageIdChain)) {
  108.             throw new \InvalidArgumentException('Argument languageIdChain must not be empty');
  109.         }
  110.         /** @var non-empty-array<string> $chain */
  111.         $chain array_keys(array_flip(array_filter($languageIdChain)));
  112.         $this->languageIdChain $chain;
  113.         $this->considerInheritance $considerInheritance;
  114.         $this->taxState $taxState;
  115.         $this->rounding $rounding ?? new CashRoundingConfig(20.01true);
  116.     }
  117.     /**
  118.      * @internal
  119.      */
  120.     public static function createDefaultContext(?ContextSource $source null): self
  121.     {
  122.         $source $source ?? new SystemSource();
  123.         return new self($source);
  124.     }
  125.     public function getSource(): ContextSource
  126.     {
  127.         return $this->source;
  128.     }
  129.     public function getVersionId(): string
  130.     {
  131.         return $this->versionId;
  132.     }
  133.     public function getLanguageId(): string
  134.     {
  135.         return $this->languageIdChain[0];
  136.     }
  137.     public function getCurrencyId(): string
  138.     {
  139.         return $this->currencyId;
  140.     }
  141.     public function getCurrencyFactor(): float
  142.     {
  143.         return $this->currencyFactor;
  144.     }
  145.     /**
  146.      * @return array<string>
  147.      */
  148.     public function getRuleIds(): array
  149.     {
  150.         return $this->ruleIds;
  151.     }
  152.     /**
  153.      * @return non-empty-array<string>
  154.      */
  155.     public function getLanguageIdChain(): array
  156.     {
  157.         return $this->languageIdChain;
  158.     }
  159.     public function createWithVersionId(string $versionId): self
  160.     {
  161.         $context = new self(
  162.             $this->source,
  163.             $this->ruleIds,
  164.             $this->currencyId,
  165.             $this->languageIdChain,
  166.             $versionId,
  167.             $this->currencyFactor,
  168.             $this->considerInheritance,
  169.             $this->taxState,
  170.             $this->rounding
  171.         );
  172.         $context->scope $this->scope;
  173.         foreach ($this->getExtensions() as $key => $extension) {
  174.             $context->addExtension($key$extension);
  175.         }
  176.         return $context;
  177.     }
  178.     /**
  179.      * @return mixed the return value of the provided callback function
  180.      */
  181.     public function scope(string $scope, callable $callback)
  182.     {
  183.         $currentScope $this->getScope();
  184.         $this->scope $scope;
  185.         try {
  186.             $result $callback($this);
  187.         } finally {
  188.             $this->scope $currentScope;
  189.         }
  190.         return $result;
  191.     }
  192.     public function getScope(): string
  193.     {
  194.         return $this->scope;
  195.     }
  196.     public function considerInheritance(): bool
  197.     {
  198.         return $this->considerInheritance;
  199.     }
  200.     public function setConsiderInheritance(bool $considerInheritance): void
  201.     {
  202.         $this->considerInheritance $considerInheritance;
  203.     }
  204.     public function getTaxState(): string
  205.     {
  206.         return $this->taxState;
  207.     }
  208.     public function setTaxState(string $taxState): void
  209.     {
  210.         $this->taxState $taxState;
  211.     }
  212.     public function isAllowed(string $privilege): bool
  213.     {
  214.         if ($this->source instanceof AdminApiSource) {
  215.             return $this->source->isAllowed($privilege);
  216.         }
  217.         return true;
  218.     }
  219.     /**
  220.      * @param array<string> $ruleIds
  221.      */
  222.     public function setRuleIds(array $ruleIds): void
  223.     {
  224.         $this->ruleIds array_filter(array_values($ruleIds));
  225.     }
  226.     /**
  227.      * @return mixed
  228.      */
  229.     public function enableInheritance(callable $function)
  230.     {
  231.         $previous $this->considerInheritance;
  232.         $this->considerInheritance true;
  233.         $result $function($this);
  234.         $this->considerInheritance $previous;
  235.         return $result;
  236.     }
  237.     /**
  238.      * @return mixed
  239.      */
  240.     public function disableInheritance(callable $function)
  241.     {
  242.         $previous $this->considerInheritance;
  243.         $this->considerInheritance false;
  244.         $result $function($this);
  245.         $this->considerInheritance $previous;
  246.         return $result;
  247.     }
  248.     public function getApiAlias(): string
  249.     {
  250.         return 'context';
  251.     }
  252.     public function getRounding(): CashRoundingConfig
  253.     {
  254.         return $this->rounding;
  255.     }
  256.     public function setRounding(CashRoundingConfig $rounding): void
  257.     {
  258.         $this->rounding $rounding;
  259.     }
  260. }