Passed
Push — trunk ( c7d149...e95fb4 )
by Christian
16:03 queued 12s
created

LineItemPropertyValueRule::matchLineItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 11
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Cart\Rule;
4
5
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
6
use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionDefinition;
7
use Shopware\Core\Framework\Log\Package;
8
use Shopware\Core\Framework\Rule\Rule;
9
use Shopware\Core\Framework\Rule\RuleComparison;
10
use Shopware\Core\Framework\Rule\RuleConfig;
11
use Shopware\Core\Framework\Rule\RuleConstraints;
12
use Shopware\Core\Framework\Rule\RuleScope;
13
14
#[Package('business-ops')]
15
class LineItemPropertyValueRule extends Rule
16
{
17
    public const RULE_NAME = 'cartLineItemPropertyValue';
18
19
    /**
20
     * @internal
21
     *
22
     * @param list<string>|null $identifiers
0 ignored issues
show
Bug introduced by
The type Shopware\Core\Checkout\Cart\Rule\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
     */
24
    public function __construct(
25
        public string $operator = Rule::OPERATOR_EQ,
26
        public ?array $identifiers = null
27
    ) {
28
        parent::__construct();
29
    }
30
31
    public function getConstraints(): array
32
    {
33
        return [
34
            'operator' => RuleConstraints::uuidOperators(false),
35
            'identifiers' => RuleConstraints::uuids(),
36
        ];
37
    }
38
39
    public function match(RuleScope $scope): bool
40
    {
41
        if ($scope instanceof LineItemScope) {
42
            return $this->matchLineItem($scope->getLineItem());
43
        }
44
45
        if (!$scope instanceof CartRuleScope) {
46
            return false;
47
        }
48
49
        foreach ($scope->getCart()->getLineItems()->filterGoodsFlat() as $item) {
50
            if ($this->matchLineItem($item)) {
51
                return true;
52
            }
53
        }
54
55
        return false;
56
    }
57
58
    public function matchLineItem(LineItem $lineItem): bool
59
    {
60
        /**
61
         * @var list<string> $value
62
         */
63
        $value = $lineItem->getPayloadValue('propertyIds') ?? [];
64
65
        return RuleComparison::uuids(
66
            $value,
0 ignored issues
show
Bug introduced by
$value of type Shopware\Core\Checkout\Cart\Rule\list is incompatible with the type array|null expected by parameter $itemValue of Shopware\Core\Framework\...RuleComparison::uuids(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
            /** @scrutinizer ignore-type */ $value,
Loading history...
67
            $this->identifiers,
68
            $this->operator
69
        );
70
    }
71
72
    public function getConfig(): RuleConfig
73
    {
74
        return (new RuleConfig())
75
            ->operatorSet(RuleConfig::OPERATOR_SET_STRING, false, true)
76
            ->entitySelectField('identifiers', PropertyGroupOptionDefinition::ENTITY_NAME, true);
77
    }
78
}
79