CalculateFlightMilesParameters::getIataCode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
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 CalculateFlightMilesParameters implements ValueObjectInterface
10
{
11
    private static $cardTypesEnum = ['CC', 'CP', 'EC', 'EP'];
12
13
    private $origin;
14
    private $destination;
15
    private $cabinCode;
16
    private $classCode;
17
    private $marketingClassCode;
18
    private $cardType;
19
    private $flightDate;
20
    private $operatingFlightNumber;
21
    private $marketingFlightNumber;
22
23
    public function __construct(string $origin, string $destination)
24
    {
25
        $this->origin = $this->getIataCode($origin);
26
        $this->destination = $this->getIataCode($destination);
27
    }
28
29
    private function getIataCode(string $iataCode) : string
30
    {
31
        if (!preg_match('/^[A-Z]{3}$/', $iataCode)) {
32
            {
33
                throw new InvalidArgumentException(
34
                    sprintf(
35
                        'Invalid OriginLocation.LocationCode value (%s) provided. Valid IATA code must be used',
36
                        $iataCode
37
                    )
38
                );
39
            }
40
        }
41
        return $iataCode;
42
    }
43
44
    public function withCabinCode() : CalculateFlightMilesParameters
45
    {
46
        $this->cabinCode = 'Y';
47
        return $this;
48
    }
49
50
    public function withClassCode() : CalculateFlightMilesParameters
51
    {
52
        $this->classCode = 'Y';
53
        return $this;
54
    }
55
56
    public function withMarketingClassCode(string $marketingClassCode) : CalculateFlightMilesParameters
57
    {
58
        $this->marketingClassCode = $marketingClassCode;
59
        return $this;
60
    }
61
62
    public function withCardType(string $cardType) : CalculateFlightMilesParameters
63
    {
64
        if (!\in_array($cardType, self::$cardTypesEnum, true)) {
65
            throw new InvalidArgumentException(
66
                'Invalid card_type value provided. Possile values are: ' .
67
                implode(', ', self::$cardTypesEnum)
68
            );
69
        }
70
        $this->cardType = $cardType;
71
        return $this;
72
    }
73
74
    public function withFlightDate(DateTimeImmutable $flightDate) : CalculateFlightMilesParameters
75
    {
76
        $this->flightDate = $flightDate;
77
        return $this;
78
    }
79
80
    public function withOperatingFlightNumber(string $operatingFlightNumber) : CalculateFlightMilesParameters
81
    {
82
        $this->operatingFlightNumber = $operatingFlightNumber;
83
        return $this;
84
    }
85
86
    public function withMarketingFlightNumber(string $marketingFlightNumber) : CalculateFlightMilesParameters
87
    {
88
        $this->marketingFlightNumber = $marketingFlightNumber;
89
        return $this;
90
    }
91
92
    public function getValue() : array
93
    {
94
        $calculateFlightMilesParameters = [
95
            'origin' => $this->origin,
96
            'destination' => $this->destination
97
        ];
98
        if ($this->cabinCode !== null) {
99
            $calculateFlightMilesParameters['cabin_code'] = 'Y';
100
        }
101
        if ($this->classCode !== null) {
102
            $calculateFlightMilesParameters['class_code'] = 'Y';
103
        }
104
        if ($this->marketingClassCode !== null) {
105
            $calculateFlightMilesParameters['marketingClassCode'] = $this->marketingClassCode;
106
        }
107
        if ($this->cardType !== null) {
108
            $calculateFlightMilesParameters['card_type'] = $this->cardType;
109
        }
110
        if ($this->flightDate !== null) {
111
            $calculateFlightMilesParameters['flightDate'] = $this->flightDate->format('d.m.Y');
112
        }
113
        if ($this->operatingFlightNumber !== null) {
114
            $calculateFlightMilesParameters['operatingFlightNumber'] = $this->operatingFlightNumber;
115
        }
116
        if ($this->marketingFlightNumber !== null) {
117
            $calculateFlightMilesParameters['marketingFlightNumber'] = $this->marketingFlightNumber;
118
        }
119
120
        return $calculateFlightMilesParameters;
121
    }
122
}
123