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