Passed
Push — master ( 3b22a1...94154d )
by Tom
03:44
created

Image::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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 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
     * @return ImageName image name
45
     */
46 2
    public function getName()
47
    {
48 2
        return $this->name;
49
    }
50
51
    /**
52
     * @return Properties properties additional to name
53
     */
54 1
    public function getProperties()
55
    {
56 1
        return $this->properties;
57
    }
58
59
    /**
60
     * Specify data which should be serialized to JSON
61
     *
62
     * @return array
63
     */
64 1
    public function jsonSerialize()
65
    {
66
        return array(
67 1
            'name' => (string)$this->getName()
68
        );
69
    }
70
71
    /**
72
     * @param array|string $image
73
     * @throws \Ktomk\Pipelines\File\ParseException
74
     */
75 10
    private function parse($image)
76
    {
77 10
        if (is_string($image)) {
78 3
            $this->parseString($image);
79 7
        } elseif (is_array($image)) {
0 ignored issues
show
introduced by
The condition is_array($image) is always true.
Loading history...
80 6
            $this->parseArray($image);
81
        } else {
82 1
            ParseException::__(
83 1
                "'image' expects either 'a string' or 'a section'"
84
            );
85
        }
86 6
    }
87
88
    /**
89
     * @param string $name
90
     * @throws \Ktomk\Pipelines\File\ParseException
91
     */
92 8
    private function parseString($name)
93
    {
94 8
        if (!ImageName::validate($name)) {
95 1
            ParseException::__(
96 1
                sprintf("'image' invalid Docker image name: '%s'", $name)
97
            );
98
        }
99
100 7
        $this->name = new ImageName($name);
101 7
    }
102
103
    /**
104
     * @param array $image
105
     * @throws \Ktomk\Pipelines\File\ParseException
106
     */
107 6
    private function parseArray(array $image)
108
    {
109 6
        if (!isset($image['name'])) {
110 1
            ParseException::__("'image' needs a name");
111
        }
112 5
        $this->parseString($image['name']);
113 5
        unset($image['name']);
114
115 5
        $entries = array('run-as-user', 'username', 'password', 'email', 'aws');
116 5
        $image = $this->properties->import($image, $entries);
117
118 5
        if (!empty($image)) {
119 1
            ParseException::__(sprintf(
120 1
                "unknown 'image' property '%s', expects either a string or a section",
121 1
                key($image)
122
            ));
123
        }
124 4
    }
125
}
126