Completed
Push — master ( 89ed1a...e7e380 )
by Tom
07:14
created

Step::getPipeline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
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;
6
7
use Ktomk\Pipelines\File\Artifacts;
8
use Ktomk\Pipelines\File\Image;
9
use Ktomk\Pipelines\File\ParseException;
10
11
class Step
12
{
13
    /**
14
     * @var array
15
     */
16
    private $step;
17
18
    /**
19
     * @var int numer of the step, starting at one
20
     */
21
    private $index;
22
23
    /**
24
     * @var Pipeline
25
     */
26
    private $pipeline;
27
28
    /**
29
     * Step constructor.
30
     * @param Pipeline $pipeline
31
     * @param int $index
32
     * @param array $step
33
     */
34 14
    public function __construct(Pipeline $pipeline, $index, array $step)
35
    {
36
        // quick validation: image name
37 14
        File::validateImage($step);
38
39
        // quick validation: script
40 13
        $this->parseScript($step);
41
42 10
        $this->pipeline = $pipeline;
43 10
        $this->index = $index;
44 10
        $this->step = $step;
45 10
    }
46
47
    /**
48
     * @throws \Ktomk\Pipelines\File\ParseException
49
     * @return null|Artifacts
50
     */
51 3
    public function getArtifacts()
52
    {
53 3
        return isset($this->step['artifacts'])
54 1
            ? new Artifacts($this->step['artifacts'])
55 3
            : null;
56
    }
57
58
    /**
59
     * @throws \Ktomk\Pipelines\File\ParseException
60
     * @return Image
61
     */
62 3
    public function getImage()
63
    {
64 3
        return isset($this->step['image'])
65 1
            ? new Image($this->step['image'])
66 3
            : $this->pipeline->getFile()->getImage();
67
    }
68
69
    /**
70
     * @return null|string
71
     */
72 3
    public function getName()
73
    {
74 3
        return isset($this->step['name'])
75 1
            ? (string)$this->step['name']
76 3
            : null;
77
    }
78
79
    /**
80
     * @return array|string[]
81
     */
82 2
    public function getScript()
83
    {
84 2
        return $this->step['script'];
85
    }
86
87
    /**
88
     * Specify data which should be serialized to JSON
89
     *
90
     * @return array
91
     */
92 1
    public function jsonSerialize()
93
    {
94 1
        $image = $this->getImage();
95 1
        $image = null === $image ? "" : $image->jsonSerialize();
96
97
        return array(
98 1
            'name' => $this->getName(),
99 1
            'image' => $image,
100 1
            'script' => $this->getScript(),
101 1
            'artifacts' => $this->getArtifacts(),
102
        );
103
    }
104
105
    /**
106
     * @return int
107
     */
108 1
    public function getIndex()
109
    {
110 1
        return $this->index;
111
    }
112
113
    /**
114
     * @return Pipeline
115
     * @codeCoverageIgnore
116
     */
117
    public function getPipeline()
118
    {
119
        return $this->pipeline;
120
    }
121
122
    /**
123
     * Parse a step script section
124
     *
125
     * @param array $step
126
     * @throws \Ktomk\Pipelines\File\ParseException
127
     */
128 13
    private function parseScript(array $step)
129
    {
130 13
        if (!isset($step['script'])) {
131 1
            ParseException::__("'step' requires a script");
132
        }
133 12
        if (!is_array($step['script']) || !count($step['script'])) {
134 1
            ParseException::__("'script' requires a list of commands");
135
        }
136
137 11
        foreach ($step['script'] as $index => $line) {
138 11
            if (!is_scalar($line) && null !== $line) {
139 1
                ParseException::__(
140 1
                    sprintf(
141 1
                        "'script' requires a list of commands, step #%d is not a command",
142 11
                        $index
143
                    )
144
                );
145
            }
146
        }
147 10
    }
148
}
149