Pipelines   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 298
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 64
c 1
b 0
f 0
dl 0
loc 298
ccs 76
cts 76
cp 1
rs 9.84
wmc 32

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 3 1
A searchTypeReference() 0 5 2
A getIdDefault() 0 3 1
A searchReference() 0 7 2
A __construct() 0 7 1
A parsePipelineReferences() 0 9 1
A getDefault() 0 3 1
A searchIdByReference() 0 9 2
A getFile() 0 7 2
A getPipelines() 0 12 3
A getById() 0 3 1
A getPipelineIds() 0 3 1
A parseValidatePipelines() 0 13 5
A referencesAddSections() 0 22 5
A searchIdByTypeReference() 0 3 1
A referencesDefault() 0 17 3
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\File;
6
7
use InvalidArgumentException;
8
use Ktomk\Pipelines\Runner\Reference;
9
10
/**
11
 * Class Pipelines
12
 *
13
 * @package Ktomk\Pipelines\File
14
 */
15
class Pipelines implements Dom\FileNode
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $array;
21
22
    /**
23
     * @var null|File
24
     */
25
    protected $file;
26
27
    /**
28
     * @var array
29
     */
30
    protected $references;
31
32
    /**
33
     * Pipelines constructor.
34
     *
35
     * @param array $array
36
     * @param null|File $file
37
     */
38 18
    public function __construct(array $array, File $file = null)
39
    {
40 18
        $this->file = $file;
41
42 18
        $this->references = $this->parsePipelineReferences($array);
43
44 14
        $this->array = $array;
45
    }
46
47
    /**
48
     * @throws InvalidArgumentException
49
     * @throws ParseException
50
     *
51
     * @return null|Pipeline
52
     */
53 2
    public function getDefault()
54
    {
55 2
        return PipelinesReferences::byId($this, 'default');
56
    }
57
58
    /**
59
     * returns the id of the default pipeline in file or null if there is none
60
     *
61
     * @return null|string
62
     */
63 2
    public function getIdDefault()
64
    {
65 2
        return PipelinesReferences::idDefault($this);
66
    }
67
68
    /**
69
     * @return array|string[]
70
     */
71 3
    public function getPipelineIds()
72
    {
73 3
        return array_map('strval', array_keys($this->references));
74
    }
75
76
    /**
77
     * @param string $id
78
     *
79
     * @throws InvalidArgumentException
80
     * @throws ParseException
81
     *
82
     * @return null|Pipeline
83
     */
84 8
    public function getById($id)
85
    {
86 8
        return PipelinesReferences::byId($this, $id);
87
    }
88
89
    /**
90
     * get id of a pipeline
91
     *
92
     * @param Pipeline $pipeline
93
     *
94
     * @return null|string
95
     */
96 2
    public function getId(Pipeline $pipeline)
97
    {
98 2
        return PipelinesReferences::id($this, $pipeline);
99
    }
100
101
    /**
102
     * Searches a reference
103
     *
104
     * @param Reference $reference
105
     *
106
     * @throws InvalidArgumentException
107
     * @throws ParseException
108
     *
109
     * @return null|Pipeline
110
     */
111 7
    public function searchReference(Reference $reference)
112
    {
113 7
        if (null === $type = $reference->getPipelinesType()) {
114 2
            return $this->getDefault();
115
        }
116
117 5
        return $this->searchTypeReference($type, $reference->getName());
118
    }
119
120
    /**
121
     * Searches a reference within type, returns found one, if
122
     * none is found, the default pipeline or null if there is
123
     * no default pipeline.
124
     *
125
     * @param string $type of pipeline, can be branches, tags or bookmarks
126
     * @param null|string $reference
127
     *
128
     * @throws InvalidArgumentException
129
     * @throws ParseException
130
     *
131
     * @return null|Pipeline
132
     */
133 8
    public function searchTypeReference($type, $reference)
134
    {
135 8
        $id = $this->searchIdByTypeReference($type, $reference);
136
137 7
        return null !== $id ? $this->getById($id) : null;
138
    }
139
140
    /**
141
     * Searches the pipeline that matches the reference
142
     *
143
     * @param Reference $reference
144
     *
145
     * @throws InvalidArgumentException
146
     *
147
     * @return null|string id if found, null otherwise
148
     */
149 1
    public function searchIdByReference(Reference $reference)
150
    {
151 1
        if (null === $reference->getType()) {
152 1
            return $this->getIdDefault();
153
        }
154
155 1
        return $this->searchIdByTypeReference(
156 1
            $reference->getPipelinesType(),
157 1
            $reference->getName()
158
        );
159
    }
