StepCaches   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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