Passed
Push — master ( 0cfc7e...b88fc1 )
by Tom
03:33 queued 42s
created

Image::parseArray()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 8
nop 1
dl 0
loc 16
ccs 12
cts 12
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\File;
6
7
/**
8
 * Class Image
9
 *
10
 * @package Ktomk\Pipelines\File
11
 */
12
class Image
13
{
14
    /**
15
     * @var ImageName
16
     */
17
    private $name;
18
19
    /**
20
     * @var array
21
     */
22
    private $properties;
23
24
    /**
25
     * Image constructor.
26
     *
27
     * @param string|array $image
28
     */
29 8
    public function __construct($image)
30
    {
31 8
        $this->parse($image);
32 4
    }
33
34
    /**
35
     * @param string|array $image
36
     */
37 8
    private function parse($image)
38
    {
39 8
        if (is_string($image)) {
40 3
            $this->parseString($image);
41 5
        } elseif (is_array($image)) {
0 ignored issues
show
introduced by
The condition is_array($image) can never be false.
Loading history...
42 4
            $this->parseArray($image);
43
        } else {
44 1
            ParseException::__(
45 1
                "'image' expects either 'a string' or 'a section'"
46
            );
47
        }
48 4
    }
49
50
    /**
51
     * @param string $name
52
     */
53 6
    private function parseString($name)
54
    {
55 6
        if (!ImageName::validate($name)) {
56 1
            ParseException::__(
57 1
                sprintf("'image' invalid Docker image name: '%s'", $name)
58
            );
59
        }
60
61 5
        $this->name = new ImageName($name);
62 5
        $this->properties = array();
63 5
    }
64
65 4
    private function parseArray(array $image)
66
    {
67 4
        if (!isset($image['name'])) {
68 1
            ParseException::__("'image' needs a name");
69
        }
70 3
        $this->parseString($image['name']);
71 3
        unset($image['name']);
72 3
        $entries = array('username', 'password', 'email', 'aws');
73 3
        foreach ($entries as $key) {
74 3
            $image = $this->parseArrayEntry($image, $key);
75
        }
76
77 3
        if (!empty($image)) {
78 1
            ParseException::__(sprintf(
79 1
                "unknown 'image' property '%s', expects either a string or a section",
80 1
                key($image)
81
            ));
82
        }
83 2
    }
84
85 3
    private function parseArrayEntry(array $image, $key)
86
    {
87 3
        if (isset($image[$key])) {
88 1
            $this->properties[$key] = $image[$key];
89
        }
90 3
        unset($image[$key]);
91 3
        return $image;
92
    }
93
94
    /**
95
     * @return ImageName image name
96
     */
97 1
    public function getName()
98
    {
99 1
        return $this->name;
100
    }
101
102 1
    public function __toString()
103
    {
104 1
        return (string)$this->name;
105
    }
106
}
107