Completed
Push — master ( 8f168b...4b1795 )
by Dieter
09:49
created

AddMultiElements::loadCreatePnr()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 16
cts 16
cp 1
rs 9.2
c 0
b 0
f 0
cc 2
eloc 13
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;
24
25
use Amadeus\Client\RequestOptions\Pnr\Element;
26
use Amadeus\Client\RequestOptions\Pnr\Element\ReceivedFrom;
27
use Amadeus\Client\RequestOptions\Pnr\Itinerary;
28
use Amadeus\Client\RequestOptions\Pnr\Segment;
29
use Amadeus\Client\RequestOptions\Pnr\Traveller;
30
use Amadeus\Client\RequestOptions\Pnr\TravellerGroup;
31
use Amadeus\Client\RequestOptions\PnrAddMultiElementsBase;
32
use Amadeus\Client\RequestOptions\PnrAddMultiElementsOptions;
33
use Amadeus\Client\RequestOptions\PnrCreatePnrOptions;
34
use Amadeus\Client\Struct\BaseWsMessage;
35
use Amadeus\Client\Struct\InvalidArgumentException;
36
use Amadeus\Client\Struct\Pnr\AddMultiElements\AirAuxItinerary;
37
use Amadeus\Client\Struct\Pnr\AddMultiElements\DataElementsIndiv;
38
use Amadeus\Client\Struct\Pnr\AddMultiElements\DataElementsMaster;
39
use Amadeus\Client\Struct\Pnr\AddMultiElements\ElementManagementItinerary;
40
use Amadeus\Client\Struct\Pnr\AddMultiElements\ItineraryInfo;
41
use Amadeus\Client\Struct\Pnr\AddMultiElements\OriginDestination;
42
use Amadeus\Client\Struct\Pnr\AddMultiElements\OriginDestinationDetails;
43
use Amadeus\Client\Struct\Pnr\AddMultiElements\Reference;
44
use Amadeus\Client\Struct\Pnr\AddMultiElements\ReferenceForDataElement;
45
use Amadeus\Client\Struct\Pnr\AddMultiElements\ReferenceForSegment;
46
use Amadeus\Client\Struct\Pnr\AddMultiElements\TravellerInfo;
47
48
/**
49
 * Structure class for representing the PNR_AddMultiElements request message
50
 *
51
 * @package Amadeus\Client\Struct\Pnr
52
 * @author Dieter Devlieghere <[email protected]>
53
 */
