Passed
Push — master ( 0cfc7e...b88fc1 )
by Tom
03:33 queued 42s
created

Step::parseScript()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 10
nc 12
nop 1
dl 0
loc 15
ccs 11
cts 11
cp 1
crap 7
rs 8.2222
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 Pipeline
20
     */
21
    private $pipeline;
22
23
    /**
24
     * Step constructor.
25
     * @param Pipeline $pipeline
26
     * @param array $step
27
     */
28 13
    public function __construct(Pipeline $pipeline, array $step)
29
    {
30
        // quick validation: image name
31 13
        File::validateImage($step);
32
33
        // quick validation: script
34 12
        $this->parseScript($step);
35
36 9
        $this->pipeline = $pipeline;
37 9
        $this->step = $step;
38 9
    }
39
40
    /**
41
     * Parse a step script section
42
     *
43
     * @param array $step
44
     */
45 12
    private function parseScript(array $step)
46
    {
47 12
        if (!isset($step['script'])) {
48 1
            ParseException::__("'step' requires a script");
49
        }
50 11
        if (!count($step['script']) || !is_array($step['script'])) {
51 1
            ParseException::__("'script' requires a list of commands");
52
        }
53
54 10
        foreach ($step['script'] as $index => $line) {
55 10
            if (!is_scalar($line) && !is_null($line)) {
56 1
                ParseException::__(
57 1
                    sprintf(
58 1
                        "'script' requires a list of commands, step #%d is not a command",
59 10
                        $index
60
                    )
61
                );
62
            }
63
        }
64 9
    }
65
66
    /**
67
     * @return Artifacts|null
68
     */
69 2
    public function getArtifacts()
70
    {
71 2
        return isset($this->step['artifacts'])
72 1
            ? new Artifacts($this->step['artifacts'])
73 2
            : null;
74
    }
75
76
    /**
77
     * @return Image
78
     */
79 2
    public function getImage()
80
    {
81 2
        return isset($this->step['image'])
82 1
            ? new Image($this->step['image'])
83 2
            : $this->pipeline->getFile()->getImage();
84
    }
85
86
    /**
87
     * @return string|null
88
     */
89 2
    public function getName()
90
    {
91 2
        return isset($this->step['name'])
92 1
            ? (string)$this->step['name']
93 2
            : null;
94
    }
95
96
    /**
97
     * @return array|string[]
98
     */
99 1
    public function getScript()
100
    {
101 1
        return $this->step['script'];
102
    }
103
}
104