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\GetFareFamilyListParameters;
8
9
class GetFareFamilyListParametersFactory implements ValueObjectFactoryInterface
10
{
11
    /**
12
     * @param array $parameters
13
     * @return GetFareFamilyListParameters
14
     * @throws \Exception
15
     */
16
    public static function createFromArray(array $parameters) : GetFareFamilyListParameters
17
    {
18
        $getFareFamilyListParameters = new GetFareFamilyListParameters();
19
        foreach ($parameters['portList'] as $airportIataCode) {
20
            $getFareFamilyListParameters->withAirportIataCode($airportIataCode);
21
        }
22
        if (array_key_exists('isMilesRequest', $parameters) && $parameters['isMilesRequest'] === 'T') {
23
            $getFareFamilyListParameters->withMilesRequest();
24
        }
25
        return  $getFareFamilyListParameters;
26
    }
27
28
29
    /**
30
     * @param string $json
31
     * @return GetFareFamilyListParameters
32
     * @throws InvalidArgumentException
33
     * @throws \Exception
34
     */
35
    public static function createFromJson(string $json) : GetFareFamilyListParameters
36
    {
37
        $parameters = json_decode($json, (bool) JSON_OBJECT_AS_ARRAY);
38
        if (json_last_error() !== JSON_ERROR_NONE) {
39
            throw new InvalidArgumentException(
40
                'GetFareFamilyListParametersFactory Error: ' . json_last_error_msg()
41
            );
42
        }
43
        return self::createFromArray($parameters);
44
    }
45
}
46