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

Caches::getByName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 3
rs 10
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\File\Definitions;
6
7
use Ktomk\Pipelines\File\ParseException;
8
9
class Caches
10
{
11
    /**
12
     * @var array
13
     */
14
    private $predefined = array();
15
16
    /**
17
     * @var array
18
     */
19
    private $map = array();
20
21
    /**
22
     * Caches constructor.
23
     *
24
     * @param array $array
25
     */
26 8
    public function __construct(array $array)
27
    {
28 8
        $this->predefined = array(
29
            'composer' => '~/.composer/cache',
30
            'dotnetcore' => '~/.nuget/packages',
31
            'gradle' => '~/.gradle/caches',
32
            'ivy2' => '~/.ivy2/cache',
33
            'maven' => '~/.m2/repository',
34
            'node' => 'node_modules',
35
            'pip' => '~/.cache/pip',
36
            'sbt' => '~/.sbt',
37
        );
38
39 8
        $this->parse($array);
40 5
    }
41
42
    /**
43
     * @param string $name
44
     *
45
     * @return null|string|true path of custom or default cache definition, true for internal caches and null for no definition
46
     */
47 1
    public function getByName($name)
48
    {
49
        // docker cache is system-wide in pipelines
50 1
        if ('docker' === $name) {
51 1
            return true;
52
        }
53
54 1
        $mapMode = $this->map + $this->predefined;
55
56 1
        if (isset($mapMode[$name])) {
57 1
            return $mapMode[$name];
58
        }
59
60 1
        return null;
61
    }
62
63
    /**
64
     * @param array $names
65
     *
66
     * @return array cache map
67
     */
68 3
    public function getByNames(array $names)
69
    {
70 3
        $reservoir = array();
71
72 3
        $mapMode = $this->map + $this->predefined;
73 3
        unset($mapMode['docker']); // docker cache is system-wide in pipelines
74
75 3
        foreach ($names as $name) {
76 3
            if (!isset($mapMode[$name])) {
77 1
                continue;
78
            }
79 3
            $reservoir[$name] = $mapMode[$name];
80 3
            unset($mapMode[$name]);
81
        }
82
83 3
        return $reservoir;
84
    }
85
86
    /**
87
     * @param array $array
88
     *
89
     * @return void
90
     */
91 8
    private function parse(array $array)
92
    {
93 8
        foreach ($array as $name => $path) {
94 6
            if (!is_string($name)) {
95 1
                throw new ParseException("cache definition invalid cache name: ${name}");
96
            }
97
98 5
            if (null === $path) {
99 1
                throw new ParseException("cache '${name}' should be a string value (it is currently null or empty)");
100
            }
101
102
            if (is_bool($path)) {
103
                throw new ParseException("cache '${name}' should be a string (it is currently defined as a boolean)");
104
            }
105
106
            // Fixme(tk): more importantly is that $path is not array or object
107
        }
108
109 5
        $this->map = $array;
110 5
    }
111
}
112