Passed
Branch test (2117b7)
by Tom
02:24
created

Image::parseArray()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 1
dl 0
loc 15
ccs 11
cts 11
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
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 string|array $image
30
     */
31 9
    public function __construct($image)
32
    {
33 9
        $this->properties = new Properties();
34 9
        $this->parse($image);
35 5
    }
36
37
    /**
38
     * @param string|array $image
39
     */
40 9
    private function parse($image)
41
    {
42 9
        if (is_string($image)) {
43 3
            $this->parseString($image);
44 6
        } elseif (is_array($image)) {
0 ignored issues
show
introduced by
The condition is_array($image) can never be false.
Loading history...
45 5
            $this->parseArray($image);
46
        } else {
47 1
            ParseException::__(
48 1
                "'image' expects either 'a string' or 'a section'"
49
            );
50
        }
51 5
    }
52
53
    /**
54
     * @param string $name
55
     */
56 7
    private function parseString($name)
57
    {
58 7
        if (!ImageName::validate($name)) {
59 1
            ParseException::__(
60 1
                sprintf("'image' invalid Docker image name: '%s'", $name)
61
            );
62
        }
63
64 6
        $this->name = new ImageName($name);
65 6
    }
66
67 5
    private function parseArray(array $image)
68
    {
69 5
        if (!isset($image['name'])) {
70 1
            ParseException::__("'image' needs a name");
71
        }
72 4
        $this->parseString($image['name']);
73 4
        unset($image['name']);
74
75 4
        $entries = array('username', 'password', 'email', 'aws');
76 4
        $image = $this->properties->import($image, $entries);
77
78 4
        if (!empty($image)) {
79 1
            ParseException::__(sprintf(
80 1
                "unknown 'image' property '%s', expects either a string or a section",
81 1
                key($image)
82
            ));
83
        }
84 3
    }
85
86
    /**
87
     * @return ImageName image name
88
     */
89 1
    public function getName()
90
    {
91 1
        return $this->name;
92
    }
93
94 1
    public function __toString()
95
    {
96 1
        return (string)$this->name;
97
    }
98
99
    /**
100
     * @return Properties properties additional to name
101
     */
102 1
    public function getProperties()
103
    {
104 1
        return $this->properties;
105
    }
106
}
107