Completed
Pull Request — master (#231)
by
unknown
07:23
created

TravellerInfo::loadTravellerGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * amadeus-ws-client
4
 *
5
 * Copyright 2015 Amadeus Benelux NV
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 * http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 *
19
 * @package Amadeus
20
 * @license https://opensource.org/licenses/Apache-2.0 Apache 2.0
21
 */
22
23
namespace Amadeus\Client\Struct\Pnr\AddMultiElements;
24
25
use Amadeus\Client\RequestOptions\Pnr\Traveller as TravellerOptions;
26
use Amadeus\Client\RequestOptions\Pnr\TravellerGroup as TravellerGroupOptions;
27
28
/**
29
 * TravellerInfo
30
 *
31
 * @package Amadeus\Client\Struct\Pnr\AddMultiElements
32
 * @author Dieter Devlieghere <[email protected]>
33
 */
34
class TravellerInfo
35
{
36
    /**
37
     * @var ElementManagementPassenger
38
     */
39
    public $elementManagementPassenger;
40
    /**
41
     * Up to 2 PassengerData elements
42
     *
43
     * @var PassengerData[]
44
     */
45
    public $passengerData = [];
46
    /**
47
     * @todo expand this structure
48
     * @var array
49
     */
50
    public $enhancedPassengerData = [];
51
52
    /**
53
     * TravellerInfo constructor.
54
     *
55
     * @param TravellerOptions|null $traveller
56
     * @param TravellerGroupOptions|null $travellerGroup
57
     */
58 156
    public function __construct($traveller = null, $travellerGroup = null)
59
    {
60 156
        if ($traveller instanceof TravellerOptions) {
61 152
            $this->loadTraveller($traveller);
62 84
        } elseif ($travellerGroup instanceof TravellerGroupOptions) {
63 8
            $this->loadTravellerGroup($travellerGroup);
64 4
        }
65 156
    }
66
67
    /**
68
     * @param TravellerGroupOptions $group
69
     */
70 8
    protected function loadTravellerGroup($group)
71
    {
72 8
        $this->elementManagementPassenger = new ElementManagementPassenger(
73 4
            ElementManagementPassenger::SEG_GROUPNAME
74 4
        );
75
76 8
        $this->passengerData[] = new PassengerData($group->name);
77
78 8
        $this->passengerData[0]->travellerInformation->traveller->quantity = $group->nrOfTravellers;
79 8
        $this->passengerData[0]->travellerInformation->traveller->qualifier = Traveller::QUAL_GROUP;
80 8
    }
81
82
    /**
83
     * @param TravellerOptions $traveller
84
     */
85 152
    protected function loadTraveller(TravellerOptions $traveller)
86
    {
87 152
        $this->elementManagementPassenger = new ElementManagementPassenger(
88 76
            ElementManagementPassenger::SEG_NAME
89 76
        );
90
91 152
        $this->passengerData[] = new PassengerData($traveller->lastName);
92
93 152
        if (!is_null($traveller->number)) {
94 148
            $this->elementManagementPassenger->reference = new Reference(
95 148
                Reference::QUAL_PASSENGER,
96 148
                $traveller->number
97 74
            );
98 74
        }
99
100 152
        if ($traveller->firstName !== null || $traveller->travellerType !== null) {
101 148
            $this->passengerData[0]->travellerInformation->passenger[] = new Passenger(
102 148
                $traveller->firstName,
103 148
                $traveller->travellerType
104 74
            );
105 74
        }
106
107 152
        if ($traveller->withInfant === true || $traveller->infant !== null) {
108 16
            $this->addInfant($traveller);
109 8
        }
110
111 152 View Code Duplication
        if ($traveller->dateOfBirth instanceof \DateTime) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112 8
            $this->passengerData[0]->dateOfBirth = new DateOfBirth(
113 8
                $this->formatDateOfBirth($traveller->dateOfBirth)
114 4
            );
115 4
        }
116 152
    }
117
118
    /**
119
     * Add infant
120
     *
121
     * 3 scenario's:
122
     * - infant without additional information
123
     * - infant with only first name provided
124
     * - infant with first name, last name & date of birth provided.
125
     *
126
     * @param TravellerOptions $traveller
127
     */
128 16
    protected function addInfant($traveller)
129
    {
130 16
        $this->passengerData[0]->travellerInformation->traveller->quantity = 2;
131
132 16
        if ($traveller->withInfant && is_null($traveller->infant)) {
133 4
            $this->makePassengerIfNeeded();
134 4
            $this->passengerData[0]->travellerInformation->passenger[0]->infantIndicator = Passenger::INF_NOINFO;
135 14
        } elseif ($traveller->infant instanceof TravellerOptions) {
136 12
            if (empty($traveller->infant->lastName)) {
137 8
                $this->makePassengerIfNeeded();
138 8
                $this->passengerData[0]->travellerInformation->passenger[0]->infantIndicator = Passenger::INF_GIVEN;
139
140 8
                $tmpInfantPassenger = new Passenger(
141 8
                    $traveller->infant->firstName,
142 4
                    Passenger::PASST_INFANT
143 4
                );
144
145 8
                $this->passengerData[0]->travellerInformation->passenger[] = $tmpInfantPassenger;
146 4
            } else {
147 4
                $this->makePassengerIfNeeded();
148 4
                $this->passengerData[0]->travellerInformation->passenger[0]->infantIndicator = Passenger::INF_FULL;
149
150 4
                $tmpInfant = new PassengerData($traveller->infant->lastName);
151 4
                $tmpInfant->travellerInformation->passenger[] = new Passenger(
152 4
                    $traveller->infant->firstName,
153 2
                    Passenger::PASST_INFANT
154 2
                );
155
156 4 View Code Duplication
                if ($traveller->infant->dateOfBirth instanceof \DateTime) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157 4
                    $tmpInfant->dateOfBirth = new DateOfBirth(
158 4
                        $this->formatDateOfBirth($traveller->infant->dateOfBirth)
159 2
                    );
160 2
                }
161
162 4
                $this->passengerData[] = $tmpInfant;
163
            }
164 6
        }
165 16
    }
166
167
    /**
168
     * If there is no passenger node at
169
     * $travellerInfo->passengerData[0]->travellerInformation->passenger[0]
170
     * create one
171
     */
172 16
    protected function makePassengerIfNeeded()
173
    {
174 16
        if (count($this->passengerData[0]->travellerInformation->passenger) < 1) {
175 4
            $this->passengerData[0]->travellerInformation->passenger[0] = new Passenger(null, null);
176 2
        }
177 16
    }
178
179 16
    protected function formatDateOfBirth(\DateTime $dateOfBirth)
180
    {
181 16
        $day = (int) $dateOfBirth->format('d');
182 16
        if ($day < 10) {
183 12
            $day = "0$day";
184 6
        }
185
186 16
        $monthAndYear = strtoupper($dateOfBirth->format('My'));
187
188 16
        return $day . $monthAndYear;
189
    }
190
}
191