Completed
Push — master ( 50a8c9...dab013 )
by Dieter
07:18
created

AddMultiElements::loadBare()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 30
cts 30
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 25
nc 16
nop 1
crap 5
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 40
    public function __construct(PnrAddMultiElementsBase $params = null)
83
    {
84 40
        if (!is_null($params)) {
85 40
            if ($params instanceof PnrCreatePnrOptions) {
86 29
                $this->loadCreatePnr($params);
87 36
            } elseif ($params instanceof PnrAddMultiElementsOptions) {
88 11
                $this->loadBare($params);
89 11
            }
90 36
        }
91 36
    }
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 11
    protected function loadBare(PnrAddMultiElementsOptions $params)
101
    {
102 11
        $tattooCounter = 0;
103
104 11
        if (!is_null($params->actionCode)) {
105 11
            $this->pnrActions = new AddMultiElements\PnrActions(
106 11
                $params->actionCode
0 ignored issues
show
Bug introduced by
It seems like $params->actionCode can also be of type array<integer,integer>; however, Amadeus\Client\Struct\Pn...rActions::__construct() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
107 11
            );
108 11
        }
109
110 11
        if (!is_null($params->recordLocator)) {
111 3
            $this->reservationInfo = new AddMultiElements\ReservationInfo($params->recordLocator);
112 3
        }
113
114 11
        if ($params->travellerGroup !== null) {
115 1
            $this->addTravellerGroup($params->travellerGroup);
116 1
        } else {
117 10
            $this->addTravellers($params->travellers);
118
        }
119
120 11
        $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 11
        if (!empty($params->elements)) {
123 5
            $this->addElements(
124 5
                $params->elements,
125 5
                $tattooCounter,
126 5
                $params->autoAddReceivedFrom,
127 5
                $params->defaultReceivedFrom,
0 ignored issues
show
Deprecated Code introduced by
The property Amadeus\Client\RequestOp...e::$defaultReceivedFrom has been deprecated with message: This is a workaround until we can decide on what happens with RF elements for the next major version.

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...
128 5
                $params->receivedFrom
129 5
            );
130 5
        } else {
131 6
            $this->addReceivedFrom(
132 6
                $params->receivedFrom,
133 6
                $params->autoAddReceivedFrom,
134 6
                $params->defaultReceivedFrom,
0 ignored issues
show
Deprecated Code introduced by
The property Amadeus\Client\RequestOp...e::$defaultReceivedFrom has been deprecated with message: This is a workaround until we can decide on what happens with RF elements for the next major version.

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...
135
                $tattooCounter
136 6
            );
137
        }
138 11
    }
139
140
    /**
141
     * Make PNR_AddMultiElements structure from a PnrCreatePnrOptions input.
142
     *
143
     * @throws InvalidArgumentException When invalid input is provided
144
     * @param PnrCreatePnrOptions $params
145
     */
146 29
    protected function loadCreatePnr(PnrCreatePnrOptions $params)
147
    {
148 29
        $this->pnrActions = new AddMultiElements\PnrActions(
149 29
            $params->actionCode
0 ignored issues
show
Bug introduced by
It seems like $params->actionCode can also be of type array<integer,integer>; however, Amadeus\Client\Struct\Pn...rActions::__construct() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
150 29
        );
151
152 29
        $tattooCounter = 0;
153
154 29
        if ($params->travellerGroup !== null) {
155 1
            $this->addTravellerGroup($params->travellerGroup);
156 1
        } else {
157 28
            $this->addTravellers($params->travellers);
158
        }
159
160 29
        $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...
161
162 27
        $this->addElements(
163 27
            $params->elements,
164 27
            $tattooCounter,
165 27
            $params->autoAddReceivedFrom,
166 27
            $params->defaultReceivedFrom,
0 ignored issues
show
Deprecated Code introduced by
The property Amadeus\Client\RequestOp...e::$defaultReceivedFrom has been deprecated with message: This is a workaround until we can decide on what happens with RF elements for the next major version.

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...
167 27
            $params->receivedFrom
168 27
        );
169 25
    }
170
171
    /**
172
     * Load Segment itinerary
173
     *
174
     * @param Itinerary[] $itineraries
175
     * @param Segment[] $legacySegments
176
     * @param int $tattooCounter (BYREF)
177
     */
178 40
    protected function addItineraries($itineraries, $legacySegments, &$tattooCounter)
179
    {
180 40
        if (!empty($legacySegments)) {
181 24
            $this->addSegments($legacySegments, $tattooCounter);
182 22
        }
183
184 38
        foreach ($itineraries as $itinerary) {
185 7
            $this->addSegments(
186 7
                $itinerary->segments,
187 7
                $tattooCounter,
188 7
                $itinerary->origin,
189 7
                $itinerary->destination
190 7
            );
191 38
        }
192 38
    }
193
194
    /**
195
     * @param Segment[] $segments
196
     * @param int $tattooCounter
197
     * @param string|null $origin
198
     * @param string|null $destination
199
     *
200
     */
201 31
    protected function addSegments($segments, &$tattooCounter, $origin = null, $destination = null)
