Passed
Push — trunk ( a2366d...897fa6 )
by Christian
16:38 queued 13s
created

CustomerCustomFieldRule::isSwitchOrBoolField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Log\Package;
7
use Shopware\Core\Framework\Rule\CustomFieldRule;
8
use Shopware\Core\Framework\Rule\Exception\UnsupportedOperatorException;
9
use Shopware\Core\Framework\Rule\Rule;
10
use Shopware\Core\Framework\Rule\RuleScope;
11
12
#[Package('business-ops')]
13
class CustomerCustomFieldRule extends Rule
14
{
15
    final public const RULE_NAME = 'customerCustomField';
16
17
    protected string|int|bool|null|float $renderedFieldValue = null;
18
19
    /**
20
     * @param array<string, string> $renderedField
21
     *
22
     * @internal
23
     */
24
    public function __construct(
25
        protected string $operator = self::OPERATOR_EQ,
26
        protected array $renderedField = []
27
    ) {
28
        parent::__construct();
29
    }
30
31
    /**
32
     * @throws UnsupportedOperatorException
33
     */
34
    public function match(RuleScope $scope): bool
35
    {
36
        if (!$scope instanceof CheckoutRuleScope) {
37
            return false;
38
        }
39
40
        $customer = $scope->getSalesChannelContext()->getCustomer();
41
42
        if ($customer === null) {
43
            return false;
44
        }
45
46
        $customFields = $customer->getCustomFields() ?? [];
47
48
        return CustomFieldRule::match($this->renderedField, $this->renderedFieldValue, $this->operator, $customFields);
49
    }
50
51
    public function getConstraints(): array
52
    {
53
        return CustomFieldRule::getConstraints($this->renderedField);
54
    }
55
}
56