Passed
Push — master ( 9b47e1...f74cd8 )
by Tom
05:04 queued 02:06
created

Step::parseAfterScript()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
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\Dom\FileNode;
9
use Ktomk\Pipelines\File\Image;
10
use Ktomk\Pipelines\File\ParseException;
11
use Ktomk\Pipelines\File\Pipeline;
12
13
class Step implements FileNode
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
     * @var array step environment variables
32
     *   BITBUCKET_PARALLEL_STEP - zero-based index of the current step in the group, e.g. 0, 1, 2, ...
33
     *   BITBUCKET_PARALLEL_STEP_COUNT - total number of steps in the group, e.g. 5.
34
     */
35
    private $env;
36
37
    /**
38
     * Step constructor.
39
     *
40
     * @param Pipeline $pipeline
41
     * @param int $index
42
     * @param array $step
43
     * @param array $env [optional] environment variables in array notation for the new step
44
     */
45 22
    public function __construct(Pipeline $pipeline, $index, array $step, array $env = array())
46
    {
47
        // validate step
48 22
        StepParser::validate($step, $env);
49
50 18
        $this->pipeline = $pipeline;
51 18
        $this->index = $index;
52 18
        $this->step = $step;
53 18
        $this->env = $env;
54
    }
55
56
    /**
57
     * @throws ParseException
58
     *
59
     * @return null|Artifacts
60
     */
61 3
    public function getArtifacts()
62
    {
63 3
        return isset($this->step['artifacts'])
64 1
            ? new Artifacts($this->step['artifacts'])
65 3
            : null;
66
    }
67
68
    /**
69
     * @return null|StepCondition
70
     */
71 2
    public function getCondition()
72
    {
73 2
        return isset($this->step['condition'])
74 1
            ? new StepCondition($this->step['condition'])
75 2
            : null;
76
    }
77
78
    /**
79
     * @throws ParseException
80
     *
81
     * @return Image
82
     */
83 3
    public function getImage()
84
    {
85 3
        return isset($this->step['image'])
86 1
            ? new Image($this->step['image'])
87 3
            : $this->pipeline->getFile()->getImage();
88
    }
89
90
    /**
91
     * @return null|string
92
     */
93 3
    public function getName()
94
    {
95 3
        return isset($this->step['name'])
96 1
            ? (string)$this->step['name']
97 3
            : null;
98
    }
99
100
    /**
101
     * @return StepCaches
102
     */
103 1
    public function getCaches()
104
    {
105 1
        $caches = isset($this->step['caches']) ? $this->step['caches'] : array();
106
107 1
        return new StepCaches($this, $caches);
108
    }
109
110
    /**
111
     * @return StepServices
112
     */
113 1
    public function getServices()
114
    {
115 1
        $services = isset($this->step['services']) ? $this->step['services'] : array();
116
117 1
        return new StepServices($this, $services);
118
    }
119
120
    /**
121
     * @return array|string[]
122
     */
123 2
    public function getScript()
124
    {
125 2
        return $this->step['script'];
126
    }
127
128
    /**
129
     * @return array|string[]
130
     */
131 1
    public function getAfterScript()
132
    {
133 1
        if (isset($this->step['after-script'])) {
134 1
            return $this->step['after-script'];
135
        }
136
137 1
        return array();
138
    }
139
140
    /**
141
     * @return bool
142
     */
143 1
    public function isManual()
144
    {
145 1
        if (0 === $this->index) {
146 1
            return false;
147
        }
148
149 1
        return (isset($this->step['trigger']) && 'manual' === $this->step['trigger']);
150
    }
151
152
    /**
153
     * Specify data which should be serialized to JSON
154
     *
155
     * @return array
156
     */
157 1
    public function jsonSerialize()
158
    {
159 1
        $image = $this->getImage();
160 1
        $image = null === $image ? '' : $image->jsonSerialize();
161
162
        return array(
163 1
            'name' => $this->getName(),
164
            'image' => $image,
165 1
            'script' => $this->getScript(),
166 1
            'artifacts' => $this->getArtifacts(),
167
        );
168
    }
169
170
    /**
171
     * @return int
172
     */
173 1
    public function getIndex()
174
    {
175 1
        return $this->index;
176
    }
177
178
    /**
179
     * @return Pipeline
180
     * @codeCoverageIgnore
181
     */
182
    public function getPipeline()
183
    {
184
        return $this->pipeline;
185
    }
186
187
    /**
188
     * @return array step container environment variables (e.g. parallel a step)
189
     */
190 1
    public function getEnv()
191
    {
192 1
        return $this->env;
193
    }
194
195
    /**
196
     * @return \Ktomk\Pipelines\File\File
197
     */
198 1
    public function getFile()
199
    {
200 1
        return $this->pipeline->getFile();
201
    }
202
}
203