CalculateAwardMilesWithTaxParameters   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 70
dl 0
loc 137
rs 10
c 0
b 0
f 0
wmc 23

12 Methods

Rating   Name   Duplication   Size   Complexity  
D getValue() 0 37 10
A withDepartureDestination() 0 4 1
A withDepartureDate() 0 4 1
A __construct() 0 10 2
A withPassengerType() 0 4 1
A withArrivalOrigin() 0 4 1
A getIataCode() 0 13 2
A withDepartureOrigin() 0 4 1
A withArrivalDate() 0 4 1
A withOneWay() 0 4 1
A withSeatGuaranteed() 0 4 1
A withArrivalDestination() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace TK\API\ValueObject;
5
6
use DateTimeImmutable;
7
use TK\API\Exception\InvalidArgumentException;
8
9
class CalculateAwardMilesWithTaxParameters implements ValueObjectInterface
10
{
11
    public const AWARD_TYPE_ECONOMY = 'E';
12
    public const AWARD_TYPE_BUSINESS = 'B';
13
    public const AWARD_TYPE_FIRST_CLASS = 'C';
14
15
    private static $awardTypeEnum = ['E', 'B', 'C'];
16
17
    private $awardType;
18
    private $wantMoreMiles;
19
    private $isOneWay = 'F';
20
    private $departureOrigin;
21
    private $departureDestination;
22
    private $departureDate;
23
    private $arrivalOrigin;
24
    private $arrivalDestination;
25
    private $arrivalDate;
26
    private $ptcType;
27
28
    public function __construct(string $awardType)
29
    {
30
        if (!\in_array($awardType, self::$awardTypeEnum, true)) {
31
            throw new InvalidArgumentException(
32
                'Invalid awardType value. Possible values are "' .
33
                implode(', ', self::$awardTypeEnum) . '"' .
34
                ' but provided value is "' . $awardType . '"'
35
            );
36
        }
37
        $this->awardType = $awardType;
38
    }
39
40
    public function withDepartureOrigin(string $iataCode) : CalculateAwardMilesWithTaxParameters
41
    {
42
        $this->departureOrigin = $this->getIataCode($iataCode);
43
        return $this;
44
    }
45
46
    public function withDepartureDate(DateTimeImmutable $date) : CalculateAwardMilesWithTaxParameters
47
    {
48
        $this->departureDate = $date;
49
        return $this;
50
    }
51
52
    public function withDepartureDestination(string $iataCode) : CalculateAwardMilesWithTaxParameters
53
    {
54
        $this->departureDestination = $this->getIataCode($iataCode);
55
        return $this;
56
    }
57
58
    public function withArrivalOrigin(string $iataCode) : CalculateAwardMilesWithTaxParameters
59
    {
60
        $this->arrivalOrigin = $this->getIataCode($iataCode);
61
        return $this;
62
    }
63
64
    public function withArrivalDate(DateTimeImmutable $date) : CalculateAwardMilesWithTaxParameters
65
    {
66
        $this->arrivalDate = $date;
67
        return $this;
68
    }
69
70
    public function withArrivalDestination(string $iataCode) : CalculateAwardMilesWithTaxParameters
71
    {
72
        $this->arrivalDestination = $this->getIataCode($iataCode);
73
        return $this;
74
    }
75
76
    public function withPassengerType(string $passengerType) : CalculateAwardMilesWithTaxParameters
77
    {
78
        $this->ptcType = $passengerType;
79
        return $this;
80
    }
81
82
    public function withOneWay() : CalculateAwardMilesWithTaxParameters
83
    {
84
        $this->isOneWay = 'T';
85
        return $this;
86
    }
87
88
    public function withSeatGuaranteed() : CalculateAwardMilesWithTaxParameters
89
    {
90
        $this->wantMoreMiles = 'T';
91
        return $this;
92
    }
93
94
    private function getIataCode(string $iataCode) : string
95
    {
96
        if (!preg_match('/^[A-Z]{3}$/', $iataCode)) {
97
            {
98
                throw new InvalidArgumentException(
99
                    sprintf(
100
                        'Invalid OriginLocation.LocationCode value (%s) provided. Valid IATA code must be used',
101
                        $iataCode
102
                    )
103
                );
104
            }
105
        }
106
        return $iataCode;
107
    }
108
109
    public function getValue() : array
110
    {
111
        $calculateAwardMilesWithTaxParameters = [
112
            'awardType' => $this->awardType
113
        ];
114
        if ($this->wantMoreMiles !== null) {
115
            $calculateAwardMilesWithTaxParameters['wantMoreMiles'] = 'T';
116
        }
117
        if ($this->isOneWay !== null) {
118
            $calculateAwardMilesWithTaxParameters['isOneWay'] = 'T';
119
        }
120
        if ($this->departureOrigin !== null) {
121
            $calculateAwardMilesWithTaxParameters['departureOrigin'] = $this->departureOrigin;
122
        }
123
        if ($this->departureDestination !== null) {
124
            $calculateAwardMilesWithTaxParameters['departureDestination'] = $this->departureDestination;
125
        }
126
        if ($this->departureDate !== null) {
127
            $calculateAwardMilesWithTaxParameters['departureDateDay'] = (int) $this->departureDate->format('j');
128
            $calculateAwardMilesWithTaxParameters['departureDateMonth'] = (int) $this->departureDate->format('n');
129
            $calculateAwardMilesWithTaxParameters['departureDateYear'] = (int) $this->departureDate->format('Y');
130
        }
131
        if ($this->arrivalOrigin !== null) {
132
            $calculateAwardMilesWithTaxParameters['arrivalOrigin'] = $this->arrivalOrigin;
133
        }
134
        if ($this->arrivalDestination !== null) {
135
            $calculateAwardMilesWithTaxParameters['arrivalDestination'] = $this->arrivalDestination;
136
        }
137
        if ($this->arrivalDate !== null) {
138
            $calculateAwardMilesWithTaxParameters['arrivalDateDay'] = (int) $this->arrivalDate->format('j');
139
            $calculateAwardMilesWithTaxParameters['arrivalDateMonth'] = (int) $this->arrivalDate->format('n');
140
            $calculateAwardMilesWithTaxParameters['arrivalDateYear'] = (int) $this->arrivalDate->format('Y');
141
        }
142
        if ($this->ptcType !== null) {
143
            $calculateAwardMilesWithTaxParameters['ptcType'] = $this->ptcType;
144
        }
145
        return $calculateAwardMilesWithTaxParameters;
146
    }
147
}
148