Passed
Push — test ( 72c582...a158b4 )
by Tom
02:24
created

Image   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 144
ccs 53
cts 53
cp 1
rs 10
c 0
b 0
f 0
wmc 20

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 3 1
B validate() 0 25 7
A getProperties() 0 3 1
A getName() 0 3 1
A parseArray() 0 15 3
A jsonSerialize() 0 4 1
A parse() 0 9 3
A parseString() 0 9 2
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\File;
6
7
use Ktomk\Pipelines\Value\Properties;
8
9
/**
10
 * Class Image
11
 *
12
 * @package Ktomk\Pipelines\File
13
 */
14
class Image
15
{
16
    /**
17
     * @var ImageName
18
     */
19
    private $name;
20
21
    /**
22
     * @var Properties
23
     */
24
    private $properties;
25
26
    /**
27
     * Image constructor.
28
     *
29
     * @param array|string $image
30
     * @throws \Ktomk\Pipelines\File\ParseException
31
     */
32 10
    public function __construct($image)
33
    {
34 10
        $this->properties = new Properties();
35 10
        $this->parse($image);
36 6
    }
37
38 1
    public function __toString()
39
    {
40 1
        return (string)$this->name;
41
    }
42
43
    /**
44
     * if an 'image' entry is set, validate it is a string or a section.
45
     *
46
     * @param array $array
47
     * @throw ParseException if the image name is invalid
48
     * @throws \Ktomk\Pipelines\File\ParseException
49
     */
50 7
    public static function validate(array $array)
51
    {
52 7
        if (!array_key_exists('image', $array)) {
53 1
            return;
54
        }
55
56 6
        $image = $array['image'];
57
58 6
        if (is_array($image) && isset($image['name'])) {
59 2
            if (!ImageName::validate($image['name'])) {
60 1
                ParseException::__(sprintf(
61 1
                    "'image' invalid Docker image name: '%s'",
62 1
                    $image['name']
63
                ));
64
            }
65
66 1
            return;
67
        }
68
69 4
        if (!is_string($image)) {
70 2
            ParseException::__("'image' requires a Docker image name");
71
        }
72 2
        if (!ImageName::validate($image)) {
73 1
            ParseException::__(
74 1
                sprintf("'image' invalid Docker image name: '%s'", $image)
75
            );
76
        }
77 1
    }
78
79
    /**
80
     * @return ImageName image name
81
     */
82 2
    public function getName()
83
    {
84 2
        return $this->name;
85
    }
86
87
    /**
88
     * @return Properties properties additional to name
89
     */
90 1
    public function getProperties()
91
    {
92 1
        return $this->properties;
93
    }
94
95
    /**
96
     * Specify data which should be serialized to JSON
97
     *
98
     * @return array
99
     */
100 1
    public function jsonSerialize()
101
    {
102
        return array(
103 1
            'name' => (string)$this->getName()
104
        );
105
    }
106
107
    /**
108
     * @param array|string $image
109
     * @throws \Ktomk\Pipelines\File\ParseException
110
     */
111 10
    private function parse($image)
112
    {
113 10
        if (is_string($image)) {
114 3
            $this->parseString($image);
115 7
        } elseif (is_array($image)) {
0 ignored issues
show
introduced by
The condition is_array($image) is always true.
Loading history...
116 6
            $this->parseArray($image);
117
        } else {
118 1
            ParseException::__(
119 1
                "'image' expects either 'a string' or 'a section'"
120
            );
121
        }
122 6
    }
123
124
    /**
125
     * @param string $name
126
     * @throws \Ktomk\Pipelines\File\ParseException
127
     */
128 8
    private function parseString($name)
129
    {
130 8
        if (!ImageName::validate($name)) {
131 1
            ParseException::__(
132 1
                sprintf("'image' invalid Docker image name: '%s'", $name)
133
            );
134
        }
135
136 7
        $this->name = new ImageName($name);
137 7
    }
138
139
    /**
140
     * @param array $image
141
     * @throws \Ktomk\Pipelines\File\ParseException
142
     */
143 6
    private function parseArray(array $image)
144
    {
145 6
        if (!isset($image['name'])) {
146 1
            ParseException::__("'image' needs a name");
147
        }
148 5
        $this->parseString($image['name']);
149 5
        unset($image['name']);
150
151 5
        $entries = array('run-as-user', 'username', 'password', 'email', 'aws');
152 5
        $image = $this->properties->import($image, $entries);
153
154 5
        if (!empty($image)) {
155 1
            ParseException::__(sprintf(
156 1
                "unknown 'image' property '%s', expects either a string or a section",
157 1
                key($image)
158
            ));
159
        }
160 4
    }
161
}
162