createFromArray()   B
last analyzed

Complexity

Conditions 7
Paths 24

Size

Total Lines 51
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 37
nc 24
nop 1
dl 0
loc 51
rs 8.3946
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace TK\API\ValueObject\Factory;
5
6
use TK\API\ValueObject\GetAvailabilityParameters;
7
use TK\API\ValueObject\Location;
8
use TK\API\ValueObject\DepartureDateTime;
9
use TK\API\ValueObject\OriginDestinationInformation;
10
use TK\API\ValueObject\PassengerTypeQuantity;
11
use DateTimeImmutable;
12
use TK\API\Exception\InvalidArgumentException;
13
14
final class GetAvailabilityParametersFactory implements ValueObjectFactoryInterface
15
{
16
    /**
17
     * @param array $parameters
18
     * @return GetAvailabilityParameters
19
     * @throws \Exception
20
     */
21
    public static function createFromArray(array $parameters) : GetAvailabilityParameters
22
    {
23
        $originDestinationInformations = [];
24
        foreach ($parameters['OriginDestinationInformation'] as $originDestinationInformation) {
25
            $originLocation = new Location(
26
                $originDestinationInformation['OriginLocation']['LocationCode'],
27
                $originDestinationInformation['OriginLocation']['MultiAirportCityInd']
28
            );
29
            $destinationLocation  = new Location(
30
                $originDestinationInformation['DestinationLocation']['LocationCode'],
31
                $originDestinationInformation['DestinationLocation']['MultiAirportCityInd']
32
            );
33
            $departureDateTime = (new DepartureDateTime(
34
                DateTimeImmutable::createFromFormat('dM', $originDestinationInformation['DepartureDateTime']['Date']),
0 ignored issues
show
Bug introduced by
It seems like DateTimeImmutable::creat...tureDateTime']['Date']) can also be of type false; however, parameter $date of TK\API\ValueObject\Depar...DateTime::__construct() does only seem to accept DateTimeImmutable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
                /** @scrutinizer ignore-type */ DateTimeImmutable::createFromFormat('dM', $originDestinationInformation['DepartureDateTime']['Date']),
Loading history...
35
                $originDestinationInformation['DepartureDateTime']['WindowAfter'],
36
                $originDestinationInformation['DepartureDateTime']['WindowBefore']
37
            ))->withDateFormat('dM');
38
            $originDestinationInformationObject = new OriginDestinationInformation(
39
                $departureDateTime,
40
                $originLocation,
41
                $destinationLocation
42
            );
43
            if (array_key_exists('CabinPreferences', $originDestinationInformation)) {
44
                foreach ($originDestinationInformation['CabinPreferences'] as $cabinPreference) {
45
                    $originDestinationInformationObject = $originDestinationInformationObject
46
                        ->withCabinPreferences($cabinPreference['Cabin']);
47
                }
48
            }
49
            $originDestinationInformations[] = $originDestinationInformationObject;
50
        }
51
        $passengerTypeQuantityObject =  new PassengerTypeQuantity();
52
        foreach ($parameters['PassengerTypeQuantity'] as $passengerTypeQuantity) {
53
            $passengerTypeQuantityObject->withQuantity(
54
                $passengerTypeQuantity['Code'],
55
                $passengerTypeQuantity['Quantity']
56
            );
57
        }
58
        $getAvailabilityParameters = new GetAvailabilityParameters(
59
            $parameters['ReducedDataIndicator'],
60
            $parameters['RoutingType'],
61
            $passengerTypeQuantityObject
62
        );
63
64
        if (array_key_exists('TargetSource', $parameters)) {
65
            $getAvailabilityParameters = $getAvailabilityParameters->withTargetSource();
66
        }
67
        foreach ($originDestinationInformations as $originDestinationInformationObject) {
68
            $getAvailabilityParameters = $getAvailabilityParameters
69
                ->withOriginDestinationInformation($originDestinationInformationObject);
70
        }
71
        return $getAvailabilityParameters;
72
    }
73
74
    /**
75
     * @param string $json
76
     * @return GetAvailabilityParameters
77
     * @throws InvalidArgumentException
78
     * @throws \Exception
79
     */
80
    public static function createFromJson(string $json) : GetAvailabilityParameters
81
    {
82
        $parameters = json_decode($json, (bool) JSON_OBJECT_AS_ARRAY);
83
        if (json_last_error() !== JSON_ERROR_NONE) {
84
            throw new InvalidArgumentException(
85
                'GetAvailabilityParametersFactory Error: ' . json_last_error_msg()
86
            );
87
        }
88
        return self::createFromArray($parameters);
89
    }
90
}
91