/
LineItemWithQuantityRule.php
74 lines (62 loc) · 2.08 KB
/
LineItemWithQuantityRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php declare(strict_types=1);
namespace Shopware\Core\Checkout\Cart\Rule;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Rule\Exception\UnsupportedOperatorException;
use Shopware\Core\Framework\Rule\Rule;
use Shopware\Core\Framework\Rule\RuleComparison;
use Shopware\Core\Framework\Rule\RuleConstraints;
use Shopware\Core\Framework\Rule\RuleScope;
use Shopware\Core\Framework\Validation\Constraint\Uuid;
use Symfony\Component\Validator\Constraints\NotBlank;
#[Package('services-settings')]
class LineItemWithQuantityRule extends Rule
{
final public const RULE_NAME = 'cartLineItemWithQuantity';
/**
* @internal
*/
public function __construct(
protected string $operator = self::OPERATOR_EQ,
protected ?string $id = null,
protected ?int $quantity = null
) {
parent::__construct();
}
/**
* @throws UnsupportedOperatorException
*/
public function match(RuleScope $scope): bool
{
if ($scope instanceof LineItemScope) {
return $this->lineItemMatches($scope->getLineItem());
}
if (!$scope instanceof CartRuleScope) {
return false;
}
foreach ($scope->getCart()->getLineItems()->filterGoodsFlat() as $lineItem) {
if ($this->lineItemMatches($lineItem)) {
return true;
}
}
return false;
}
public function getConstraints(): array
{
return [
'id' => [new NotBlank(), new Uuid()],
'quantity' => RuleConstraints::int(),
'operator' => RuleConstraints::numericOperators(false),
];
}
private function lineItemMatches(LineItem $lineItem): bool
{
if ($lineItem->getReferencedId() !== $this->id && $lineItem->getPayloadValue('parentId') !== $this->id) {
return false;
}
if ($this->quantity === null) {
return true;
}
return RuleComparison::numeric($lineItem->getQuantity(), $this->quantity, $this->operator);
}
}