160
161
    /**
162
     * @return null|File
163
     */
164 1
    public function getFile()
165
    {
166 1
        if ($this->file instanceof File) {
167 1
            return $this->file;
168
        }
169
170 1
        return null;
171
    }
172
173
    /**
174
     * @throws InvalidArgumentException
175
     * @throws ParseException
176
     *
177
     * @return array|Pipeline[]
178
     */
179 2
    public function getPipelines()
180
    {
181 2
        $pipelines = array();
182
183 2
        foreach ($this->getPipelineIds() as $id) {
184 2
            if (!ReferenceTypes::isValidId($id)) {
185 1
                throw new ParseException(sprintf("invalid pipeline id '%s'", $id));
186
            }
187 1
            $pipelines[$id] = $this->getById($id);
188
        }
189
190 1
        return $pipelines;
191
    }
192
193
    /**
194
     * Index all pipelines as references array map by id
195
     *
196
     * a reference is array($section (or default), $pattern (or null for default), &$arrayFileParseData)
197
     * references are keyed by their pipeline id
198
     *
199
     * @param array $array
200
     *
201
     * @throws ParseException
202
     *
203
     * @return array
204
     */
205 18
    private function parsePipelineReferences(array &$array)
206
    {
207 18
        $this->parseValidatePipelines($array);
208
209 16
        $references = $this->referencesDefault($array);
210
211 15
        $references = $this->referencesAddSections($references, $array);
212
213 14
        return $references;
214
    }
215
216
    /**
217
     * quick validation of pipeline sections (default pipeline + sections)
218
     *
219
     * there must be at least one pipeline in the file
220
     *
221
     * NOTE: the check is incomplete, as it assumes any section contains at
222
     *       least one pipeline which is unchecked
223
     *
224
     * @param array $array
225
     *
226
     * @return void
227
     */
228 18
    private function parseValidatePipelines(array $array)
229
    {
230 18
        $sections = ReferenceTypes::getSections();
231 18
        $count = 0;
232 18
        foreach ($sections as $section) {
233 18
            if (isset($array[$section])) {
234 12
                $count++;
235
            }
236
        }
237 18
        if (!$count && !isset($array['default'])) {
238 2
            $middle = implode(', ', array_slice($sections, 0, -1));
239
240 2
            throw new ParseException("'pipelines' requires at least a default, {$middle} or custom section");
241
        }
242
    }
243
244
    /**
245
     * create references by default pipeline
246
     *
247
     * @param array $array
248
     *
249
     * @return array
250
     */
251 16
    private function referencesDefault(array &$array)
252
    {
253 16
        $references = array();
254
255 16
        $default = ReferenceTypes::SEG_DEFAULT;
256
257 16
        if (!isset($array[$default])) {
258 5
            return $references;
259
        }
260
261 11
        if (!is_array($array[$default])) {
262 1
            throw new ParseException("'{$default}' requires a list of steps");
263
        }
264
265 10
        $references[$default] = array($default, null, &$array[$default]);
266
267 10
        return $references;
268
    }
269
270
    /**
271
     * add section pipelines to references
272
     *
273
     * @param array $references
274
     * @param array $array
275
     *
276
     * @return array
277
     */
278 15
    private function referencesAddSections(array $references, array &$array)
279
    {
280
        // reference section pipelines
281 15
        $sections = ReferenceTypes::getSections();
282
283 15
        foreach ($array as $section => $refs) {
284 15
            if (!in_array($section, $sections, true)) {
285 10
                continue; // not a section for references
286
            }
287 12
            if (!is_array($refs)) {
288 1
                throw new ParseException("'{$section}' requires a list");
289
            }
290 11
            foreach ($refs as $pattern => $pipeline) {
291 11
                $references["{$section}/{$pattern}"] = array(
292 11
                    (string)$section,
293 11
                    (string)$pattern,
294 11
                    &$array[$section][$pattern],
295
                );
296
            }
297
        }
298
299 14
        return $references;
300
    }
301
302
    /**
303
     * @param null|string $type
304
     * @param null|string $reference
305
     *
306
     * @throws InvalidArgumentException
307
     *
308
     * @return null|string
309
     */
310 5
    private function searchIdByTypeReference($type, $reference)
311
    {
312 5
        return PipelinesReferences::idByTypeReference($this, $type, $reference);
313
    }
314
}
315