Test Setup Failed
Push — test ( 256cd8...8f35ef )
by Tom
02:59
created

Pipeline::getPipId()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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