Passed
Push — test ( 2956c8...b121f6 )
by Tom
03:07
created

StepCondition::getIncludePaths()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 1
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\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 6
        $count = 0;
53 6
        foreach ($includePaths as $index => $path) {
54 6
            if ($index !== $count) {
55 1
                throw new ParseException('Condition "changesets" "includePaths" must be a list');
56
            }
57 5
            if (!is_string($path)) {
58 2
                throw new ParseException(
59 2
                    sprintf(
60
                        'Condition "changesets" "includePaths" must be string at index #%d, got %s (%s)',
61
                        $index,
62 2
                        gettype($path),
63 2
                        (string)$path
64
                    )
65
                );
66
            }
67 4
            if ('' === $path) {
68 1
                throw new ParseException(
69 1
                    sprintf(
70
                        'Condition "changesets" "includePaths" empty path at index #%d',
71
                        $index
72
                    )
73
                );
74
            }
75 3
            $this->includePaths[$count] = $path;
76 3
            $count++;
77
        }
78
    }
79
}
80