Passed
Push — trunk ( 10200d...995d50 )
by Christian
11:59 queued 12s
created

ShippingStateRule   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 71
rs 10
c 1
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfig() 0 5 1
A match() 0 17 4
A getConstraints() 0 16 2
A __construct() 0 5 1
A getName() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Customer\Rule;
4
5
use Shopware\Core\Checkout\CheckoutRuleScope;
6
use Shopware\Core\Framework\Rule\Exception\UnsupportedOperatorException;
7
use Shopware\Core\Framework\Rule\Rule;
8
use Shopware\Core\Framework\Rule\RuleComparison;
9
use Shopware\Core\Framework\Rule\RuleConfig;
10
use Shopware\Core\Framework\Rule\RuleScope;
11
use Shopware\Core\Framework\Validation\Constraint\ArrayOfUuid;
12
use Shopware\Core\System\Country\Aggregate\CountryState\CountryStateDefinition;
13
use Symfony\Component\Validator\Constraints\Choice;
14
use Symfony\Component\Validator\Constraints\NotBlank;
15
16
/**
17
 * @package business-ops
18
 */
19
class ShippingStateRule extends Rule
20
{
21
    /**
22
     * @var string[]
23
     */
24
    protected ?array $stateIds;
25
26
    protected string $operator;
27
28
    /**
29
     * @internal
30
     *
31
     * @param list<string>|null $stateIds
0 ignored issues
show
Bug introduced by
The type Shopware\Core\Checkout\Customer\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...
32
     */
33
    public function __construct(string $operator = self::OPERATOR_EQ, ?array $stateIds = null)
34
    {
35
        parent::__construct();
36
        $this->operator = $operator;
37
        $this->stateIds = $stateIds;
38
    }
39
40
    /**
41
     * @throws UnsupportedOperatorException
42
     */
43
    public function match(RuleScope $scope): bool
44
    {
45
        if (!$scope instanceof CheckoutRuleScope) {
46
            return false;
47
        }
48
49
        if (!$state = $scope->getSalesChannelContext()->getShippingLocation()->getState()) {
50
            return RuleComparison::isNegativeOperator($this->operator);
51
        }
52
53
        $stateId = $state->getId();
54
        $parameter = [$stateId];
55
        if ($stateId === '') {
56
            $parameter = [];
57
        }
58
59
        return RuleComparison::uuids($parameter, $this->stateIds, $this->operator);
60
    }
61
62
    public function getConstraints(): array
63
    {
64
        $constraints = [
65
            'operator' => [
66
                new NotBlank(),
67
                new Choice([self::OPERATOR_EQ, self::OPERATOR_NEQ, self::OPERATOR_EMPTY]),
68
            ],
69
        ];
70
71
        if ($this->operator === self::OPERATOR_EMPTY) {
72
            return $constraints;
73
        }
74
75
        $constraints['stateIds'] = [new NotBlank(), new ArrayOfUuid()];
76
77
        return $constraints;
78
    }
79
80
    public function getName(): string
81
    {
82
        return 'customerShippingState';
83
    }
84
85
    public function getConfig(): RuleConfig
86
    {
87
        return (new RuleConfig())
88
            ->operatorSet(RuleConfig::OPERATOR_SET_STRING, true, true)
89
            ->entitySelectField('stateIds', CountryStateDefinition::ENTITY_NAME, true);
90
    }
91
}
92