Passed
Push — master ( 57b88f...5a4f78 )
by David
03:01
created

Service::setServiceName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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
    private $serviceName = '';
13
    /** @var string */
14
    private $image = '';
15
    /** @var int[] */
16
    private $internalPorts = [];
17
    /** @var string[] */
18
    private $dependsOn = [];
19
    /** @var mixed[] */
20
    private $ports = [];
21
    /** @var mixed[] */
22
    private $labels = [];
23
    /** @var mixed[] */
24
    private $environment = [];
25
    /** @var mixed[] */
26
    private $volumes = [];
27
    /** @var \stdClass */
28
    private $validatorSchema;
29
30
    /**
31
     * Service constructor.
32
     */
33
    public function __construct()
34
    {
35
        $this->validatorSchema = json_decode(file_get_contents(__DIR__ . '/ServiceJsonSchema.json'), false);
36
    }
37
38
    /**
39
     * @param mixed[] $payload
40
     * @return Service
41
     * @throws ServiceException
42
     */
43
    public static function parsePayload(array $payload): Service
44
    {
45
        $service = new Service();
46
        $service->checkValidity($payload);
47
        $service->serviceName = $payload['serviceName'] ?? '';
48
        $s = $payload['service'] ?? array();
49
        if (!empty($s)) {
50
            $service->image = $s['image'] ?? '';
51
            $service->internalPorts = $s['internalPorts'] ?? array();
52
            $service->dependsOn = $s['dependsOn'] ?? array();
53
            $service->ports = $s['ports'] ?? array();
54
            $service->labels = $s['labels'] ?? array();
55
            $service->environment = $s['environment'] ?? array();
56
            $service->volumes = $s['volumes'] ?? array();
57
        }
58
        return $service;
59
    }
60
61
    /**
62
     * Specify data which should be serialized to JSON
63
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
64
     * @return array data which can be serialized by <b>json_encode</b>,
65
     * which is a value of any type other than a resource.
66
     * @since 5.4.0
67
     * @throws ServiceException
68
     */
69
    public function jsonSerialize(): array
70
    {
71
        $array = self::arrayFilterRec(array(
72
            'serviceName' => $this->serviceName,
73
            'service' => array(
74
                'image' => $this->image,
75
                'internalPorts' => $this->internalPorts,
76
                'dependsOn' => $this->dependsOn,
77
                'ports' => $this->ports,
78
                'labels' => $this->labels,
79
                'environment' => $this->environment,
80
                'volumes' => $this->volumes,
81
            )
82
        ));
83
        $this->checkValidity($array);
84
        return $array;
85
    }
86
87
    /**
88
     * @param \stdClass|array|string $data
89
     * @return bool
90
     * @throws ServiceException
91
     */
92
    private function checkValidity($data): bool
93
    {
94
        if (\is_array($data)) {
95
            $data = json_decode(json_encode($data), false);
96
        }
97
        $validator = new Validator();
98
        $result = $validator->dataValidation($data, $this->validatorSchema);
99
        if (!$result->isValid()) {
100
            /** @var ValidationError $vError */
101
            $vError = $result->getFirstError();
102
            throw ServiceException::invalidServiceData($vError);
103
        }
104
        return $result->isValid();
105
    }
106
107
    /**
108
     * Delete all key/value pairs with empty value by recursively using array_filter
109
     * @param array $input
110
     * @return mixed[] array
111
     */
112
    private static function arrayFilterRec(array $input): array
113
    {
114
        foreach ($input as &$value) {
115
            if (\is_array($value)) {
116
                $value = self::arrayFilterRec($value);
117
            }
118
        }
119
        return array_filter($input);
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    public function getServiceName(): string
126
    {
127
        return $this->serviceName;
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    public function getImage(): string
134
    {
135
        return $this->image;
136
    }
137
138
    /**
139
     * @return int[]
140
     */
141
    public function getInternalPorts(): array
142
    {
143
        return $this->internalPorts;
144
    }
145
146
    /**
147
     * @return string[]
148
     */
149
    public function getDependsOn(): array
150
    {
151
        return $this->dependsOn;
152
    }
153
154
    /**
155
     * @return mixed[]
156
     */
157
    public function getPorts(): array
158
    {
159
        return $this->ports;
160
    }
161
162
    /**
163
     * @return mixed[]
164
     */
165
    public function getLabels(): array
166
    {
167
        return $this->labels;
168
    }
169
170
    /**
171
     * @return mixed[]
172
     */
173
    public function getEnvironment(): array
174
    {
175
        return $this->environment;
176
    }
177
178
    /**
179
     * @return mixed[]
180
     */
181
    public function getVolumes(): array
182
    {
183
        return $this->volumes;
184
    }
185
186
    /**
187
     * @param string $serviceName
188
     */
189
    public function setServiceName(string $serviceName): void
190
    {
191
        $this->serviceName = $serviceName;
192
    }
193
194
    /**
195
     * @param string $image
196
     */
197
    public function setImage(string $image): void
198
    {
199
        $this->image = $image;
200
    }
201
202
    /**
203
     * @param int[] $internalPorts
204
     */
205
    public function setInternalPorts(array $internalPorts): void
206
    {
207
        $this->internalPorts = $internalPorts;
208
    }
209
210
    /**
211
     * @param string[] $dependsOn
212
     */
213
    public function setDependsOn(array $dependsOn): void
214
    {
215
        $this->dependsOn = $dependsOn;
216
    }
217
218
    /**
219
     * @param int $internalPort
220
     */
221
    public function addInternalPort(int $internalPort): void
222
    {
223
        $this->internalPorts[] = $internalPort;
224
    }
225
226
    /**
227
     * @param string $dependsOn
228
     */
229
    public function addDependsOn(string $dependsOn): void
230
    {
231
        $this->dependsOn[] = $dependsOn;
232
    }
233
234
    /**
235
     * @param string $source
236
     * @param string $target
237
     */
238
    public function addPort(string $source, string $target): void
239
    {
240
        $this->ports[] = array(
241
            'source' => $source,
242
            'target' => $target,
243
        );
244
    }
245
246
    /**
247
     * @param string $key
248
     * @param string $value
249
     */
250
    public function addLabel(string $key, string $value): void
251
    {
252
        $this->labels[] = array(
253
            'key' => $key,
254
            'values' => $value,
255
        );
256
    }
257
258
    /**
259
     * @param string $key
260
     * @param string $value
261
     */
262
    public function addEnvironment(string $key, string $value): void
263
    {
264
        $this->environment[] = array(
265
            'key' => $key,
266
            'values' => $value,
267
        );
268
    }
269
270
    /**
271
     * @param string $type
272
     * @param string $source
273
     * @param string $target
274
     * @param bool|null $readONly
275
     */
276
    public function addVolume(string $type, string $source, string $target, ?bool $readONly): void
277
    {
278
        $this->volumes[] = array(
279
            'type' => $type,
280
            'source' => $source,
281
            'target' => $target,
282
            'readOnly' => $readONly,
283
        );
284
    }
285
}
286