custom/plugins/SwagPlatformSecurity/src/Fixes/NEXT37399/Context.php line 18

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. use Shopware\Core\System\SalesChannel\Exception\ContextRulesLockedException;
  13. use Symfony\Component\Serializer\Annotation\Ignore;
  14. #[Package('core')]
  15. class Context extends Struct
  16. {
  17.     const SYSTEM_SCOPE 'system';
  18.     const USER_SCOPE 'user';
  19.     const CRUD_API_SCOPE 'crud';
  20.     /**
  21.      * @deprecated tag:v6.5.0 - Use `\Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria::STATE_ELASTICSEARCH_AWARE` on Criteria instead
  22.      */
  23.     const STATE_ELASTICSEARCH_AWARE 'elasticsearchAware';
  24.     const SKIP_TRIGGER_FLOW 'skipTriggerFlow';
  25.     /**
  26.      * @var non-empty-array<string>
  27.      */
  28.     protected $languageIdChain;
  29.     protected $scope self::USER_SCOPE;
  30.     protected $rulesLocked false;
  31.     /**
  32.      * @var array<string>
  33.      */
  34.     #[Ignore]
  35.     protected $extensions = [];
  36.     /**
  37.      * @var ContextSource
  38.      */
  39.     protected $source;
  40.     /**
  41.      * @var non-empty-array<string>
  42.      */
  43.     protected $ruleIds = [];
  44.     /**
  45.      * @var string
  46.      */
  47.     protected $currencyId Defaults::CURRENCY;
  48.     /**
  49.      * @var string
  50.      */
  51.     protected $versionId Defaults::LIVE_VERSION;
  52.     /**
  53.      * @var float
  54.      */
  55.     protected $currencyFactor 1.0;
  56.     /**
  57.      * @var bool
  58.      */
  59.     protected $considerInheritance false;
  60.     /**
  61.      * @var string
  62.      */
  63.     protected $taxState CartPrice::TAX_STATE_GROSS;
  64.     /**
  65.      * @var CashRoundingConfig
  66.      */
  67.     protected $rounding;
  68.     /**
  69.      * @var array<string>
  70.      */
  71.     private $states = [];
  72.     /**
  73.      * @param array<string> $languageIdChain
  74.      * @param array<string> $ruleIds
  75.      */
  76.     public function __construct(
  77.         ContextSource $source,
  78.         array $ruleIds = [],
  79.         string $currencyId Defaults::CURRENCY,
  80.         array $languageIdChain = [Defaults::LANGUAGE_SYSTEM],
  81.         string $versionId Defaults::LIVE_VERSION,
  82.         float $currencyFactor 1.0,
  83.         bool $considerInheritance false,
  84.         /**
  85.          * @see CartPrice::TAX_STATE_GROSS, CartPrice::TAX_STATE_NET, CartPrice::TAX_STATE_FREE
  86.          */
  87.         string $taxState CartPrice::TAX_STATE_GROSS,
  88.         CashRoundingConfig $rounding null
  89.     ) {
  90.         $this->rounding $rounding ?? new CashRoundingConfig(20.01true);
  91.         $this->taxState $taxState;
  92.         $this->considerInheritance $considerInheritance;
  93.         $this->currencyFactor $currencyFactor;
  94.         $this->versionId $versionId;
  95.         $this->currencyId $currencyId;
  96.         $this->ruleIds $ruleIds;
  97.         $this->source $source;
  98.         if ($source instanceof SystemSource) {
  99.             $this->scope self::SYSTEM_SCOPE;
  100.         }
  101.         if (empty($languageIdChain)) {
  102.             throw new \InvalidArgumentException('Argument languageIdChain must not be empty');
  103.         }
  104.         /** @var non-empty-array<string> $chain */
  105.         $chain array_keys(array_flip(array_filter($languageIdChain)));
  106.         $this->languageIdChain $chain;
  107.     }
  108.     /**
  109.      * @internal
  110.      */
  111.     public static function createDefaultContext(ContextSource $source null): self
  112.     {
  113.         return new self($source ?? new SystemSource());
  114.     }
  115.     public static function createCLIContext(ContextSource $source null): self
  116.     {
  117.         return self::createDefaultContext($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.     /**
  140.      * @return array<string>
  141.      */
  142.     public function getRuleIds(): array
  143.     {
  144.         return $this->ruleIds;
  145.     }
  146.     /**
  147.      * @return non-empty-array<string>
  148.      */
  149.     public function getLanguageIdChain(): array
  150.     {
  151.         return $this->languageIdChain;
  152.     }
  153.     public function createWithVersionId(string $versionId): self
  154.     {
  155.         $context = new self(
  156.             $this->source,
  157.             $this->ruleIds,
  158.             $this->currencyId,
  159.             $this->languageIdChain,
  160.             $versionId,
  161.             $this->currencyFactor,
  162.             $this->considerInheritance,
  163.             $this->taxState,
  164.             $this->rounding
  165.         );
  166.         $context->scope $this->scope;
  167.         foreach ($this->getExtensions() as $key => $extension) {
  168.             $context->addExtension($key$extension);
  169.         }
  170.         return $context;
  171.     }
  172.     /**
  173.      * @template TReturn of mixed
  174.      *
  175.      * @param \Closure(Context): TReturn $callback
  176.      *
  177.      * @return TReturn the return value of the provided callback function
  178.      */
  179.     public function scope(string $scope, \Closure $callback)
  180.     {
  181.         $currentScope $this->getScope();
  182.         $this->scope $scope;
  183.         try {
  184.             $result $callback($this);
  185.         } finally {
  186.             $this->scope $currentScope;
  187.         }
  188.         return $result;
  189.     }
  190.     public function getScope(): string
  191.     {
  192.         return $this->scope;
  193.     }
  194.     public function considerInheritance(): bool
  195.     {
  196.         return $this->considerInheritance;
  197.     }
  198.     /**
  199.      * @return void
  200.      */
  201.     public function setConsiderInheritance(bool $considerInheritance)
  202.     {
  203.         $this->considerInheritance $considerInheritance;
  204.     }
  205.     public function getTaxState(): string
  206.     {
  207.         return $this->taxState;
  208.     }
  209.     /**
  210.      * @return void
  211.      */
  212.     public function setTaxState(string $taxState)
  213.     {
  214.         $this->taxState $taxState;
  215.     }
  216.     /**
  217.      * @return bool
  218.      */
  219.     public function isAllowed(string $privilege)
  220.     {
  221.         if ($this->source instanceof AdminApiSource) {
  222.             return $this->source->isAllowed($privilege);
  223.         }
  224.         return true;
  225.     }
  226.     /**
  227.      * @param array<string> $ruleIds
  228.      */
  229.     public function setRuleIds(array $ruleIds)
  230.     {
  231.         if ($this->rulesLocked) {
  232.             throw new ContextRulesLockedException();
  233.         }
  234.         $this->ruleIds array_filter(array_values($ruleIds));
  235.     }
  236.     /**
  237.      * @template TReturn of mixed
  238.      *
  239.      * @param \Closure(Context): TReturn $function
  240.      *
  241.      * @return TReturn
  242.      */
  243.     public function enableInheritance(\Closure $function)
  244.     {
  245.         $previous $this->considerInheritance;
  246.         $this->considerInheritance true;
  247.         $result $function($this);
  248.         $this->considerInheritance $previous;
  249.         return $result;
  250.     }
  251.     /**
  252.      * @template TReturn of mixed
  253.      *
  254.      * @param \Closure(Context): TReturn $function
  255.      *
  256.      * @return TReturn
  257.      */
  258.     public function disableInheritance(\Closure $function)
  259.     {
  260.         $previous $this->considerInheritance;
  261.         $this->considerInheritance false;
  262.         $result $function($this);
  263.         $this->considerInheritance $previous;
  264.         return $result;
  265.     }
  266.     public function getApiAlias(): string
  267.     {
  268.         return 'context';
  269.     }
  270.     public function getRounding(): CashRoundingConfig
  271.     {
  272.         return $this->rounding;
  273.     }
  274.     /**
  275.      * @return void
  276.      */
  277.     public function setRounding(CashRoundingConfig $rounding)
  278.     {
  279.         $this->rounding $rounding;
  280.     }
  281.     /**
  282.      * @return void
  283.      */
  284.     public function lockRules()
  285.     {
  286.         $this->rulesLocked true;
  287.     }
  288.     /**
  289.      * @return void
  290.      */
  291.     public function addState(string ...$states)
  292.     {
  293.         foreach ($states as $state) {
  294.             $this->states[$state] = $state;
  295.         }
  296.     }
  297.     /**
  298.      * @return void
  299.      */
  300.     public function removeState(string $state)
  301.     {
  302.         unset($this->states[$state]);
  303.     }
  304.     public function hasState(string ...$states): bool
  305.     {
  306.         foreach ($states as $state) {
  307.             if (isset($this->states[$state])) {
  308.                 return true;
  309.             }
  310.         }
  311.         return false;
  312.     }
  313.     /**
  314.      * @return array<string>
  315.      */
  316.     public function getStates(): array
  317.     {
  318.         return array_keys($this->states);
  319.     }
  320. }