Passed
Push — test ( 9e5fe0...6e7932 )
by Tom
02:50
created

StepCaches::getNames()   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 0
Metric Value
cc 1
eloc 1
c 0
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 5
    public function __construct(Step $step, $caches)
41
    {
42
        // quick validation: script
43 5
        $parsed = $this->parseCaches($caches);
44
45 3
        $this->step = $step;
46 3
        $this->caches = array_flip($parsed);
47 3
    }
48
49
    /**
50
     * get step caches (as defined)
51
     *
52
     * @return array cache map
53
     */
54 2
    public function getDefinitions()
55
    {
56 2
        if (null === $file = $this->getFile()) {
57 2
            return array();
58
        }
59
60 1
        return $file->getDefinitions()->getCaches()->getByNames($this->getNames());
61
    }
62
63
    /**
64
     * Get all cache names of step
65
     *
66
     * @return array|string[]
67
     */
68 1
    public function getNames()
69
    {
70 1
        return array_keys($this->caches);
71
    }
72
73
    /**
74
     * @return null|File
75
     */
76 2
    public function getFile()
77
    {
78 2
        return $this->step->getFile();
79
    }
80
81
    /**
82
     * @return \ArrayIterator|string[]
83
     */
84 1
    public function getIterator()
85
    {
86 1
        return new \ArrayIterator($this->getDefinitions());
87
    }
88
89
    /**
90
     * parse caches
91
     *
92
     * @param null|array|mixed $caches
93
     *
94
     * @return string[]
95
     */
96 5
    private function parseCaches($caches)
97
    {
98 5
        if (!is_array($caches)) {
99 1
            throw new ParseException("'caches' requires a list of caches");
100
        }
101
102 4
        $reservoir = array();
103 4
        foreach ($caches as $cache) {
104 1
            if (!is_string($cache)) {
105 1
                throw new ParseException("'caches' cache name string expected");
106
            }
107
108 1
            '' === ($cache = trim($cache)) || $reservoir[] = $cache;
109
        }
110
111 3
        return $reservoir;
112
    }
113
}
114