Passed
Push — test ( 6e7932...65a187 )
by Tom
02:39
created

StepCaches::getFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 7
    public function __construct(Step $step, $caches)
41
    {
42 7
        $this->step = $step;
43
44
        // quick validation: script
45 7
        $parsed = $this->parseCaches($caches);
46
47 3
        $this->caches = array_flip($parsed);
48 3
    }
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 1
    public function getNames()
70
    {
71 1
        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 7
    private function parseCaches($caches)
98
    {
99 7
        if (!is_array($caches)) {
100 1
            throw new ParseException("'caches' requires a list of caches");
101
        }
102
103 6
        $reservoir = array();
104 6
        foreach ($caches as $cache) {
105 3
            if (!is_string($cache)) {
106 1
                throw new ParseException("'caches' cache name string expected");
107
            }
108
109 2
            '' === ($cache = trim($cache)) || $reservoir[] = $cache;
110
111 2
            if ('' === $cache) {
112 1
                throw new ParseException("'caches' cache name must not be empty");
113
            }
114
115 1
            if (null === $this->getCacheDefinitionByName($cache)) {
116 1
                throw new ParseException(
117 1
                    sprintf("cache '%s' must reference a custom or default cache definition", $cache)
118
                );
119
            }
120
        }
121
122 3
        return $reservoir;
123
    }
124
125
    /**
126
     * @param string $name
127
     *
128
     * @return null|string|true
129
     */
130 1
    private function getCacheDefinitionByName($name)
131
    {
132 1
        $file = $this->step->getFile();
133
134 1
        $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...
135
136 1
        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...
137
    }
138
}
139