Passed
Push — test ( 5112de...d2aafa )
by Tom
02:49
created

Step::getName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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