Passed
Push — test ( 3aeef5...f1b0ee )
by Tom
02:39
created

StepCaches   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 130
ccs 36
cts 36
cp 1
rs 10
wmc 17

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getNames() 0 3 1
A getIterator() 0 3 1
A getFile() 0 3 1
A __construct() 0 8 1
A getDefinitions() 0 7 2
A parseCaches() 0 18 5
A getCacheDefinitionByName() 0 7 3
A verifyCache() 0 9 3
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\File\Pipeline;
6
7
use Ktomk\Pipelines\File\Definitions\Caches;
8
use Ktomk\Pipelines\File\Dom\FileNode;
9
use Ktomk\Pipelines\File\File;
10
use Ktomk\Pipelines\File\ParseException;
11
12
/**
13
 * Class StepCaches
14
 *
15
 * Caches entry in a step
16
 *
17
 * @package Ktomk\Pipelines\File\File
18
 */
19
class StepCaches implements FileNode, \IteratorAggregate
20
{
21
    /**
22
     * @var Step
23
     */
24
    private $step;
25
26
    /**
27
     * @var array
28
     * @psalm-var array<string, int>
29
     */
30
    private $caches;
31
32
    /**
33
     * StepCaches constructor.
34
     *
35
     * @param Step $step
36
     * @param null|array|mixed $caches
37
     *
38
     * @return void
39
     */
40 8
    public function __construct(Step $step, $caches)
41
    {
42 8
        $this->step = $step;
43
44
        // quick validation: script
45 8
        $parsed = $this->parseCaches($caches);
46
47 4
        $this->caches = array_flip($parsed);
48 4
    }
49
50
    /**
51
     * get step caches (as defined)
52
     *
53
     * @return array cache map
54
     */
55 2
    public function getDefinitions()
56
    {
57 2
        if (null === $file = $this->getFile()) {
58 2
            return array();
59
        }
60
61 1
        return $file->getDefinitions()->getCaches()->getByNames($this->getNames());
62
    }
63
64
    /**
65
     * Get all cache names of step
66
     *
67
     * @return array|string[]
68
     */
69 2
    public function getNames()
70
    {
71 2
        return array_keys($this->caches);
72
    }
73
74
    /**
75
     * @return null|File
76
     */
77 2
    public function getFile()
78
    {
79 2
        return $this->step->getFile();
80
    }
81
82
    /**
83
     * @return \ArrayIterator|string[]
84
     */
85 1
    public function getIterator()
86
    {
87 1
        return new \ArrayIterator($this->getDefinitions());
88
    }
89
90
    /**
91
     * parse caches
92
     *
93
     * @param null|array|mixed $caches
94
     *
95
     * @return string[]
96
     */
97 8
    private function parseCaches($caches)
98
    {
99 8
        if (!is_array($caches)) {
100 1
            throw new ParseException("'caches' requires a list of caches");
101
        }
102
103 7
        $reservoir = array();
104 7
        foreach ($caches as $cache) {
105 4
            if (!is_string($cache)) {
106 1
                throw new ParseException("'caches' cache name string expected");
107
            }
108
109 3
            '' === ($cache = trim($cache)) || $reservoir[] = $cache;
110
111 3
            $this->verifyCache($cache);
112
        }
113
114 4
        return $reservoir;
115
    }
116
117
    /**
118
     * @param $cache
119
     *
120
     * @throws ParseException
121
     *
122
     * @return void
123
     */
124 3
    private function verifyCache($cache)
125
    {
126 3
        if ('' === $cache) {
127 1
            throw new ParseException("'caches' cache name must not be empty");
128
        }
129
130 2
        if (null === $this->getCacheDefinitionByName($cache)) {
131 1
            throw new ParseException(
132 1
                sprintf("cache '%s' must reference a custom or default cache definition", $cache)
133
            );
134
        }
135 1
    }
136
137
    /**
138
     * @param string $name
139
     *
140
     * @return null|string|true
141
     */
142 2
    private function getCacheDefinitionByName($name)
143
    {
144 2
        $file = $this->step->getFile();
145
146 2
        $definitions = $file ? $file->getDefinitions()->getCaches() : $file;
0 ignored issues
show
introduced by
$file is of type Ktomk\Pipelines\File\File, thus it always evaluated to true.
Loading history...
147
148 2
        return $definitions ? $definitions->getByName($name) : null;
0 ignored issues
show
introduced by
$definitions is of type Ktomk\Pipelines\File\Definitions\Caches, thus it always evaluated to true.
Loading history...
149
    }
150
}
151