Passed
Push — master ( 3b22a1...94154d )
by Tom
03:44
created

Pipeline::jsonSerialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines;
6
7
use Ktomk\Pipelines\File\ParseException;
8
9
class Pipeline
10
{
11
    /**
12
     * @var File
13
     */
14
    private $file;
15
16
    /**
17
     * @var array|Step[]
18
     */
19
    private $steps;
20
21
    /**
22
     * Pipeline constructor.
23
     * @param File $file
24
     * @param array $definition
25
     * @throws \Ktomk\Pipelines\File\ParseException
26
     */
27 5
    public function __construct(File $file, array $definition)
28
    {
29
        // quick validation
30 5
        if (!isset($definition[0])) {
31 1
            ParseException::__("Pipeline requires a list of steps");
32
        }
33
34 5
        $this->file = $file;
35 5
        $this->steps = $this->parseSteps($definition);
36 4
    }
37
38
    /**
39
     * @return File
40
     */
41 2
    public function getFile()
42
    {
43 2
        return $this->file;
44
    }
45
46
    /**
47
     * get id of pipeline within the corresponding file object
48
     *
49
     * @return null|string id, can be null in fake/test conditions
50
     */
51 1
    public function getId()
52
    {
53 1
        return $this->file->getIdFrom($this);
54
    }
55
56
    /**
57
     * @return array|Step[]
58
     */
59 1
    public function getSteps()
60
    {
61 1
        return $this->steps;
62
    }
63
64
    /**
65
     * Specify data which should be serialized to JSON
66
     *
67
     * @return array
68
     * @since 5.4.0
69
     */
70 1
    public function jsonSerialize()
71
    {
72 1
        $steps = array();
73 1
        foreach ($this->steps as $step) {
74 1
            $steps[] = $step->jsonSerialize();
75
        }
76
77
        return array(
78 1
            'steps' => $steps,
79
        );
80
    }
81
82
    /**
83
     * @param array $definition
84
     * @throws \Ktomk\Pipelines\File\ParseException
85
     * @return array
86
     */
87 5
    private function parseSteps(array $definition) {
88 5
        $steps = array();
89 5
        foreach ($definition as $index => $step) {
90 5
            if (!is_array($step)) {
91 1
                ParseException::__("Pipeline requires a list of steps");
92
            }
93 5
            $steps[] = $this->step($step);
94
        }
95
96 4
        return $steps;
97
    }
98
99
    /**
100
     * @param array $step
101
     * @throws \Ktomk\Pipelines\File\ParseException
102
     * @return Step
103
     */
104 5
    private function step(array $step)
105
    {
106 5
        if (!isset($step['step'])) {
107 1
            ParseException::__(
108 1
                "Missing required property 'step'"
109
            );
110
        }
111
112 4
        return new Step($this, $step['step']);
113
    }
114
}
115