|
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 CampaignCodeRule extends Rule |
|
17
|
|
|
{ |
|
18
|
|
|
protected string $operator; |
|
19
|
|
|
|
|
20
|
|
|
protected ?string $campaignCode = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @internal |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(string $operator = self::OPERATOR_EQ, ?string $campaignCode = null) |
|
26
|
|
|
{ |
|
27
|
|
|
parent::__construct(); |
|
28
|
|
|
|
|
29
|
|
|
$this->operator = $operator; |
|
30
|
|
|
$this->campaignCode = $campaignCode; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getName(): string |
|
34
|
|
|
{ |
|
35
|
|
|
return 'customerCampaignCode'; |
|
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->campaignCode && $this->operator !== self::OPERATOR_EMPTY) { |
|
49
|
|
|
throw new UnsupportedValueException(\gettype($this->campaignCode), self::class); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (!$campaignCode = $customer->getCampaignCode()) { |
|
53
|
|
|
return RuleComparison::isNegativeOperator($this->operator); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return RuleComparison::string($campaignCode, $this->campaignCode ?? '', $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['campaignCode'] = 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('campaignCode'); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|