getScope example

static::assertImportExportSucceeded($progress$this->getInvalidLogContent($progress->getInvalidRecordsLogId()));

        /** @var PromotionEntity $promotion */
        $promotion = $this->getContainer()->get('promotion.repository')->search((new Criteria([$promotionId]))->addAssociation('discounts.discountRules')$context)->first();

        static::assertInstanceOf(PromotionDiscountCollection::class$discounts = $promotion->getDiscounts());
        static::assertCount(2, $discounts);

        $firstDiscount = $discounts->first();
        static::assertInstanceOf(PromotionDiscountEntity::class$firstDiscount);
        static::assertEquals('cart', $firstDiscount->getScope());
        static::assertEquals('absolute', $firstDiscount->getType());
        static::assertEquals(5, $firstDiscount->getValue());
        static::assertFalse($firstDiscount->isConsiderAdvancedRules());
        static::assertNull($firstDiscount->getMaxValue());
        static::assertEquals('PRICE_ASC', $firstDiscount->getSorterKey());
        static::assertEquals('ALL', $firstDiscount->getApplierKey());
        static::assertEquals('ALL', $firstDiscount->getUsageKey());
        static::assertEmpty($firstDiscount->getPickerKey());
        static::assertInstanceOf(RuleCollection::class$firstDiscountRules = $firstDiscount->getDiscountRules());
        static::assertEmpty($firstDiscountRules->getIds());

        
/** @var PriceDefinitionInterface $priceDefinition */
        $priceDefinition = $item->getPriceDefinition();

        $discount = new DiscountLineItem(
            $label,
            $priceDefinition,
            $item->getPayload(),
            $item->getReferencedId()
        );

        $packager = match ($discount->getScope()) {
            PromotionDiscountEntity::SCOPE_CART => $this->cartScopeDiscountPackager,
            PromotionDiscountEntity::SCOPE_SET => $this->setScopeDiscountPackager,
            PromotionDiscountEntity::SCOPE_SETGROUP => $this->setGroupScopeDiscountPackager,
            default => throw new InvalidScopeDefinitionException($discount->getScope()),
        };

        $packages = $packager->getMatchingItems($discount$calculatedCart$context);

        // check if no result is found,         // then this would mean -> no discount         if ($packages->count() <= 0) {
            
if (!($field instanceof StateMachineStateField)) {
            throw DataAbstractionLayerException::invalidSerializerField(StateMachineStateField::class$field);
        }

        // Always allow any status when creating a new entity. A state transition from one state into another makes no         // sense in that case.         if (!$existence->exists()) {
            return parent::encode($field$existence$data$parameters);
        }

        // Allow the change of the stateMachineState if the scope is one of the allowed ones.         $scope = $parameters->getContext()->getContext()->getScope();
        if (\in_array($scope$field->getAllowedWriteScopes(), true)) {
            return parent::encode($field$existence$data$parameters);
        }

        // In every other case force the user to use a state-transition         $messageTemplate = 'Changing the state-machine-state of this entity is not allowed for scope {{ scope }}. '
            . 'Either change the state-machine-state via a state-transition or use a different scope.';
        $messageParameters = [
            '{{ scope }}' => $scope,
        ];

        
