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

StepParser   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 107
ccs 32
cts 32
cp 1
rs 10
c 1
b 0
f 0
wmc 20

7 Methods

Rating   Name   Duplication   Size   Complexity  
A validateTrigger() 0 13 4
A parseNamedScriptLine() 0 10 6
A parseNamedScript() 0 8 4
A __construct() 0 11 1
A parseScript() 0 6 2
A parseAfterScript() 0 4 2
A validate() 0 3 1
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\File\Pipeline;
6
7
use Ktomk\Pipelines\File\Image;
8
use Ktomk\Pipelines\File\ParseException;
9
10
class StepParser
11
{
12 22
    public static function validate(array $step, array $env)
13
    {
14 22
        $parser = new StepParser($step, $env);
0 ignored issues
show
Unused Code introduced by
The assignment to $parser is dead and can be removed.
Loading history...
15
    }
16
17 22
    private function __construct(array $step, array $env)
18
    {
19
        // quick validation: image name
20 22
        Image::validate($step);
21
22
        // quick validation: trigger
23 21
        $this->validateTrigger($step, (bool)$env);
24
25
        // quick validation: script + after-script
26 21
        $this->parseScript($step);
27 18
        $this->parseAfterScript($step);
28
    }
29
30
    /**
31
     * validate step trigger (none, manual, automatic)
32
     *
33
     * @param array $array
34
     * @param bool $isParallelStep
35
     *
36
     * @return void
37
     */
38 21
    private function validateTrigger(array $array, $isParallelStep)
39
    {
40 21
        if (!array_key_exists('trigger', $array)) {
41 21
            return;
42
        }
43
44 2
        $trigger = $array['trigger'];
45 2
        if ($isParallelStep) {
46 1
            throw new ParseException("Unexpected property 'trigger' in parallel step");
47
        }
48
49 2
        if (!in_array($trigger, array('manual', 'automatic'), true)) {
50 1
            throw new ParseException("'trigger' expects either 'manual' or 'automatic'");
51
        }
52
    }
53
54
    /**
55
     * Parse a step script section
56
     *
57
     * @param array $step
58
     *
59
     * @throws ParseException
60
     *
61
     * @return void
62
     */
63 21
    private function parseScript(array $step)
64
    {
65 21
        if (!isset($step['script'])) {
66 1
            throw new ParseException("'step' requires a script");
67
        }
68 20
        $this->parseNamedScript('script', $step);
69
    }
70
71
    /**
72
     * @param array $step
73
     *
74
     * @return void
75
     */
76 18
    private function parseAfterScript(array $step)
77
    {
78 18
        if (isset($step['after-script'])) {
79 1
            $this->parseNamedScript('after-script', $step);
80
        }
81
    }
82
83
    /**
84
     * @param string $name
85
     * @param $script
86
     *
87
     * @return void
88
     */
89 20
    private function parseNamedScript($name, array $script)
90
    {
91 20
        if (!is_array($script[$name]) || !count($script[$name])) {
92 1
            throw new ParseException("'${name}' requires a list of commands");
93
        }
94
95 19
        foreach ($script[$name] as $index => $line) {
96 19
            $this->parseNamedScriptLine($name, $index, $line);
97
        }
98
    }
99
100
    /**
101
     * @param string $name
102
     * @param int $index
103
     * @param null|array|bool|float|int|string $line
104
     *
105
     * @return void
106
     */
107 19
    private function parseNamedScriptLine($name, $index, $line)
108
    {
109 19
        $standard = is_scalar($line) || null === $line;
110 19
        $pipe = is_array($line) && isset($line['pipe']) && is_string($line['pipe']);
111
112 19
        if (!($standard || $pipe)) {
113 1
            throw new ParseException(sprintf(
114
                "'%s' requires a list of commands, step #%d is not a command",
115
                $name,
116
                $index
117
            ));
118
        }
119
    }
120
}
121