GetTimetableParametersFactory::createFromArray()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 39
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 29
nc 8
nop 1
dl 0
loc 39
rs 9.1448
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TK\API\ValueObject\Factory;
5
6
use DateTimeImmutable;
7
use TK\API\ValueObject\Location;
8
use TK\API\ValueObject\DepartureDateTime;
9
use TK\API\ValueObject\OriginDestinationInformation;
10
use TK\API\ValueObject\AirScheduleRQ;
11
use TK\API\ValueObject\GetTimetableParameters;
12
use TK\API\Exception\InvalidArgumentException;
13
14
final class GetTimetableParametersFactory implements ValueObjectFactoryInterface
15
{
16
    /**
17
     * @param array $parameters
18
     * @return GetTimetableParameters
19
     * @throws \Exception
20
     */
21
    public static function createFromArray(array $parameters) : GetTimetableParameters
22
    {
23
        $originDestinationInformation = $parameters['OTA_AirScheduleRQ']['OriginDestinationInformation'];
24
        $originLocation = new Location(
25
            $originDestinationInformation['OriginLocation']['LocationCode'],
26
            $originDestinationInformation['OriginLocation']['MultiAirportCityInd']
27
        );
28
        $destinationLocation = new Location(
29
            $originDestinationInformation['DestinationLocation']['LocationCode'],
30
            $originDestinationInformation['DestinationLocation']['MultiAirportCityInd']
31
        );
32
        $departureDateTime = new DepartureDateTime(
33
            DateTimeImmutable::createFromFormat('Y-m-d', $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

33
            /** @scrutinizer ignore-type */ DateTimeImmutable::createFromFormat('Y-m-d', $originDestinationInformation['DepartureDateTime']['Date']),
Loading history...
34
            'P3D',
35
            'P3D'
36
        );
37
        $originDestinationInformationObject = new OriginDestinationInformation(
38
            $departureDateTime,
39
            $originLocation,
40
            $destinationLocation
41
        );
42
        $airScheduleRQ = new AirScheduleRQ($originDestinationInformationObject);
43
        if (array_key_exists('AirlineCode', $parameters['OTA_AirScheduleRQ'])) {
44
            $airScheduleRQ = $airScheduleRQ->withAirlineCode($parameters['OTA_AirScheduleRQ']['AirlineCode']);
45
        }
46
        if (array_key_exists('FlightTypePref', $parameters['OTA_AirScheduleRQ']) &&
47
            $parameters['OTA_AirScheduleRQ']['FlightTypePref']['DirectAndNonStopOnlyInd'] === true) {
48
            $airScheduleRQ = $airScheduleRQ->withDirectAndNonStopOnlyInd();
49
        }
50
        $getTimetableParameters = new GetTimetableParameters(
51
            $airScheduleRQ,
52
            $parameters['scheduleType'],
53
            $parameters['tripType']
54
        );
55
        if (array_key_exists('returnDate', $parameters)) {
56
            $returnDate = DateTimeImmutable::createFromFormat('Y-m-d', $parameters['returnDate']);
57
            $getTimetableParameters = $getTimetableParameters->withReturnDate($returnDate);
0 ignored issues
show
Bug introduced by
It seems like $returnDate can also be of type false; however, parameter $returnDate of TK\API\ValueObject\GetTi...eters::withReturnDate() does only seem to accept DateTimeImmutable|null, 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

57
            $getTimetableParameters = $getTimetableParameters->withReturnDate(/** @scrutinizer ignore-type */ $returnDate);
Loading history...
58
        }
59
        return $getTimetableParameters;
60
    }
61
62
    /**
63
     * @param string $json
64
     * @return GetTimetableParameters
65
     * @throws InvalidArgumentException
66
     * @throws \Exception
67
     */
68
    public static function createFromJson(string $json) : GetTimetableParameters
69
    {
70
        $parameters = json_decode($json, (bool) JSON_OBJECT_AS_ARRAY);
71
        if (json_last_error() !== JSON_ERROR_NONE) {
72
            throw new InvalidArgumentException(
73
                'GetTimetableParametersFactory Error: ' . json_last_error_msg()
74
            );
75
        }
76
        return self::createFromArray($parameters);
77
    }
78
}
79