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