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

Caches::parse()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 19
ccs 7
cts 7
cp 1
crap 5
rs 9.6111
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 7
    public function __construct(array $array)
27
    {
28 7
        $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 7
        $this->parse($array);
40 4
    }
41
42
    /**
43
     * @param array $names
44
     *
45
     * @return array cache map
46
     */
47 3
    public function getByNames(array $names)
48
    {
49 3
        $reservoir = array();
50 3
        $mapMode = $this->map + $this->predefined;
51 3
        unset($mapMode['docker']); // docker cache is system-wide in pipelines
52 3
        foreach ($names as $name) {
53 3
            if (!isset($mapMode[$name])) {
54 1
                continue;
55
            }
56 3
            $reservoir[$name] = $mapMode[$name];
57 3
            unset($mapMode[$name]);
58
        }
59
60 3
        return $reservoir;
61
    }
62
63
    /**
64
     * @param array $array
65
     *
66
     * @return void
67
     */
68 7
    private function parse(array $array)
69
    {
70 7
        foreach ($array as $name => $path) {
71 5
            if (!is_string($name)) {
72 1
                throw new ParseException("cache definition invalid cache name: ${name}");
73
            }
74
75 4
            if (null === $path) {
76 1
                throw new ParseException("cache '${name}' should be a string value (it is currently null or empty)");
77
            }
78
79
            if (is_bool($path)) {
80
                throw new ParseException("cache '${name}' should be a string (it is currently defined as a boolean)");
81
            }
82
83
            // Fixme(tk): more importantly is that $path is not array or object
84
        }
85
86 4
        $this->map = $array;
87 4
    }
88
}
89