Passed
Push — master ( 9e5fe0...88d9d6 )
by Tom
04:35
created

StepCaches   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 127
ccs 34
cts 34
cp 1
rs 10
c 1
b 0
f 0
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getNames() 0 3 1
A getFile() 0 3 1
A __construct() 0 8 1
A getDefinitions() 0 7 2
A parseCaches() 0 18 5
A getIterator() 0 3 1
A getCacheDefinitionByName() 0 3 1
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
     * @psalm-return \ArrayIterator<array-key, string>
85
     */
86 1
    public function getIterator()
87
    {
88 1
        return new \ArrayIterator($this->getDefinitions());
89
    }
90
91
    /**
92
     * parse caches
93
     *
94
     * @param null|array|mixed $caches
95
     *
96
     * @return string[]
97
     */
98 8
    private function parseCaches($caches)
99
    {
100 8
        if (!is_array($caches)) {
101 1
            throw new ParseException("'caches' requires a list of caches");
102
        }
103
104 7
        $reservoir = array();
105 7
        foreach ($caches as $cache) {
106 4
            if (!is_string($cache)) {
107 1
                throw new ParseException("'caches' cache name string expected");
108
            }
109
110 3
            '' === ($cache = trim($cache)) || $reservoir[] = $cache;
111
112 3
            $this->verifyCache($cache);
113
        }
114
115 4
        return $reservoir;
116
    }
117
118
    /**
119
     * @param string $cache
120
     *
121
     * @throws ParseException
122
     *
123
     * @return void
124
     */
125 3
    private function verifyCache($cache)
126
    {
127 3
        if ('' === $cache) {
128 1
            throw new ParseException("'caches' cache name must not be empty");
129
        }
130
131 2
        if (null === $this->getCacheDefinitionByName($cache)) {
132 1
            throw new ParseException(
133 1
                sprintf("cache '%s' must reference a custom or default cache definition", $cache)
134
            );
135
        }
136 1
    }
137
138
    /**
139
     * @param string $name
140
     *
141
     * @return null|string|true
142
     */
143 2
    private function getCacheDefinitionByName($name)
144
    {
145 2
        return $this->step->getFile()->getDefinitions()->getCaches()->getByName($name);
146
    }
147
}
148