202
    {
203 31
        $tmpOrigDest = new OriginDestinationDetails();
204
205 31
        foreach ($segments as $segment) {
206 31
            $tmpOrigDest->itineraryInfo[] = $this->createSegment($segment, $tattooCounter);
207 29
        }
208
209 29
        if (!is_null($origin) && !is_null($destination)) {
210 5
            $tmpOrigDest->originDestination = new OriginDestination($origin, $destination);
211 5
        }
212
213 29
        $this->originDestinationDetails[] = $tmpOrigDest;
214 29
    }
215
216
    /**
217
     * @param Segment $segment
218
     * @param int $tattooCounter (BYREF)
219
     * @return ItineraryInfo
220
     */
221 31
    protected function createSegment($segment, &$tattooCounter)
222
    {
223 31
        $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 31
        $tattooCounter++;
226
227 31
        $reflect = new \ReflectionClass($segment);
228 31
        $segmentType = $reflect->getShortName();
229
230
        switch ($segmentType) {
231 31 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 24
                $createdSegment = new ItineraryInfo($tattooCounter, ElementManagementItinerary::SEGMENT_MISCELLANEOUS);
234 24
                $createdSegment->airAuxItinerary = new AirAuxItinerary($segmentType, $segment);
235 24
                break;
236 7
            case 'Air':
237
                /** @var Segment\Air $segment */
238 5
                $createdSegment = new ItineraryInfo($tattooCounter, ElementManagementItinerary::SEGMENT_AIR);
239 5
                $createdSegment->airAuxItinerary = new AirAuxItinerary($segmentType, $segment);
240 5
                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 29
        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 29
        return $createdSegment;
262
    }
263
264
265
    /**
266
     * @param Traveller[] $travellers
267
     */
268 40
    protected function addTravellers($travellers)
269
    {
270 40
        foreach ($travellers as $traveller) {
271 35
            $this->travellerInfo[] = $this->createTraveller($traveller);
272 40
        }
273 40
    }
274
275
    /**
276
     * @param Traveller $traveller
277
     * @return TravellerInfo
278
     */
279 35
    protected function createTraveller($traveller)
280
    {
281 35
        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 bool $autoAddRf
298
     * @param string|null $defaultRf
299
     * @param string|null $explicitRf
300
     */
301 32
    protected function addElements($elements, &$tattooCounter, $autoAddRf, $defaultRf, $explicitRf = null)
302
    {
303 32
        if ($this->dataElementsMaster === null) {
304 32
            $this->dataElementsMaster = new DataElementsMaster();
305 32
        }
306
307
        //Only add a default RF element if there is no explicitly provided RF element!
308 32
        $hasReceivedFromElement = false;
309
310 32
        foreach ($elements as $element) {
311 23
            if ($element instanceof Element) {
312 23
                $this->dataElementsMaster->dataElementsIndiv[] = $this->createElement(
313 23
                    $element,
314
                    $tattooCounter
315 23
                );
316 21
            }
317
318 21
            if ($element instanceof ReceivedFrom) {
319 1
                $hasReceivedFromElement = true;
320 1
            }
321 30
        }
322
323 30
        if (!$hasReceivedFromElement) {
324 29
            $this->addReceivedFrom(
325 29
                $explicitRf,
326 29
                $autoAddRf,
327 29
                $defaultRf,
328
                $tattooCounter
329 29
            );
330 29
        }
331 30
    }
332
333
    /**
334
     * @param Element $element
335
     * @param int $tattooCounter (BYREF)
336
     * @throws InvalidArgumentException
337
     * @return DataElementsIndiv
338
     */
339 37
    protected function createElement($element, &$tattooCounter)
340
    {
341 37
        $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...
342
343 37
        $tattooCounter++;
344
345 37
        $createdElement = new DataElementsIndiv($element, $tattooCounter);
346
347 35
        if (!empty($element->references)) {
348 5
            $createdElement->referenceForDataElement = new ReferenceForDataElement($element->references);
349 5
        }
350
351 35
        return $createdElement;
352
    }
353
354
    /**
355
     * Add Received From field - if needed.
356
     *
357
     * @param string|null $explicitRf Explicitly provided RF string on request.
358
     * @param bool $doAutoAdd Wether to automatically add an RF field.
359
     * @param string|null $defaultRf The default RF string set in the client.
360
     * @param int $tattooCounter (BYREF)
361
     */
362 35
    protected function addReceivedFrom($explicitRf, $doAutoAdd, $defaultRf, &$tattooCounter)
363
    {
364 35
        if ($this->dataElementsMaster === null) {
365 6
            $this->dataElementsMaster = new DataElementsMaster();
366 6
        }
367
368 35
        if (!empty($explicitRf) || ($doAutoAdd && !empty($defaultRf))) {
369
            //Set a received from if explicitly provided or if auto received from is enabled
370
371 30
            $tattooCounter++;
372
373 30
            $rfToAdd = (!empty($explicitRf)) ? $explicitRf : $defaultRf;
374
375 30
            $this->dataElementsMaster->dataElementsIndiv[] = $this->createElement(
376 30
                new ReceivedFrom(['receivedFrom' => $rfToAdd]),
377
                $tattooCounter
378 30
            );
379 30
        }
380 35
    }
381
}
382