ArrayFactory::getProcess()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
* This file is part of the StateMachine package
7
*
8
* (c) Michal Wachowski <[email protected]>
9
*
10
* For the full copyright and license information, please view the LICENSE
11
* file that was distributed with this source code.
12
*/
13
14
namespace StateMachine;
15
16
/**
17
 * Adapter for array schemas
18
 *
19
 * @package StateMachine
20
 */
21
final class ArrayFactory
22
{
23
    /**
24
     * Schema array
25
     *
26
     * @var array
27
     */
28
    private $schema;
29
30
    /**
31
     * Resolved process instance
32
     *
33
     * @var Process
34
     */
35
    private $process;
36
37
    /**
38
     * Construct
39
     *
40
     * @param array $schema
41
     */
42 3
    public function __construct(array $schema)
43
    {
44 3
        $this->schema = $schema;
45 3
    }
46
47
    /**
48
     * Return schema (process) name
49
     *
50
     * @return string
51
     */
52 2
    public function getSchemaName(): string
53
    {
54 2
        return $this->getOffsetFromArray($this->schema, 'name');
55
    }
56
57
    /**
58
     * Return initial state name
59
     *
60
     * @return string
61
     */
62 2
    public function getInitialState(): string
63
    {
64 2
        return $this->getOffsetFromArray($this->schema, 'initialState');
65
    }
66
67
    /**
68
     * Build states for process
69
     *
70
     * @return array
71
     */
72 1
    private function buildStates(): array
73
    {
74 1
        $states = [];
75 1
        foreach ($this->getOffsetFromArray($this->schema, 'states', []) as $state) {
76 1
            $states[] = new State(
77 1
                $this->getOffsetFromArray($state, 'name'),
78 1
                $this->buildEvents($state),
79 1
                $this->getAdditionalFromArray($state, ['events', 'name'])
80
            );
81
        }
82
83 1
        return $states;
84
    }
85
86
    /**
87
     * Build state events
88
     *
89
     * @param array $state
90
     *
91
     * @return Event[]
92
     */
93 1
    private function buildEvents(array $state): array
94
    {
95 1
        $events = [];
96 1
        foreach ($this->getOffsetFromArray($state, 'events', []) as $event) {
97 1
            $events[] = new Event(
98 1
                $this->getOffsetFromArray($event, 'name'),
99 1
                $this->getOffsetFromArray($event, 'targetState'),
100 1
                $this->getOffsetFromArray($event, 'errorState'),
101 1
                $this->getOffsetFromArray($event, 'command'),
102 1
                $this->getAdditionalFromArray($event, ['name', 'command', 'targetState', 'errorState'])
103
            );
104
        }
105
106 1
        return $events;
107
    }
108
109
    /**
110
     * Return schema process
111
     *
112
     * @return Process
113
     */
114 1
    public function getProcess(): Process
115
    {
116 1
        if ($this->process === null) {
117 1
            $this->process = new Process(
118 1
                $this->getSchemaName(),
119 1
                $this->getInitialState(),
120 1
                $this->buildStates()
121
            );
122
        }
123
124 1
        return $this->process;
125
    }
126
127
    /**
128
     * Returns array element matching key
129
     *
130
     * @param array  $array
131
     * @param string $offset
132
     * @param mixed  $default
133
     *
134
     * @return mixed
135
     */
136 3
    private function getOffsetFromArray(array $array, $offset, $default = null)
137
    {
138 3
        return array_key_exists($offset, $array) ? $array[$offset] : $default;
139
    }
140
141
    /**
142
     * Return array elements not matching keys
143
     *
144
     * @param array $array
145
     * @param array $ignoredKeys
146
     *
147
     * @return array
148
     */
149 1
    private function getAdditionalFromArray(array $array, array $ignoredKeys): array
150
    {
151 1
        return array_intersect_key($array, array_diff_key($array, array_flip($ignoredKeys)));
152
    }
153
}
154