54
class AddMultiElements extends BaseWsMessage
55
{
56
    /**
57
     * @var AddMultiElements\ReservationInfo
58
     */
59
    public $reservationInfo;
60
    /**
61
     * @var AddMultiElements\PnrActions
62
     */
63
    public $pnrActions;
64
    /**
65
     * @var AddMultiElements\TravellerInfo[]
66
     */
67
    public $travellerInfo = [];
68
    /**
69
     * @var AddMultiElements\OriginDestinationDetails[]
70
     */
71
    public $originDestinationDetails = [];
72
    /**
73
     * @var AddMultiElements\DataElementsMaster
74
     */
75
    public $dataElementsMaster;
76
77
    /**
78
     * Create PNR_AddMultiElements object
79
     *
80
     * @param PnrAddMultiElementsBase|null $params
81
     */
82 33
    public function __construct(PnrAddMultiElementsBase $params = null)
83
    {
84 33
        if (!is_null($params)) {
85 33
            if ($params instanceof PnrCreatePnrOptions) {
86 26
                $this->loadCreatePnr($params);
87 29
            } elseif ($params instanceof PnrAddMultiElementsOptions) {
88 7
                $this->loadBare($params);
89 7
            }
90 29
        }
91 29
    }
92
93
    /**
94
     * PNR_AddMultiElements call which only adds requested data to the message
95
     *
96
     * For doing specific actions like ignoring or saving PNR.
97
     *
98
     * @param PnrAddMultiElementsOptions $params
99
     */
100 7
    protected function loadBare(PnrAddMultiElementsOptions $params)
101
    {
102 7
        $tattooCounter = 0;
103
104 7
        if (!is_null($params->actionCode)) {
105 7
            $this->pnrActions = new AddMultiElements\PnrActions(
106 7
                $params->actionCode
107 7
            );
108 7
        }
109
110 7
        if (!is_null($params->recordLocator)) {
111 3
            $this->reservationInfo = new AddMultiElements\ReservationInfo($params->recordLocator);
112 3
        }
113
114 7
        if ($params->travellerGroup !== null) {
115 1
            $this->addTravellerGroup($params->travellerGroup);
116 1
        } else {
117 6
            $this->addTravellers($params->travellers);
118
        }
119
120 7
        $this->addItineraries($params->itineraries, $params->tripSegments, $tattooCounter);
0 ignored issues
show
Deprecated Code introduced by
The property Amadeus\Client\RequestOp...sOptions::$tripSegments has been deprecated with message: use $this->itinerary instead

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
121
122 7
        if (!empty($params->elements)) {
123 1
            $this->addElements(
124 1
                $params->elements,
125 1
                $tattooCounter,
126 1
                $params->receivedFrom
127 1
            );
128 7
        } elseif (!is_null($params->receivedFrom)) {
129 4
            if ($this->dataElementsMaster === null) {
130 4
                $this->dataElementsMaster = new DataElementsMaster();
131 4
            }
132
133 4
            $tattooCounter++;
134
135 4
            $this->dataElementsMaster->dataElementsIndiv[] = $this->createElement(
136 4
                new ReceivedFrom(['receivedFrom' => $params->receivedFrom]),
137
                $tattooCounter
138 4
            );
139 4
        }
140 7
    }
141
142
    /**
143
     * Make PNR_AddMultiElements structure from a PnrCreatePnrOptions input.
144
     *
145
     * @throws InvalidArgumentException When invalid input is provided
146
     * @param PnrCreatePnrOptions $params
147
     */
148 26
    protected function loadCreatePnr(PnrCreatePnrOptions $params)
149
    {
150 26
        $this->pnrActions = new AddMultiElements\PnrActions(
151 26
            $params->actionCode
152 26
        );
153
154 26
        $tattooCounter = 0;
155
156 26
        if ($params->travellerGroup !== null) {
157 1
            $this->addTravellerGroup($params->travellerGroup);
158 1
        } else {
159 25
            $this->addTravellers($params->travellers);
160
        }
161
162 26
        $this->addItineraries($params->itineraries, $params->tripSegments, $tattooCounter);
0 ignored issues
show
Deprecated Code introduced by
The property Amadeus\Client\RequestOp...rOptions::$tripSegments has been deprecated with message: use $this->itinerary instead

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
163
164 24
        $this->addElements(
165 24
            $params->elements,
166 24
            $tattooCounter,
167 24
            $params->receivedFrom
168 24
        );
169 22
    }
170
171
    /**
172
     * Load Segment itinerary
173
     *
174
     * @param Itinerary[] $itineraries
175
     * @param Segment[] $legacySegments
176
     * @param int $tattooCounter (BYREF)
177
     */
178 33
    protected function addItineraries($itineraries, $legacySegments, &$tattooCounter)
179
    {
180 33
        if (!empty($legacySegments)) {
181 23
            $this->addSegments($legacySegments, $tattooCounter);
182 21
        }
183
184 31
        foreach ($itineraries as $itinerary) {
185 4
            $this->addSegments(
186 4
                $itinerary->segments,
187 4
                $tattooCounter,
188 4
                $itinerary->origin,
189 4
                $itinerary->destination
190 4
            );
191 31
        }
192 31
    }
193
194
    /**
195
     * @param Segment[] $segments
196
     * @param int $tattooCounter
197
     * @param string|null $origin
198
     * @param string|null $destination
199
     *
200
     */
201 27
    protected function addSegments($segments, &$tattooCounter, $origin = null, $destination = null)
202
    {
203 27
        $tmpOrigDest = new OriginDestinationDetails();
204
205 27
        foreach ($segments as $segment) {
206 27
            $tmpOrigDest->itineraryInfo[] = $this->createSegment($segment, $tattooCounter);
207 25
        }
208
209 25
        if (!is_null($origin) && !is_null($destination)) {
210 3
            $tmpOrigDest->originDestination = new OriginDestination($origin, $destination);
211 3
        }
212
213 25
        $this->originDestinationDetails[] = $tmpOrigDest;
214 25
    }
215
216
    /**
217
     * @param Segment $segment
218
     * @param int $tattooCounter (BYREF)
219
     * @return ItineraryInfo
220
     */
221 27
    protected function createSegment($segment, &$tattooCounter)
222
    {
223 27
        $createdSegment = null;
0 ignored issues
show
Unused Code introduced by
$createdSegment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
224
225 27
        $tattooCounter++;
226
227 27
        $reflect = new \ReflectionClass($segment);
228 27
        $segmentType = $reflect->getShortName();
229
230
        switch ($segmentType) {
231 27 View Code Duplication
            case 'Miscellaneous':
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...
232
                /** @var Segment\Miscellaneous $segment */
233 22
                $createdSegment = new ItineraryInfo($tattooCounter, ElementManagementItinerary::SEGMENT_MISCELLANEOUS);
234 22
                $createdSegment->airAuxItinerary = new AirAuxItinerary($segmentType, $segment);
235 22
                break;
236 5
            case 'Air':
237
                /** @var Segment\Air $segment */
238 3
                $createdSegment = new ItineraryInfo($tattooCounter, ElementManagementItinerary::SEGMENT_AIR);
239 3
                $createdSegment->airAuxItinerary = new AirAuxItinerary($segmentType, $segment);
240 3
                break;
241 3 View Code Duplication
            case 'ArrivalUnknown':
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...
242
                /** @var Segment\ArrivalUnknown $segment */
243 1
                $createdSegment = new ItineraryInfo($tattooCounter, ElementManagementItinerary::SEGMENT_AIR);
244 1
                $createdSegment->airAuxItinerary = new AirAuxItinerary($segmentType, $segment);
245 1
                break;
246 2
            case 'Ghost':
247 1
                throw new \RuntimeException('NOT YET IMPLEMENTED');
248
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
249 1
            default:
250 1
                throw new InvalidArgumentException('Segment type ' . $segmentType . ' is not supported');
251
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
252 1
        }
253
254 25
        if (count($segment->references) > 0) {
255 5
            $createdSegment->referenceForSegment = new ReferenceForSegment();
256 5
            foreach ($segment->references as $singleRef) {
257 5
                $createdSegment->referenceForSegment->reference[] = new Reference($singleRef->type, $singleRef->id);
258 5
            }
259 5
        }
260
261 25
        return $createdSegment;
262
    }
263
264
265
    /**
266
     * @param Traveller[] $travellers
267
     */
268 33
    protected function addTravellers($travellers)
269
    {
270 33
        foreach ($travellers as $traveller) {
271 29
            $this->travellerInfo[] = $this->createTraveller($traveller);
272 33
        }
273 33
    }
274
275
    /**
276
     * @param Traveller $traveller
277
     * @return TravellerInfo
278
     */
279 29
    protected function createTraveller($traveller)
280
    {
281 29
        return new TravellerInfo($traveller);
282
    }
283
284
    /**
285
     * @param TravellerGroup $group
286
     */
287 2
    protected function addTravellerGroup($group)
288
    {
289 2
        $this->travellerInfo[] = new TravellerInfo(null, $group);
290
291 2
        $this->addTravellers($group->travellers);
292 2
    }
293
294
    /**
295
     * @param Element[] $elements
296
     * @param int $tattooCounter (BYREF)
297
     * @param string|null $receivedFromString
298
     */
299 25
    protected function addElements($elements, &$tattooCounter, $receivedFromString = null)
300
    {
301 25
        if ($this->dataElementsMaster === null) {
302 25
            $this->dataElementsMaster = new DataElementsMaster();
303 25
        }
304
305
        //Only add a default RF element if there is no explicitly provided RF element!
306 25
        $explicitRf = false;
307
308 25
        foreach ($elements as $element) {
309 16
            if ($element instanceof Element) {
310 16
                $this->dataElementsMaster->dataElementsIndiv[] = $this->createElement(
311 16
                    $element,
312
                    $tattooCounter
313 16
                );
314 14
            }
315
316 14
            if ($element instanceof ReceivedFrom) {
317 1
                $explicitRf = true;
318 1
            }
319 23
        }
320
321 23
        if ($receivedFromString !== null && !$explicitRf) {
322 21
            $this->dataElementsMaster->dataElementsIndiv[] = $this->createElement(
323 21
                new ReceivedFrom(['receivedFrom' => $receivedFromString]),
324
                $tattooCounter
325 21
            );
326 21
        }
327 23
    }
328
329
    /**
330
     * @param Element $element
331
     * @param int $tattooCounter (BYREF)
332
     * @throws InvalidArgumentException
333
     * @return DataElementsIndiv
334
     */
335 29
    protected function createElement($element, &$tattooCounter)
336
    {
337 29
        $createdElement = null;
0 ignored issues
show
Unused Code introduced by
$createdElement is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
338
339 29
        $tattooCounter++;
340
341 29
        $createdElement = new DataElementsIndiv($element, $tattooCounter);
342
343 27
        if (!empty($element->references)) {
344 1
            $createdElement->referenceForDataElement = new ReferenceForDataElement($element->references);
345 1
        }
346
347 27
        return $createdElement;
348
    }
349
}
350