CalculateAwardMilesWithTaxParametersFactory   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 18

2 Methods

Rating   Name   Duplication   Size   Complexity  
F createFromArray() 0 53 16
A createFromJson() 0 9 2
1
<?php
2
declare(strict_types=1);
3
4
namespace TK\API\ValueObject\Factory;
5
6
use DateTimeImmutable;
7
use TK\API\Exception\InvalidArgumentException;
8
use TK\API\ValueObject\CalculateAwardMilesWithTaxParameters;
9
10
class CalculateAwardMilesWithTaxParametersFactory implements ValueObjectFactoryInterface
11
{
12
    /**
13
     * @param array $parameters
14
     * @return CalculateAwardMilesWithTaxParameters
15
     * @throws \Exception
16
     */
17
    public static function createFromArray(array $parameters) : CalculateAwardMilesWithTaxParameters
18
    {
19
        $calculateAwardMilesWithTaxParameters = new CalculateAwardMilesWithTaxParameters(
20
            $parameters['awardType']
21
        );
22
        if (array_key_exists('wantMoreMiles', $parameters) && $parameters['wantMoreMiles'] === 'T') {
23
            $calculateAwardMilesWithTaxParameters = $calculateAwardMilesWithTaxParameters->withSeatGuaranteed();
24
        }
25
        if (array_key_exists('isOneWay', $parameters) && $parameters['isOneWay'] === 'T') {
26
            $calculateAwardMilesWithTaxParameters = $calculateAwardMilesWithTaxParameters->withOneWay();
27
        }
28
        if (array_key_exists('departureOrigin', $parameters)) {
29
            $calculateAwardMilesWithTaxParameters = $calculateAwardMilesWithTaxParameters
30
                ->withDepartureOrigin($parameters['departureOrigin']);
31
        }
32
        if (array_key_exists('departureDestination', $parameters)) {
33
            $calculateAwardMilesWithTaxParameters = $calculateAwardMilesWithTaxParameters
34
                ->withDepartureDestination($parameters['departureDestination']);
35
        }
36
37
        if (array_key_exists('departureDateDay', $parameters) &&
38
            array_key_exists('departureDateMonth', $parameters) &&
39
            array_key_exists('departureDateYear', $parameters)
40
        ) {
41
            $departureDate = $parameters['departureDateYear'] . '-' . $parameters['departureDateMonth'] .
42
                '-' . $parameters['departureDateDay'];
43
            $calculateAwardMilesWithTaxParameters = $calculateAwardMilesWithTaxParameters
44
                ->withDepartureDate(DateTimeImmutable::createFromFormat('Y-m-d', $departureDate));
0 ignored issues
show
Bug introduced by
It seems like DateTimeImmutable::creat...Y-m-d', $departureDate) can also be of type false; however, parameter $date of TK\API\ValueObject\Calcu...rs::withDepartureDate() 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

44
                ->withDepartureDate(/** @scrutinizer ignore-type */ DateTimeImmutable::createFromFormat('Y-m-d', $departureDate));
Loading history...
45
        }
46
47
        if (array_key_exists('arrivalOrigin', $parameters)) {
48
            $calculateAwardMilesWithTaxParameters = $calculateAwardMilesWithTaxParameters
49
                ->withArrivalOrigin($parameters['arrivalOrigin']);
50
        }
51
        if (array_key_exists('arrivalDestination', $parameters)) {
52
            $calculateAwardMilesWithTaxParameters = $calculateAwardMilesWithTaxParameters
53
                ->withArrivalDestination($parameters['arrivalDestination']);
54
        }
55
56
        if (array_key_exists('arrivalDateDay', $parameters) &&
57
            array_key_exists('arrivalDateMonth', $parameters) &&
58
            array_key_exists('arrivalDateYear', $parameters)
59
        ) {
60
            $arrivalDate = $parameters['arrivalDateYear'] . '-' . $parameters['arrivalDateMonth'] .
61
                '-' . $parameters['arrivalDateDay'];
62
            $calculateAwardMilesWithTaxParameters = $calculateAwardMilesWithTaxParameters
63
                ->withArrivalDate(DateTimeImmutable::createFromFormat('Y-m-d', $arrivalDate));
0 ignored issues
show
Bug introduced by
It seems like DateTimeImmutable::creat...('Y-m-d', $arrivalDate) can also be of type false; however, parameter $date of TK\API\ValueObject\Calcu...ters::withArrivalDate() 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

63
                ->withArrivalDate(/** @scrutinizer ignore-type */ DateTimeImmutable::createFromFormat('Y-m-d', $arrivalDate));
Loading history...
64
        }
65
        if (array_key_exists('ptcType', $parameters)) {
66
            $calculateAwardMilesWithTaxParameters = $calculateAwardMilesWithTaxParameters
67
                ->withPassengerType($parameters['ptcType']);
68
        }
69
        return $calculateAwardMilesWithTaxParameters;
70
    }
71
72
    /**
73
     * @param string $json
74
     * @return CalculateAwardMilesWithTaxParameters
75
     * @throws InvalidArgumentException
76
     * @throws \Exception
77
     */
78
    public static function createFromJson(string $json) : CalculateAwardMilesWithTaxParameters
79
    {
80
        $parameters = json_decode($json, (bool) JSON_OBJECT_AS_ARRAY);
81
        if (json_last_error() !== JSON_ERROR_NONE) {
82
            throw new InvalidArgumentException(
83
                'CalculateAwardMilesWithTaxParametersFactory Error: ' . json_last_error_msg()
84
            );
85
        }
86
        return self::createFromArray($parameters);
87
    }
88
}
89