GetFareFamilyListParametersFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromArray() 0 10 4
A createFromJson() 0 9 2
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