Completed
Push — master ( e5baa0...e98dc1 )
by Dieter
07:25
created

AddMultiElements::createSegment()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 42
Code Lines 29

Duplication

Lines 10
Ratio 23.81 %

Importance

Changes 0
Metric Value
dl 10
loc 42
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 29
nc 8
nop 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
    public function __construct(PnrAddMultiElementsBase $params = null)
83
    {
84
        if (!is_null($params)) {
85
            if ($params instanceof PnrCreatePnrOptions) {
86
                $this->loadCreatePnr($params);
87
            } elseif ($params instanceof PnrAddMultiElementsOptions) {
88
                $this->loadBare($params);
89
            }
90
        }
91
    }
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
    protected function loadBare(PnrAddMultiElementsOptions $params)
101
    {
102
        if (!is_null($params->actionCode)) {
103
            $this->pnrActions = new AddMultiElements\PnrActions(
104
                $params->actionCode
105
            );
106
        }
107
108
        if (!is_null($params->recordLocator)) {
109
            $this->reservationInfo = new AddMultiElements\ReservationInfo($params->recordLocator);
110
        }
111
112
        $tattooCounter = 0;
113
114
        if ($params->travellerGroup !== null) {
115
            $this->addTravellerGroup($params->travellerGroup);
116
        } else {
117
            $this->addTravellers($params->travellers);
118
        }
119
120
        if (!empty($params->tripSegments)) {
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
            $this->addSegments($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...
122
        }
123
124
        if (!empty($params->itineraries)) {
125
            $this->addItineraries($params->itineraries, $tattooCounter);
126
        }
127
128
        if (!empty($params->elements)) {
129
            $this->addElements(
130
                $params->elements,
131
                $tattooCounter,
132
                $params->receivedFrom
133
            );
134
        } elseif (!is_null($params->receivedFrom)) {
135
            if ($this->dataElementsMaster === null) {
136
                $this->dataElementsMaster = new DataElementsMaster();
137
            }
138
139
            $tattooCounter++;
140
141
            $this->dataElementsMaster->dataElementsIndiv[] = $this->createElement(
142
                new ReceivedFrom(['receivedFrom' => $params->receivedFrom]),
143
                $tattooCounter
144
            );
145
        }
146
    }
147
148
    /**
149
     * Make PNR_AddMultiElements structure from a PnrCreatePnrOptions input.
150
     *
151
     * @throws InvalidArgumentException When invalid input is provided
152
     * @param PnrCreatePnrOptions $params
153
     */
154
    protected function loadCreatePnr(PnrCreatePnrOptions $params)
155
    {
156
        $this->pnrActions = new AddMultiElements\PnrActions(
157
            $params->actionCode
158
        );
159
160
        $tattooCounter = 0;
161
162
        if ($params->travellerGroup !== null) {
163
            $this->addTravellerGroup($params->travellerGroup);
164
        } else {
165
            $this->addTravellers($params->travellers);
166
        }
167
168
        if (!empty($params->tripSegments)) {
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...
169
            $this->addSegments($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...
170
        }
171
172
        if (!empty($params->itineraries)) {
173
            $this->addItineraries($params->itineraries, $tattooCounter);
174
        }
175
176
        $this->addElements(
177
            $params->elements,
178
            $tattooCounter,
179
            $params->receivedFrom
180
        );
181
    }
182
183
    /**
184
     * @param Itinerary[] $itineraries
185
     * @param int $tattooCounter (BYREF)
186
     */
187
    protected function addItineraries($itineraries, &$tattooCounter)
188
    {
189
        foreach ($itineraries as $itinerary) {
190
            $this->addSegments(
191
                $itinerary->segments,
192
                $tattooCounter,
193
                $itinerary->origin,
194
                $itinerary->destination
195
            );
196
        }
197
    }
198
199
    /**
200
     * @param Segment[] $segments
201
     * @param int $tattooCounter
202
     * @param string|null $origin
203
     * @param string|null $destination
204
     *
205
     */
206
    protected function addSegments($segments, &$tattooCounter, $origin = null, $destination = null)
207
    {
208
        $tmpOrigDest = new OriginDestinationDetails();
209
210
        foreach ($segments as $segment) {
211
            $tmpOrigDest->itineraryInfo[] = $this->createSegment($segment, $tattooCounter);
212
        }
213
214
        if (!is_null($origin) && !is_null($destination)) {
215
            $tmpOrigDest->originDestination = new OriginDestination($origin, $destination);
216
        }
217
218
        $this->originDestinationDetails[] = $tmpOrigDest;
219
    }
220
221
    /**
222
     * @param Segment $segment
223
     * @param int $tattooCounter (BYREF)
224
     * @return ItineraryInfo
225
     */
226
    protected function createSegment($segment, &$tattooCounter)
227
    {
228
        $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...
229
230
        $tattooCounter++;
231
232
        $reflect = new \ReflectionClass($segment);
233
        $segmentType = $reflect->getShortName();
234
235
        switch ($segmentType) {
236 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...
237
                /** @var Segment\Miscellaneous $segment */
238
                $createdSegment = new ItineraryInfo($tattooCounter, ElementManagementItinerary::SEGMENT_MISCELLANEOUS);
239
                $createdSegment->airAuxItinerary = new AirAuxItinerary($segmentType, $segment);
240
                break;
241
            case 'Air':
242
                /** @var Segment\Air $segment */
243
                $createdSegment = new ItineraryInfo($tattooCounter, ElementManagementItinerary::SEGMENT_AIR);
244
                $createdSegment->airAuxItinerary = new AirAuxItinerary($segmentType, $segment);
245
                break;
246 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...
247
                /** @var Segment\ArrivalUnknown $segment */
248
                $createdSegment = new ItineraryInfo($tattooCounter, ElementManagementItinerary::SEGMENT_AIR);
249
                $createdSegment->airAuxItinerary = new AirAuxItinerary($segmentType, $segment);
250
                break;
251
            case 'Ghost':
252
                throw new \RuntimeException('NOT YET IMPLEMENTED');
253
                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...
254
            default:
255
                throw new InvalidArgumentException('Segment type ' . $segmentType . ' is not supported');
256
                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...
257
        }
258
259
        if (count($segment->references) > 0) {
260
            $createdSegment->referenceForSegment = new ReferenceForSegment();
261
            foreach ($segment->references as $singleRef) {
262
                $createdSegment->referenceForSegment->reference[] = new Reference($singleRef->type, $singleRef->id);
263
            }
264
        }
265
266
        return $createdSegment;
267
    }
268
269
270
    /**
271
     * @param Traveller[] $travellers
272
     */
273
    protected function addTravellers($travellers)
274
    {
275
        foreach ($travellers as $traveller) {
276
            $this->travellerInfo[] = $this->createTraveller($traveller);
277
        }
278
    }
279
280
    /**
281
     * @param Traveller $traveller
282
     * @return TravellerInfo
283
     */
284
    protected function createTraveller($traveller)
285
    {
286
        return new TravellerInfo($traveller);
287
    }
288
289
    /**
290
     * @param TravellerGroup $group
291
     */
292
    protected function addTravellerGroup($group)
293
    {
294
        $this->travellerInfo[] = new TravellerInfo(null, $group);
295
296
        $this->addTravellers($group->travellers);
297
    }
298
299
    /**
300
     * @param Element[] $elements
301
     * @param int $tattooCounter (BYREF)
302
     * @param string|null $receivedFromString
303
     */
304
    protected function addElements($elements, &$tattooCounter, $receivedFromString = null)
305
    {
306
        if ($this->dataElementsMaster === null) {
307
            $this->dataElementsMaster = new DataElementsMaster();
308
        }
309
310
        //Only add a default RF element if there is no explicitly provided RF element!
311
        $explicitRf = false;
312
313
        foreach ($elements as $element) {
314
            if ($element instanceof Element) {
315
                $this->dataElementsMaster->dataElementsIndiv[] = $this->createElement(
316
                    $element,
317
                    $tattooCounter
318
                );
319
            }
320
321
            if ($element instanceof ReceivedFrom) {
322
                $explicitRf = true;
323
            }
324
        }
325
326
        if ($receivedFromString !== null && !$explicitRf) {
327
            $this->dataElementsMaster->dataElementsIndiv[] = $this->createElement(
328
                new ReceivedFrom(['receivedFrom' => $receivedFromString]),
329
                $tattooCounter
330
            );
331
        }
332
    }
333
334
    /**
335
     * @param Element $element
336
     * @param int $tattooCounter (BYREF)
337
     * @throws InvalidArgumentException
338
     * @return DataElementsIndiv
339
     */
340
    protected function createElement($element, &$tattooCounter)
341
    {
342
        $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...
343
344
        $tattooCounter++;
345
346
        $createdElement = new DataElementsIndiv($element, $tattooCounter);
347
348
        if (!empty($element->references)) {
349
            $createdElement->referenceForDataElement = new ReferenceForDataElement($element->references);
350
        }
351
352
        return $createdElement;
353
    }
354
}
355