Passed
Push — trunk ( a770c7...5cef50 )
by Christian
24:11 queued 05:55
created

OrderTransactionStatusRule::match()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 14
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 25
rs 8.4444
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Content\Flow\Rule;
4
5
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates;
6
use Shopware\Core\Framework\Log\Package;
7
use Shopware\Core\Framework\Rule\FlowRule;
8
use Shopware\Core\Framework\Rule\Rule;
9
use Shopware\Core\Framework\Rule\RuleComparison;
10
use Shopware\Core\Framework\Rule\RuleConfig;
11
use Shopware\Core\Framework\Rule\RuleConstraints;
12
use Shopware\Core\Framework\Rule\RuleScope;
13
use Shopware\Core\System\StateMachine\Aggregation\StateMachineState\StateMachineStateDefinition;
14
15
#[Package('business-ops')]
16
class OrderTransactionStatusRule extends FlowRule
17
{
18
    public const RULE_NAME = 'orderTransactionStatus';
19
20
    /**
21
     * @internal
22
     *
23
     * @param list<string> $stateIds
0 ignored issues
show
Bug introduced by
The type Shopware\Core\Content\Flow\Rule\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
     */
25
    public function __construct(
26
        public string $operator = Rule::OPERATOR_EQ,
27
        public ?array $stateIds = null
28
    ) {
29
        parent::__construct();
30
    }
31
32
    public function getConstraints(): array
33
    {
34
        return [
35
            'operator' => RuleConstraints::uuidOperators(false),
36
            'stateIds' => RuleConstraints::uuids(),
37
        ];
38
    }
39
40
    public function match(RuleScope $scope): bool
41
    {
42
        if (!$scope instanceof FlowRuleScope || $this->stateIds === null) {
43
            return false;
44
        }
45
46
        if (!$transactions = $scope->getOrder()->getTransactions()) {
47
            return false;
48
        }
49
50
        $paymentMethodId = $transactions->last()->getStateId();
51
52
        foreach ($transactions->getElements() as $transaction) {
53
            $technicalName = $transaction->getStateMachineState()?->getTechnicalName();
54
            if ($technicalName !== null
55
                && $technicalName !== OrderTransactionStates::STATE_FAILED
56
                && $technicalName !== OrderTransactionStates::STATE_CANCELLED
57
            ) {
58
                $paymentMethodId = $transaction->getStateId();
59
60
                break;
61
            }
62
        }
63
64
        return RuleComparison::stringArray($paymentMethodId, $this->stateIds, $this->operator);
65
    }
66
67
    public function getConfig(): RuleConfig
68
    {
69
        return (new RuleConfig())
70
            ->operatorSet(RuleConfig::OPERATOR_SET_STRING, false, true)
71
            ->entitySelectField(
72
                'stateIds',
73
                StateMachineStateDefinition::ENTITY_NAME,
74
                true,
75
                [
76
                    'criteria' => [
77
                        'associations' => [
78
                            'stateMachine',
79
                        ],
80
                        'filters' => [
81
                            [
82
                                'type' => 'equals',
83
                                'field' => 'state_machine_state.stateMachine.technicalName',
84
                                'value' => 'order_transaction.state',
85
                            ],
86
                        ],
87
                    ],
88
                ]
89
            );
90
    }
91
}
92