Completed
Pull Request — master (#2)
by
unknown
04:49
created

Service::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace TheAentMachine\Service;
4
5
use Opis\JsonSchema\ValidationError;
6
use Opis\JsonSchema\Validator;
7
use TheAentMachine\Service\Exception\ServiceException;
8
9
class Service implements \JsonSerializable
10
{
11
    /** @var string */
12
    protected $serviceName;
13
    /** @var string */
14
    protected $image;
15
    /** @var int[]|string[] */
16
    protected $internalPorts;
17
    /** @var string[] */
18
    protected $dependsOn;
19
    /** @var mixed[] */
20
    protected $ports;
21
    /** @var mixed[] */
22
    protected $labels;
23
    /** @var mixed[] */
24
    protected $environment;
25
    /** @var mixed[] */
26
    protected $volumes;
27
    /** @var \stdClass */
28
    protected $validatorSchema;
29
30
    /**
31
     * Service constructor.
32
     */
33
    public function __construct()
34
    {
35
        $this->serviceName = '';
36
        $this->image = '';
37
        $this->internalPorts = array();
38
        $this->dependsOn = array();
39
        $this->ports = array();
40
        $this->labels = array();
41
        $this->environment = array();
42
        $this->volumes = array();
43
        $this->validatorSchema = json_decode(file_get_contents(__DIR__ . '/ServiceJsonSchema.json'), false);
44
    }
45
46
    /**
47
     * @param mixed[] $payload
48
     * @return Service
49
     * @throws ServiceException
50
     */
51
    public static function parsePayload(array $payload): Service
52
    {
53
        $service = new Service();
54
        $service->checkValidity($payload);
55
        $service->serviceName = $payload['serviceName'] ?? '';
56
        $s = $payload['service'] ?? array();
57
        if (!empty($s)) {
58
            $service->image = $s['image'] ?? '';
59
            $service->internalPorts = $s['internalPorts'] ?? array();
60
            $service->dependsOn = $s['dependsOn'] ?? array();
61
            $service->ports = $s['ports'] ?? array();
62
            $service->labels = $s['labels'] ?? array();
63
            $service->environment = $s['environment'] ?? array();
64
            $service->volumes = $s['volumes'] ?? array();
65
        }
66
        return $service;
67
    }
68
69
    /**
70
     * @param \stdClass|array|string $data
71
     * @param bool $throwsException
72
     * @return bool
73
     * @throws ServiceException
74
     */
75
    public function checkValidity($data, bool $throwsException = true): bool
76
    {
77
        if (\is_array($data)) {
78
            $data = json_decode(json_encode($data), false);
79
        }
80
        $validator = new Validator();
81
        $result = $validator->dataValidation($data, $this->validatorSchema);
82
        if ($throwsException && !$result->isValid()) {
83
            /** @var ValidationError $vError */
84
            $vError = $result->getFirstError();
85
            throw ServiceException::invalidServiceData($vError);
86
        }
87
        return $result->isValid();
88
    }
89
90
    /**
91
     * Specify data which should be serialized to JSON
92
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
93
     * @return array data which can be serialized by <b>json_encode</b>,
94
     * which is a value of any type other than a resource.
95
     * @since 5.4.0
96
     * @throws ServiceException
97
     */
98
    public function jsonSerialize(): array
99
    {
100
        $array = Utils::arrayFilterRec(array(
101
            'serviceName' => $this->serviceName,
102
            'service' => array(
103
                'image' => $this->image,
104
                'internalPorts' => $this->internalPorts,
105
                'dependsOn' => $this->dependsOn,
106
                'ports' => $this->ports,
107
                'labels' => $this->labels,
108
                'environment' => $this->environment,
109
                'volumes' => $this->volumes,
110
            )
111
        ));
112
        $this->checkValidity($array);
113
        return $array;
114
    }
115
116
117
    /**
118
     * @param string $serviceName
119
     * @return Service
120
     */
121
    public function setServiceName(string $serviceName): Service
122
    {
123
        $this->serviceName = $serviceName;
124
        return $this;
125
    }
126
127
    /**
128
     * @param string $image
129
     * @return Service
130
     */
131
    public function setImage(string $image): Service
132
    {
133
        $this->image = $image;
134
        return $this;
135
    }
136
137
    /**
138
     * @param int[]|string[] $internalPorts
139
     * @return Service
140
     */
141
    public function setInternalPorts(array $internalPorts): Service
142
    {
143
        $this->internalPorts = $internalPorts;
144
        return $this;
145
    }
146
147
    /**
148
     * @param string[] $dependsOn
149
     * @return Service
150
     */
151
    public function setDependsOn(array $dependsOn): Service
152
    {
153
        $this->dependsOn = $dependsOn;
154
        return $this;
155
    }
156
157
    /**
158
     * @param string[] $ports
159
     * @return Service
160
     */
161
    public function setPorts(array $ports): Service
162
    {
163
        $this->ports = $ports;
164
        return $this;
165
    }
166
167
    /**
168
     * @param string[] $labels
169
     * @return Service
170
     */
171
    public function setLabels(array $labels): Service
172
    {
173
        $this->labels = $labels;
174
        return $this;
175
    }
176
177
    /**
178
     * @param string[] $environment
179
     * @return Service
180
     */
181
    public function setEnvironment(array $environment): Service
182
    {
183
        $this->environment = $environment;
184
        return $this;
185
    }
186
187
    /**
188
     * @param string[] $volumes
189
     * @return Service
190
     */
191
    public function setVolumes(array $volumes): Service
192
    {
193
        $this->volumes = $volumes;
194
        return $this;
195
    }
196
}
197