Passed
Push — master ( 71afca...9e5fe0 )
by Tom
04:18
created

StepCaches   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 83
ccs 22
cts 22
cp 1
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parseCaches() 0 16 5
A getIterator() 0 3 1
A getFile() 0 3 1
A __construct() 0 7 1
A getDefinitions() 0 7 2
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(array_keys($this->caches));
61
    }
62
63
    /**
64
     * @return null|File
65
     */
66 2
    public function getFile()
67
    {
68 2
        return $this->step->getFile();
69
    }
70
71
    /**
72
     * @return \ArrayIterator|string[]
73
     */
74 1
    public function getIterator()
75
    {
76 1
        return new \ArrayIterator($this->getDefinitions());
77
    }
78
79
    /**
80
     * parse caches
81
     *
82
     * @param null|array|mixed $caches
83
     *
84
     * @return string[]
85
     */
86 5
    private function parseCaches($caches)
87
    {
88 5
        if (!is_array($caches)) {
89 1
            throw new ParseException("'caches' requires a list of caches");
90
        }
91
92 4
        $reservoir = array();
93 4
        foreach ($caches as $cache) {
94 1
            if (!is_string($cache)) {
95 1
                throw new ParseException("'caches' cache name string expected");
96
            }
97
98 1
            '' === ($cache = trim($cache)) || $reservoir[] = $cache;
99
        }
100
101 3
        return $reservoir;
102
    }
103
}
104