public static function getSubscribedEvents(): array
    {
        return [
            PreWriteValidationEvent::class => 'checkWrite',
        ];
    }

    public function checkWrite(PreWriteValidationEvent $event): void
    {
        $context = $event->getContext();

        if ($context->getSource() instanceof SystemSource || $context->getScope() === Context::SYSTEM_SCOPE) {
            return;
        }

        $integrationId = $this->getIntegrationId($context);
        $violationList = new ConstraintViolationList();

        foreach ($event->getCommands() as $command) {
            if (
                !($command->getDefinition() instanceof CustomFieldSetDefinition)
                || $command instanceof InsertCommand
            ) {
                


        return true;
    }

    public function D__get($name): mixed
    {
        $propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
        $scope = null;

        if ([$class, , $readonlyScope] = $propertyScopes[$name] ?? null) {
            $scope = Registry::getScope($propertyScopes$class$name);
            $state = $this->lazyObjectState ?? null;

            if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))
                && LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $state->initialize($this$name$readonlyScope ?? $scope)
            ) {
                goto get_in_scope;
            }
        }

        if ($parent = (Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['get']) {
            if (2 === $parent) {
                
/** * @internal */
#[Package('core')] class ContextTest extends TestCase
{
    public function testDefaultValues(): void
    {
        $context = Context::createDefaultContext();

        static::assertInstanceOf(SystemSource::class$context->getSource());
        static::assertEquals(Context::SYSTEM_SCOPE, $context->getScope());
        static::assertEquals([]$context->getRuleIds());
        static::assertEquals(Defaults::LIVE_VERSION, $context->getVersionId());
    }

    public function testScope(): void
    {
        $context = Context::createDefaultContext();

        static::assertEquals(Context::SYSTEM_SCOPE, $context->getScope());

        $context->scope('foo', function DContext $context): void {
            


        return true;
    }

    public function D__get($name): mixed
    {
        $propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
        $scope = null;

        if ([$class, , $readonlyScope] = $propertyScopes[$name] ?? null) {
            $scope = Registry::getScope($propertyScopes$class$name);
            $state = $this->lazyObjectState ?? null;

            if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))
                && LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $state->initialize($this$name$readonlyScope ?? $scope)
            ) {
                goto get_in_scope;
            }
        }

        if ($parent = (Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['get']) {
            if (2 === $parent) {
                
static::assertEquals('Black Friday', $this->discount->getLabel());
    }

    /** * This test verifies that the property is correctly * assigned as well as returned in the getter function. * * @group promotions */
    public function testScope(): void
    {
        static::assertEquals('cart', $this->discount->getScope());
    }

    /** * This test verifies that the property is correctly * assigned as well as returned in the getter function. * * @group promotions */
    public function testType(): void
    {
        static::assertEquals('absolute', $this->discount->getType());
    }


    /** * @template TReturn of mixed * * @param callable(Context): TReturn $callback * * @return TReturn the return value of the provided callback function */
    public function scope(string $scope, callable $callback)
    {
        $currentScope = $this->getScope();
        $this->scope = $scope;

        try {
            $result = $callback($this);
        } finally {
            $this->scope = $currentScope;
        }

        return $result;
    }

    

    public function validateEntityPath(array $pathSegments, array $protections, Context $context): void
    {
        foreach ($pathSegments as $pathSegment) {
            /** @var EntityDefinition $definition */
            $definition = $pathSegment['definition'];

            foreach ($protections as $protection) {
                $protectionInstance = $definition->getProtections()->get($protection);
                if (!$protectionInstance || $protectionInstance->isAllowed($context->getScope())) {
                    continue;
                }

                throw new AccessDeniedHttpException(
                    sprintf('API access for entity "%s" not allowed.', $pathSegment['entity'])
                );
            }
        }
    }

    public function validateEntitySearch(EntitySearchedEvent $event): void
    {
$promotionCodeType = 'global';
        }
        $payload['promotionCodeType'] = $promotionCodeType;

        // set our max value for maximum percentage discounts         $payload['maxValue'] = '';
        if ($discount->getType() === PromotionDiscountEntity::TYPE_PERCENTAGE && $discount->getMaxValue() !== null) {
            $payload['maxValue'] = (string) $this->getCurrencySpecificValue($discount$discount->getMaxValue()$currencyId$currencyFactor);
        }

        // set the scope of the discount cart, delivery....         $payload['discountScope'] = $discount->getScope();

        // specifies if the promotion is not combinable with any other promotion         $payload['preventCombination'] = $promotion->isPreventCombination();

        // If all combinations are prevented the exclusions dont matter         // otherwise sets a list of excluded promotion ids         $payload['exclusions'] = $payload['preventCombination'] ? [] : $promotion->getExclusionIds();

        $payload['groupId'] = '';
        // if we have set a custom setgroup scope, then the group id         // is used as suffix in the scopeKey...
$discount->setScope(PromotionDiscountEntity::SCOPE_DELIVERY);

        $item = $builder->buildDiscountLineItem('', $this->promotion, $discount, Defaults::CURRENCY, $currencyFactor);

        static::assertTrue($item->hasPayloadValue('promotionId'), 'We are expecting the promotionId as payload value');
        static::assertTrue($item->hasPayloadValue('discountId'), 'We are expecting the discountId as payload value');
        static::assertTrue($item->hasPayloadValue('discountType'), 'We are expecting the discountType as payload value');
        static::assertTrue($item->hasPayloadValue('discountScope'), 'We are expecting the discount scope as payload value');
        static::assertEquals($this->promotion->getId()$item->getPayloadValue('promotionId'), 'Wrong value in payload key promotionId');
        static::assertEquals($discount->getId()$item->getPayloadValue('discountId'), 'Wrong value in payload key discountId');
        static::assertEquals($discount->getType()$item->getPayloadValue('discountType'), 'Wrong value in payload key discountType');
        static::assertEquals($discount->getScope()$item->getPayloadValue('discountScope'), 'Wrong value in payload key scope');
    }

    /** * This test verifies that the correct filter * values are being added to the payload if set * * @group promotions */
    public function testPayloadAdvancedFilterValues(): void
    {
        $builder = new PromotionItemBuilder();

        

        if (!($field instanceof CreatedByField)) {
            throw DataAbstractionLayerException::invalidSerializerField(CreatedByField::class$field);
        }

        // only required for new entities         if ($existence->exists()) {
            return;
        }

        $context = $parameters->getContext()->getContext();
        $scope = $context->getScope();

        if (!\in_array($scope$field->getAllowedWriteScopes(), true)) {
            return;
        }

        if ($data->getValue()) {
            yield from parent::encode($field$existence$data$parameters);

            return;
        }

        
public function encode(Field $field, EntityExistence $existence, KeyValuePair $data, WriteParameterBag $parameters): \Generator
    {
        if (!($field instanceof UpdatedByField)) {
            throw DataAbstractionLayerException::invalidSerializerField(UpdatedByField::class$field);
        }

        if (!$existence->exists()) {
            return;
        }

        $context = $parameters->getContext()->getContext();
        $scope = $context->getScope();

        if (!\in_array($scope$field->getAllowedWriteScopes(), true)) {
            return;
        }

        if (!$context->getSource() instanceof AdminApiSource) {
            return;
        }

        $userId = $context->getSource()->getUserId();

        

        }

        return $main;
    }

    private function validateContextHasPermission(Field $field, KeyValuePair $data, WriteParameterBag $parameters): void
    {
        /** @var WriteProtected $flag */
        $flag = $field->getFlag(WriteProtected::class);

        if ($flag->isAllowed($parameters->getContext()->getContext()->getScope())) {
            return;
        }

        $message = 'This field is write-protected.';
        $allowedOrigins = '';
        if ($flag->getAllowedScopes()) {
            $message .= ' (Got: "%s" scope and "%s" is required)';
            $allowedOrigins = implode(' or ', $flag->getAllowedScopes());
        }

        $violationList = new ConstraintViolationList();
        
Home | Imprint | This part of the site doesn't use cookies.