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

AffiliateCodeRule::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
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\Rule\Exception\UnsupportedValueException;
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\RuleConstraints;
11
use Shopware\Core\Framework\Rule\RuleScope;
12
13
/**
14
 * @package business-ops
15
 */
16
class AffiliateCodeRule extends Rule
17
{
18
    protected string $operator;
19
20
    protected ?string $affiliateCode = null;
21
22
    /**
23
     * @internal
24
     */
25
    public function __construct(string $operator = self::OPERATOR_EQ, ?string $affiliateCode = null)
26
    {
27
        parent::__construct();
28
29
        $this->operator = $operator;
30
        $this->affiliateCode = $affiliateCode;
31
    }
32
33
    public function getName(): string
34
    {
35
        return 'customerAffiliateCode';
36
    }
37
38
    public function match(RuleScope $scope): bool
39
    {
40
        if (!$scope instanceof CheckoutRuleScope) {
41
            return false;
42
        }
43
44
        if (!$customer = $scope->getSalesChannelContext()->getCustomer()) {
45
            return RuleComparison::isNegativeOperator($this->operator);
46
        }
47
48
        if (!$this->affiliateCode && $this->operator !== self::OPERATOR_EMPTY) {
49
            throw new UnsupportedValueException(\gettype($this->affiliateCode), self::class);
50
        }
51
52
        if (!$affiliateCode = $customer->getAffiliateCode()) {
53
            return RuleComparison::isNegativeOperator($this->operator);
54
        }
55
56
        return RuleComparison::string($affiliateCode, $this->affiliateCode ?? '', $this->operator);
57
    }
58
59
    public function getConstraints(): array
60
    {
61
        $constraints = [
62
            'operator' => RuleConstraints::stringOperators(true),
63
        ];
64
65
        if ($this->operator === self::OPERATOR_EMPTY) {
66
            return $constraints;
67
        }
68
69
        $constraints['affiliateCode'] = RuleConstraints::string();
70
71
        return $constraints;
72
    }
73
74
    public function getConfig(): RuleConfig
75
    {
76
        return (new RuleConfig())
77
            ->operatorSet(RuleConfig::OPERATOR_SET_STRING, true)
78
            ->stringField('affiliateCode');
79
    }
80
}
81