Procedure::procedureExists()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.6333
c 0
b 0
f 0
cc 4
nc 6
nop 3
crap 4
1
<?php
2
3
/*
4
 * This file is part of Transfer.
5
 *
6
 * For the full copyright and license information, please view the LICENSE file located
7
 * in the root directory.
8
 */
9
10
namespace Transfer\Procedure;
11
12
/**
13
 * Procedure model.
14
 */
15
class Procedure
16
{
17
    /**
18
     * @var Procedure Parent procedure
19
     */
20
    protected $parent = null;
21
22
    /**
23
     * @var array Children
24
     */
25
    protected $children = array();
26
27
    /**
28
     * @var string Procedure name
29
     */
30
    protected $name;
31
32
    /**
33
     * @var array Source collection
34
     */
35
    protected $sources = array();
36
37
    /**
38
     * @var array Worker collection
39
     */
40
    protected $workers = array();
41
42
    /**
43
     * @var array Target collection
44
     */
45
    protected $targets = array();
46
47
    /**
48
     * @param string    $name    Procedure name
49
     * @param array     $sources Source collection
50
     * @param array     $workers Worker collection
51
     * @param array     $targets Target collection
52
     * @param Procedure $parent  Parent procedure
53
     */
54 10
    public function __construct($name = null, $sources = array(), $workers = array(), $targets = array(), Procedure $parent = null)
55
    {
56 10
        $this->name = $name;
57 10
        $this->sources = $sources;
58 10
        $this->workers = $workers;
59 10
        $this->targets = $targets;
60 10
        $this->parent = $parent;
61 10
    }
62
63
    /**
64
     * Creates a procedure from a definition.
65
     *
66
     * Procedure definition must be an array consisting of following elements:
67
     *   * name : string
68
     *   * sources : array of source adapters
69
     *   * workers : array of workers
70
     *   * targets : array of target adapters
71
     *
72
     * @param array     $definition Procedure definition (how a procedure should be built)
73
     * @param Procedure $parent     Parent procedure
74
     *
75
     * @return Procedure
76
     */
77 9
    public static function createFromDefinition(array $definition, Procedure $parent = null)
78
    {
79 9
        $procedure = new self(
80 9
            $definition['name'],
81 9
            $definition['sources'],
82 9
            $definition['workers'],
83 9
            $definition['targets'],
84
            $parent
85 9
        );
86
87 9
        foreach ($definition['children'] as $child) {
88 4
            $procedure->addChild(self::createFromDefinition($child, $procedure));
89 9
        }
90
91 9
        return $procedure;
92
    }
93
94
    /**
95
     * Returns parent procedure.
96
     *
97
     * @return Procedure Parent procedure
98
     */
99 4
    public function getParent()
100
    {
101 4
        return $this->parent;
102
    }
103
104
    /**
105
     * Returns procedure name.
106
     *
107
     * @return string Procedure name
108
     */
109 3
    public function getName()
110
    {
111 3
        return $this->name;
112
    }
113
114
    /**
115
     * Returns children.
116
     *
117
     * @return array Children procedures
118
     */
119 5
    public function getChildren()
120
    {
121 5
        return $this->children;
122
    }
123
124
    /**
125
     * Tests, if the procedure has children (sub-procedures).
126
     *
127
     * @return bool True, if procedure has children
128
     */
129 3
    public function hasChildren()
130
    {
131 3
        return !empty($this->children);
132
    }
133
134
    /**
135
     * Adds child procedure.
136
     *
137
     * @param Procedure $child Child procedure
138
     */
139 4
    public function addChild(Procedure $child)
140
    {
141 4
        $this->children[] = $child;
142 4
    }
143
144
    /**
145
     * Tests, if a (sub-)procedure exists.
146
     *
147
     * @param string|array $name    Procedure name
148
     * @param Procedure    $context Current context
149
     * @param int          $level   Current level
150
     *
151
     * @return bool True, if procedure exists
152
     */
153 3
    public function procedureExists($name, $context = null, $level = 0)
154
    {
155 3
        $context = $this->normalizeContext($context);
156
157 3
        if (!is_array($name)) {
158 1
            $name = array($name);
159 1
        }
160
161
        /** @var Procedure $procedure */
162 3
        foreach ($context->getChildren() as $procedure) {
163 2
            if ($procedure->getName() == $name[$level]) {
164 2
                return true;
165
            }
166
167 1
            return $this->procedureExists($name, $procedure, $level++);
168 2
        }
169
170 2
        return false;
171
    }
172
173
    /**
174
     * Returns source collection.
175
     *
176
     * @return array Source collection
177
     */
178 5
    public function getSources()
179
    {
180 5
        return array_merge($this->sources, $this->getParentSettings('input'));
181
    }
182
183
    /**
184
     * Returns worker collection.
185
     *
186
     * @return array Worker collection
187
     */
188 3
    public function getWorkers()
189
    {
190 3
        return array_merge($this->workers, $this->getParentSettings('worker'));
191
    }
192
193
    /**
194
     * Returns target collection.
195
     *
196
     * @return array Target collection
197
     */
198 4
    public function getTargets()
199
    {
200 4
        return array_merge($this->targets, $this->getParentSettings('target'));
201
    }
202
203
    /**
204
     * Returns parent settings.
205
     *
206
     * @param string    $type    Setting type (source, worker or target)
207
     * @param Procedure $context
208
     *
209
     * @return array Setting collection
210
     *
211
     * @internal
212
     */
213 6
    private function getParentSettings($type, $context = null)
214
    {
215 6
        $context = $this->normalizeContext($context);
216
217
        $methods = array(
218 6
            'source' => 'getSources',
219 6
            'worker' => 'getWorkers',
220 6
            'target' => 'getTargets',
221 6
        );
222
223 6
        if (!array_key_exists($type, $methods) || $context->getParent() === null) {
224 6
            return array();
225
        }
226
227 2
        $settings = array_merge(
228 2
            $this->getParentComponentSettings($context->getParent(), $methods[$type]),
229 2
            $this->getParentSettings($type, $context->getParent())
230 2
        );
231
232 2
        return $settings;
233
    }
234
235
    /**
236
     * @param Procedure $context
237
     * @param string    $method
238
     *
239
     * @return array
240
     *
241
     * @internal
242
     */
243 2
    private function getParentComponentSettings($context, $method)
244
    {
245 2
        $settings = array();
246
247 2
        foreach (call_user_func(array($context, $method)) as $setting) {
248 1
            $settings[] = $setting;
249 2
        }
250
251 2
        return $settings;
252
    }
253
254
    /**
255
     * @param Procedure|null $context
256
     *
257
     * @return Procedure
258
     *
259
     * @internal
260
     */
261 9
    private function normalizeContext($context = null)
262
    {
263 9
        if ($context === null) {
264 9
            $context = $this;
265 9
        }
266
267 9
        return $context;
268
    }
269
}
270