Passed
Push — trunk ( 8d4a38...24af8b )
by Christian
22:20 queued 11:00
created

CartTotalPurchasePriceRule::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Cart\Rule;
4
5
use Shopware\Core\Framework\Rule\Rule;
6
use Shopware\Core\Framework\Rule\RuleComparison;
7
use Shopware\Core\Framework\Rule\RuleConfig;
8
use Shopware\Core\Framework\Rule\RuleConstraints;
9
use Shopware\Core\Framework\Rule\RuleScope;
10
11
/**
12
 * @package business-ops
13
 */
14
class CartTotalPurchasePriceRule extends Rule
15
{
16
    public const RULE_NAME = 'cartTotalPurchasePrice';
17
18
    protected string $operator = Rule::OPERATOR_EQ;
19
20
    protected string $type = 'gross';
21
22
    protected float $amount = 0.0;
23
24
    public function match(RuleScope $scope): bool
25
    {
26
        if (!$scope instanceof CartRuleScope) {
27
            return false;
28
        }
29
30
        $total = 0.0;
31
32
        foreach ($scope->getCart()->getLineItems()->filterGoodsFlat() as $lineItem) {
33
            $purchasePricePayload = $lineItem->getPayloadValue('purchasePrices');
34
35
            if (!$purchasePricePayload) {
36
                continue;
37
            }
38
39
            $purchasePrice = json_decode($purchasePricePayload, true);
40
41
            $total += ($purchasePrice[$this->type] ?? 0.0) * $lineItem->getQuantity();
42
        }
43
44
        return RuleComparison::numeric($total, $this->amount, $this->operator);
45
    }
46
47
    public function getConstraints(): array
48
    {
49
        return [
50
            'operator' => RuleConstraints::numericOperators(false),
51
            'type' => RuleConstraints::string(),
52
            'amount' => RuleConstraints::float(),
53
        ];
54
    }
55
56
    public function getConfig(): RuleConfig
57
    {
58
        return (new RuleConfig())
59
            ->operatorSet(RuleConfig::OPERATOR_SET_NUMBER)
60
            ->selectField('type', ['gross', 'net'], false, ['class' => 'is--max-content'])
61
            ->numberField('amount');
62
    }
63
}
64