GetPortListParametersFactory::createFromJson()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TK\API\ValueObject\Factory;
5
6
use TK\API\Exception\InvalidArgumentException;
7
use TK\API\ValueObject\GetPortListParameters;
8
9
final class GetPortListParametersFactory implements ValueObjectFactoryInterface
10
{
11
    /**
12
     * @param array $parameters
13
     * @return GetPortListParameters
14
     * @throws \Exception
15
     */
16
    public static function createFromArray(array $parameters) : GetPortListParameters
17
    {
18
        $getPortListParameters = new GetPortListParameters($parameters['airlineCode']);
19
        if (array_key_exists('languageCode', $parameters)) {
20
            $getPortListParameters = $getPortListParameters->withLanguageCode($parameters['languageCode']);
21
        }
22
        return $getPortListParameters;
23
    }
24
25
26
    /**
27
     * @param string $json
28
     * @return GetPortListParameters
29
     * @throws InvalidArgumentException
30
     * @throws \Exception
31
     */
32
    public static function createFromJson(string $json) : GetPortListParameters
33
    {
34
        $parameters = json_decode($json, (bool) JSON_OBJECT_AS_ARRAY);
35
        if (json_last_error() !== JSON_ERROR_NONE) {
36
            throw new InvalidArgumentException(
37
                'GetPortListParametersFactory Error: ' . json_last_error_msg()
38
            );
39
        }
40
        return self::createFromArray($parameters);
41
    }
42
}
43