Completed
Push — master ( 659f96...72a295 )
by Dieter
07:40
created

EnhancedPassengerGroup::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
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\NameChange;
24
25
use Amadeus\Client\RequestOptions\Pnr\NameChange\Infant;
26
use Amadeus\Client\RequestOptions\Pnr\NameChange\Passenger;
27
use Amadeus\Client\Struct\WsMessageUtility;
28
29
/**
30
 * EnhancedPassengerGroup
31
 *
32
 * @package Amadeus\Client\Struct\Pnr\NameChange
33
 * @author Dieter Devlieghere <[email protected]>
34
 */
35
class EnhancedPassengerGroup extends WsMessageUtility
36
{
37
    /**
38
     * @var ElementManagementPassenger
39
     */
40
    public $elementManagementPassenger;
41
42
    /**
43
     * @var EnhancedPassengerInformation[]
44
     */
45
    public $enhancedPassengerInformation = [];
46
47
    /**
48
     * EnhancedPassengerGroup constructor.
49
     *
50
     * @param Passenger $passenger
51
     */
52 7
    public function __construct($passenger)
53
    {
54 7
        $this->elementManagementPassenger = new ElementManagementPassenger($passenger->reference);
55
56 7
        $infantIndicator = $this->makeInfantIndicator($passenger->infant);
57
58 7
        $this->enhancedPassengerInformation[] = $this->makeMainPax($passenger, $infantIndicator);
59
60 7
        if ($infantIndicator > 1) {
61 3
            $this->enhancedPassengerInformation[] = $this->makeInfant($passenger->reference, $passenger->infant);
62 3
        }
63 7
    }
64
65
    /**
66
     * @param Passenger|Infant $passenger
67
     * @return OtherPaxNamesDetails[]
68
     */
69 7
    protected function makeNamesForPax($passenger)
70
    {
71 7
        $details = [];
72
73 7
        if ($this->checkAnyNotEmpty($passenger->firstName, $passenger->lastName)) {
74 7
            $details[] = new OtherPaxNamesDetails(
75 7
                $passenger->lastName,
76 7
                $passenger->firstName,
77 7
                $passenger->title
78 7
            );
79 7
        }
80
81 7
        if ($this->checkAnyNotEmpty($passenger->nativeFirstName, $passenger->nativeLastName)) {
82 2
            $details[] = new OtherPaxNamesDetails(
83 2
                $passenger->nativeLastName,
84 2
                $passenger->nativeFirstName,
85 2
                $passenger->title
86 2
            );
87 2
        }
88
89 7
        return $details;
90
    }
91
92
    /**
93
     * @param Infant|null $infant
94
     * @return int|null
95
     */
96 7
    protected function makeInfantIndicator($infant)
97
    {
98 7
        $indicator = null;
99
100 7
        if ($infant instanceof Infant) {
101 4
            if ($this->hasLastAndFist($infant)) {
102 2
                $indicator = 3;
103 4
            } elseif ($this->hasFirst($infant)) {
104 1
                $indicator = 2;
105 1
            } else {
106 1
                $indicator = 1;
107
            }
108 4
        }
109
110 7
        return $indicator;
111
    }
112
113
    /**
114
     * @param Infant $infant
115
     * @return bool
116
     */
117 4
    protected function hasLastAndFist(Infant $infant)
118
    {
119 4
        return ($this->checkAllNotEmpty($infant->firstName, $infant->lastName) ||
120 4
            $this->checkAllNotEmpty($infant->nativeFirstName, $infant->nativeLastName));
121
    }
122
123
    /**
124
     * @param Infant $infant
125
     * @return bool
126
     */
127 2
    protected function hasFirst(Infant $infant)
128
    {
129 2
        return ($this->checkAllNotEmpty($infant->firstName) ||
130 2
            $this->checkAllNotEmpty($infant->nativeFirstName));
131
    }
132
133
    /**
134
     * @param Passenger $passenger
135
     * @param int|null $infantIndicator
136
     * @return EnhancedPassengerInformation
137
     */
138 7 View Code Duplication
    protected function makeMainPax($passenger, $infantIndicator)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
139
    {
140 7
        $tmp = new EnhancedPassengerInformation();
141 7
        $tmp->enhancedTravellerNameInfo = new EnhancedTravellerNameInfo();
142 7
        $tmp->enhancedTravellerNameInfo->travellerNameInfo = new TravellerNameInfo(
143 7
            $passenger->reference,
144 7
            $passenger->type,
145
            $infantIndicator
146 7
        );
147
148 7
        $tmp->enhancedTravellerNameInfo->otherPaxNamesDetails = $this->makeNamesForPax($passenger);
149
150 7
        if ($passenger->dateOfBirth instanceof \DateTime) {
151 1
            $tmp->dateOfBirthInEnhancedPaxData = new DateOfBirthInEnhancedPaxData($passenger->dateOfBirth);
152 1
        }
153
154 7
        return $tmp;
155
    }
156
157
    /**
158
     * @param int $reference
159
     * @param Infant $infant
160
     * @return EnhancedPassengerInformation
161
     */
162 3 View Code Duplication
    protected function makeInfant($reference, $infant)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
163
    {
164 3
        $tmp = new EnhancedPassengerInformation();
165 3
        $tmp->enhancedTravellerNameInfo = new EnhancedTravellerNameInfo();
166 3
        $tmp->enhancedTravellerNameInfo->travellerNameInfo = new TravellerNameInfo(
167 3
            $reference,
168
            'INF'
169 3
        );
170
171 3
        $tmp->enhancedTravellerNameInfo->otherPaxNamesDetails = $this->makeNamesForPax($infant);
172
173 3
        if ($infant->dateOfBirth instanceof \DateTime) {
174 2
            $tmp->dateOfBirthInEnhancedPaxData = new DateOfBirthInEnhancedPaxData($infant->dateOfBirth);
175 2
        }
176
177 3
        return $tmp;
178
    }
179
}
180