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