Passed
Push — trunk ( d8aafd...465790 )
by Christian
11:54 queued 12s
created

CustomerSalutationRule   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfig() 0 5 1
A match() 0 20 5
A __construct() 0 3 1
A getConstraints() 0 13 2
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\Rule;
7
use Shopware\Core\Framework\Rule\RuleComparison;
8
use Shopware\Core\Framework\Rule\RuleConfig;
9
use Shopware\Core\Framework\Rule\RuleConstraints;
10
use Shopware\Core\Framework\Rule\RuleScope;
11
use Shopware\Core\System\Salutation\SalutationDefinition;
12
13
/**
14
 * @package business-ops
15
 */
16
class CustomerSalutationRule extends Rule
17
{
18
    public const RULE_NAME = 'customerSalutation';
19
20
    /**
21
     * @internal
22
     *
23
     * @param list<string>|null $salutationIds
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...
24
     */
25
    public function __construct(public string $operator = self::OPERATOR_EQ, public ?array $salutationIds = null)
26
    {
27
        parent::__construct();
28
    }
29
30
    public function getConstraints(): array
31
    {
32
        $constraints = [
33
            'operator' => RuleConstraints::uuidOperators(true),
34
        ];
35
36
        if ($this->operator === self::OPERATOR_EMPTY) {
37
            return $constraints;
38
        }
39
40
        $constraints['salutationIds'] = RuleConstraints::uuids();
41
42
        return $constraints;
43
    }
44
45
    public function match(RuleScope $scope): bool
46
    {
47
        if (!$scope instanceof CheckoutRuleScope) {
48
            return false;
49
        }
50
51
        if (!$customer = $scope->getSalesChannelContext()->getCustomer()) {
52
            return RuleComparison::isNegativeOperator($this->operator);
53
        }
54
        $salutation = $customer->getSalutation();
55
56
        if ($this->operator === self::OPERATOR_EMPTY) {
57
            return $salutation === null;
58
        }
59
60
        if ($salutation === null) {
61
            return RuleComparison::isNegativeOperator($this->operator);
62
        }
63
64
        return RuleComparison::uuids([$salutation->getId()], $this->salutationIds, $this->operator);
65
    }
66
67
    public function getConfig(): RuleConfig
68
    {
69
        return (new RuleConfig())
70
            ->operatorSet(RuleConfig::OPERATOR_SET_STRING, true, true)
71
            ->entitySelectField('salutationIds', SalutationDefinition::ENTITY_NAME, true, ['labelProperty' => 'displayName']);
72
    }
73
}
74