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

StepCondition::parseIncludePaths()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 18
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 27
ccs 15
cts 15
cp 1
crap 5
rs 9.3554
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\File\Pipeline;
6
7
use Ktomk\Pipelines\File\ParseException;
8
9
final class StepCondition
10
{
11
    /**
12
     * @var array pipeline definition
13
     */
14
    private $array;
15
16
    /**
17
     * @param array<string>
18
     */
19
    private $includePaths;
20
21
    /**
22
     * @param array $definition
23
     */
24 12
    public function __construct(array $definition)
25
    {
26 12
        $this->parseCondition($definition);
27
    }
28
29
    /**
30
     * @return array<string>
31
     */
32 2
    public function getIncludePaths()
33
    {
34 2
        return $this->includePaths ?: array();
35
    }
36
37 12
    private function parseCondition(array $definition)
38
    {
39 12
        $this->array = array();
40
41 12
        if (!isset($definition['changesets']) || !is_array($definition['changesets'])) {
42 3
            throw new ParseException('Condition with no "changesets"');
43
        }
44 9
        $changeSets = $definition['changesets'];
45 9
        if (!isset($changeSets['includePaths']) || !is_array($changeSets['includePaths'])) {
46 2
            throw new ParseException('Condition "changesets" with no "includePaths"');
47
        }
48 7
        $includePaths = $changeSets['includePaths'];
49 7
        if (1 > count($includePaths)) {
50 1
            throw new ParseException('Condition "changesets" "includePaths" must not be empty');
51
        }
52
53 6
        $this->parseIncludePaths($includePaths);
54
    }
55
56 6
    private function parseIncludePaths(array $includePaths)
57
    {
58 6
        $count = 0;
59 6
        foreach ($includePaths as $index => $path) {
60 6
            if ($index !== $count) {
61 1
                throw new ParseException('Condition "changesets" "includePaths" must be a list');
62
            }
63 5
            if (!is_string($path)) {
64 2
                throw new ParseException(
65 2
                    sprintf(
66
                        'Condition "changesets" "includePaths" must be string at index #%d, got %s (%s)',
67
                        $index,
68 2
                        gettype($path),
69 2
                        (string)$path
70
                    )
71
                );
72
            }
73 4
            if ('' === $path) {
74 1
                throw new ParseException(
75 1
                    sprintf(
76
                        'Condition "changesets" "includePaths" empty path at index #%d',
77
                        $index
78
                    )
79
                );
80
            }
81 3
            $this->includePaths[$count] = $path;
82 3
            $count++;
83
        }
84
    }
85
}
86