Test Setup Failed
Push — test ( 8f35ef...256cd8 )
by Tom
02:35
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
     * get id of pipeline that scales across multiple files / contents
69
     *
70
     * @return string pipelines inside pipeline (pip) pipeline-id
71
     */
72 1
    public function getPipId()
73
    {
74 1
        $buffer = $this->getId();
75 1
        $hash = $this->file->getContentHash();
76
77
        return sprintf('%s{%s}', $buffer, $hash ?: '');
78
    }
79 1
80 1
    /**
81
     * @param string $expression [optional]
82 1
     *
83 1
     * @return void
84 1
     */
85 1
    public function setStepsExpression($expression = null)
86
    {
87
        if (null === $expression) {
88
            return;
89
        }
90 2
91
        // parse, resolve early
92 2
        $array = StepExpression::createFromString($expression)
93
            ->resolveSteps($this->getSteps());
94
95
        $this->steps->setGetIteratorFunctor(function () use ($array) {
96
            return new \ArrayIterator($array);
97
        });
98
    }
99
100
    /**
101
     * @return Steps
102 1
     */
103
    public function getSteps()
104 1
    {
105
        return $this->steps;
106
    }
107
108
    /**
109
     * Specify data which should be serialized to JSON
110 6
     *
111
     * @return array
112 6
     *
113 6
     * @since 5.4.0
114 6
     */
115
    public function jsonSerialize()
116 6
    {
117
        return $this->steps->jsonSerialize();
118
    }
119
120
    /**
121
     * @return void
122
     */
123
    private function parsePipeline(array $array)
124
    {
125
        $this->steps = new Steps(
126
            $this,
127 6
            $this->filterMapList($array, array('step', 'parallel'))
128
        );
129 6
    }
130 6
131 6
    /**
132 1
     * filter a list (in indexed array) af maps (string indexed array)
133
     * with a single entry only
134 6
     *
135 6
     * @param array|array[] $mapList
136 1
     * @param array|string[] $keys
137
     *
138 6
     * @return array
139 6
     */
140
    private function filterMapList(array $mapList, array $keys)
141
    {
142 6
        $result = array();
143
        foreach ($mapList as $map) {
144
            if (!is_array($map) || 0 === count($map)) {
145
                continue;
146
            }
147
            $key = key($map);
148
            if (!in_array($key, $keys, true)) {
149
                continue;
150
            }
151
            $value = current($map);
152
            $result[] = array($key => $value);
153
        }
154
155
        return $result;
156
    }
157
}
158