OrderItemStateFlagExpander   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 29
c 1
b 0
f 1
dl 0
loc 103
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A expandOrderItemWithStateFlags() 0 18 4
A getOmsStateFlags() 0 11 3
A __construct() 0 4 1
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Minubo\Business\DataExpander;
9
10
use SprykerEco\Zed\Minubo\Dependency\Facade\MinuboToOmsFacadeInterface;
11
12
class OrderItemStateFlagExpander implements OrderItemStateFlagExpanderInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    public const KEY_ITEMS = 'Items';
18
19
    /**
20
     * @var string
21
     */
22
    public const KEY_PROCESS = 'Process';
23
24
    /**
25
     * @var string
26
     */
27
    public const KEY_STATE = 'State';
28
29
    /**
30
     * @var string
31
     */
32
    public const KEY_NAME = 'name';
33
34
    /**
35
     * @var string
36
     */
37
    public const KEY_OMS_STATE_FLAGS = 'OmsStateFlags';
38
39
    /**
40
     * @var string
41
     */
42
    public const KEY_TRANSITION_LOG = 'TransitionLogs';
43
44
    /**
45
     * @var string
46
     */
47
    public const KEY_TARGET_STATE = 'target_state';
48
49
    /**
50
     * @var string
51
     */
52
    public const KEY_OMS_TARGET_STATE_FLAGS = 'OmsTargetStateFlags';
53
54
    /**
55
     * @var array
56
     */
57
    protected static $stateFlags;
58
59
    /**
60
     * @var \SprykerEco\Zed\Minubo\Dependency\Facade\MinuboToOmsFacadeInterface
61
     */
62
    protected $omsFacade;
63
64
    /**
65
     * @param \SprykerEco\Zed\Minubo\Dependency\Facade\MinuboToOmsFacadeInterface $omsFacade
66
     */
67
    public function __construct(MinuboToOmsFacadeInterface $omsFacade)
68
    {
69
        $this->omsFacade = $omsFacade;
70
        static::$stateFlags = [];
71
    }
72
73
    /**
74
     * @param array $data
75
     *
76
     * @return array
77
     */
78
    public function expandOrderItemWithStateFlags(array $data): array
79
    {
80
        foreach ($data[static::KEY_ITEMS] as $key => $item) {
81
            $processName = $item[static::KEY_PROCESS][static::KEY_NAME];
82
            $stateName = $item[static::KEY_STATE][static::KEY_NAME];
83
            $data[static::KEY_ITEMS][$key][static::KEY_OMS_STATE_FLAGS] = $this->getOmsStateFlags($processName, $stateName);
84
85
            foreach ($item[static::KEY_TRANSITION_LOG] as $trKey => $transition) {
86
                if (!array_key_exists(static::KEY_TARGET_STATE, $transition)) {
87
                    continue;
88
                }
89
                $targetState = $transition[static::KEY_TARGET_STATE];
90
                $data[static::KEY_ITEMS][$key][static::KEY_TRANSITION_LOG][$trKey][static::KEY_OMS_TARGET_STATE_FLAGS] =
91
                    $this->getOmsStateFlags($processName, $targetState);
92
            }
93
        }
94
95
        return $data;
96
    }
97
98
    /**
99
     * @param string $processName
100
     * @param string $stateName
101
     *
102
     * @return array
103
     */
104
    protected function getOmsStateFlags(string $processName, string $stateName): array
105
    {
106
        if (!isset(static::$stateFlags[$processName])) {
107
            static::$stateFlags[$processName] = [];
108
        }
109
110
        if (!isset(static::$stateFlags[$processName][$stateName])) {
111
            static::$stateFlags[$processName][$stateName] = $this->omsFacade->getStateFlags($processName, $stateName);
112
        }
113
114
        return static::$stateFlags[$processName][$stateName];
115
    }
116
}
117