Passed
Push — master ( d64961...dc496c )
by Tom
04:20
created

Pipeline::parsePipeline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\File;
6
7
use Ktomk\Pipelines\File\Dom\FileNode;
8
use Ktomk\Pipelines\File\Pipeline\Step;
9
use Ktomk\Pipelines\File\Pipeline\Steps;
10
use Ktomk\Pipelines\Value\StepExpression;
11
12
class Pipeline implements FileNode
13
{
14
    /**
15
     * @var File
16
     */
17
    private $file;
18
19
    /**
20
     * @var Steps
21
     */
22
    private $steps;
23
24
    /**
25
     * Pipeline constructor.
26
     *
27
     * @param File $file
28
     * @param array $definition
29
     *
30
     * @throws ParseException
31
     */
32 6
    public function __construct(File $file, array $definition)
33
    {
34
        // quick validation
35 6
        if (!isset($definition[0])) {
36 1
            throw new ParseException('Pipeline requires a list of steps');
37
        }
38
39 6
        $this->file = $file;
40 6
        $this->parsePipeline($definition);
41 6
    }
42
43
    /**
44
     * @return File
45
     */
46 2
    public function getFile()
47
    {
48 2
        return $this->file;
49
    }
50
51
    /**
52
     * get id of pipeline within the corresponding pipelines object
53
     *
54
     * @return null|string id, can be null in fake/test conditions
55
     */
56 1
    public function getId()
57
    {
58 1
        return $this->file->getPipelines()->getId($this);
59
    }
60
61
    /**
62
     * @param string $expression [optional]
63
     *
64
     * @return void
65
     */
66 1
    public function setStepsExpression($expression = null)
67
    {
68 1
        if (null === $expression) {
69 1
            return;
70
        }
71
72
        // parse, resolve early
73 1
        $array = StepExpression::createFromString($expression)
74 1
            ->resolveSteps($this->getSteps());
75
76 1
        $this->steps->setGetIteratorFunctor(function () use ($array) {
77 1
            return new \ArrayIterator($array);
78 1
        });
79 1
    }
80
81
    /**
82
     * @return Step[]|Steps
83
     */
84 2
    public function getSteps()
85
    {
86 2
        return $this->steps;
87
    }
88
89
    /**
90
     * Specify data which should be serialized to JSON
91
     *
92
     * @return array
93
     *
94
     * @since 5.4.0
95
     */
96 1
    public function jsonSerialize()
97
    {
98 1
        return $this->steps->jsonSerialize();
99
    }
100
101 6
    private function parsePipeline(array $array)
102
    {
103 6
        $this->steps = new Steps(
104 6
            $this,
105 6
            $this->filterMapList($array, array('step', 'parallel'))
106
        );
107 6
    }
108
109
    /**
110
     * filter a list (in indexed array) af maps (string indexed array)
111
     * with a single entry only
112
     *
113
     * @param array|array[] $mapList
114
     * @param array|string[] $keys
115
     *
116
     * @return array
117
     */
118 6
    private function filterMapList(array $mapList, array $keys)
119
    {
120 6
        $result = array();
121 6
        foreach ($mapList as $map) {
122 6
            if (!is_array($map) || 1 === !count($map)) {
123 1
                continue;
124
            }
125 6
            $key = key($map);
126 6
            if (!in_array($key, $keys, true)) {
127 1
                continue;
128
            }
129 6
            $value = current($map);
130 6
            $result[] = array($key => $value);
131
        }
132
133 6
        return $result;
134
    }
135
}
136