Passed
Push — steps ( cace08...74de34 )
by Tom
02:51
created

Step::validateTrigger()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 5
nop 2
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 4
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
12
class Step
13
{
14
    /**
15
     * @var array
16
     */
17
    private $step;
18
19
    /**
20
     * @var int number of the step, starting at one
21
     */
22
    private $index;
23
24
    /**
25
     * @var Pipeline
26
     */
27
    private $pipeline;
28
29
    /**
30
     * @var array step environment variables
31
     *   BITBUCKET_PARALLEL_STEP - zero-based index of the current step in the group, e.g. 0, 1, 2, ...
32
     *   BITBUCKET_PARALLEL_STEP_COUNT - total number of steps in the group, e.g. 5.
33
     */
34
    private $env;
35
36
    /**
37
     * Step constructor.
38
     *
39
     * @param Pipeline $pipeline
40
     * @param int $index
41
     * @param array $step
42
     * @param array $env [optional] environment variables in array notation for the new step
43
     */
44 18
    public function __construct(Pipeline $pipeline, $index, array $step, array $env = array())
45
    {
46
        // quick validation: image name
47 18
        Image::validate($step);
48
49
        // quick validation: trigger
50 17
        $this->validateTrigger($step, (bool)$env);
51
52
        // quick validation: script
53 17
        $this->parseScript($step);
54
55 14
        $this->pipeline = $pipeline;
56 14
        $this->index = $index;
57 14
        $this->step = $step;
58 14
        $this->env = $env;
59 14
    }
60
61
    /**
62
     * @throws \Ktomk\Pipelines\File\ParseException
63
     * @return null|Artifacts
64
     */
65 3
    public function getArtifacts()
66
    {
67 3
        return isset($this->step['artifacts'])
68 1
            ? new Artifacts($this->step['artifacts'])
69 3
            : null;
70
    }
71
72
    /**
73
     * @throws \Ktomk\Pipelines\File\ParseException
74
     * @return Image
75
     */
76 3
    public function getImage()
77
    {
78 3
        return isset($this->step['image'])
79 1
            ? new Image($this->step['image'])
80 3
            : $this->pipeline->getFile()->getImage();
81
    }
82
83
    /**
84
     * @return null|string
85
     */
86 3
    public function getName()
87
    {
88 3
        return isset($this->step['name'])
89 1
            ? (string)$this->step['name']
90 3
            : null;
91
    }
92
93
    /**
94
     * @return StepServices
95
     */
96 1
    public function getServices()
97
    {
98 1
        $services = isset($this->step['services']) ? $this->step['services'] : array();
99
100 1
        return new StepServices($this, $services);
101
    }
102
103
    /**
104
     * @return array|string[]
105
     */
106 2
    public function getScript()
107
    {
108 2
        return $this->step['script'];
109
    }
110
111
    /**
112
     * @return bool
113
     */
114 1
    public function isManual()
115
    {
116 1
        if (0 === $this->index) {
117 1
            return  false;
118
        }
119
120 1
        return (isset($this->step['trigger']) && 'manual' === $this->step['trigger']);
121
    }
122
123
    /**
124
     * Specify data which should be serialized to JSON
125
     *
126
     * @return array
127
     */
128 1
    public function jsonSerialize()
129
    {
130 1
        $image = $this->getImage();
131 1
        $image = null === $image ? '' : $image->jsonSerialize();
132
133
        return array(
134 1
            'name' => $this->getName(),
135 1
            'image' => $image,
136 1
            'script' => $this->getScript(),
137 1
            'artifacts' => $this->getArtifacts(),
138
        );
139
    }
140
141
    /**
142
     * @return int
143
     */
144 1
    public function getIndex()
145
    {
146 1
        return $this->index;
147
    }
148
149
    /**
150
     * @return Pipeline
151
     * @codeCoverageIgnore
152
     */
153
    public function getPipeline()
154
    {
155
        return $this->pipeline;
156
    }
157
158
    /**
159
     * @return array step container environment variables (e.g. parallel a step)
160
     */
161 1
    public function getEnv()
162
    {
163 1
        return $this->env;
164
    }
165
166
    /**
167
     *
168
     *
169
     * @param array $array
170
     * @param bool $isPrallelStep
171
     */
172 17
    private function validateTrigger(array $array, $isPrallelStep)
173
    {
174 17
        if (!array_key_exists('trigger', $array)) {
175 17
            return;
176
        }
177
178 2
        $trigger = $array['trigger'];
179 2
        if ($isPrallelStep) {
180 1
            ParseException::__("Unexpected property 'trigger' in parallel step");
181
        }
182
183 2
        if (!in_array($trigger, array('manual', 'automatic'), true)) {
184 1
            ParseException::__("'trigger' expects either 'manual' or 'automatic'");
185
        }
186 2
    }
187
188
    /**
189
     * Parse a step script section
190
     *
191
     * @param array $step
192
     * @throws \Ktomk\Pipelines\File\ParseException
193
     */
194 17
    private function parseScript(array $step)
195
    {
196 17
        if (!isset($step['script'])) {
197 1
            ParseException::__("'step' requires a script");
198
        }
199 16
        if (!is_array($step['script']) || !count($step['script'])) {
200 1
            ParseException::__("'script' requires a list of commands");
201
        }
202
203 15
        foreach ($step['script'] as $index => $line) {
204 15
            if (!is_scalar($line) && null !== $line) {
205 1
                ParseException::__(
206 1
                    sprintf(
207 1
                        "'script' requires a list of commands, step #%d is not a command",
208 1
                        $index
209
                    )
210
                );
211
            }
212
        }
213 14
    }
214
}
215