AddMultiElements::loadBare()   A
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 24
c 0
b 0
f 0
dl 0
loc 36
ccs 29
cts 29
cp 1
rs 9.2248
cc 5
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
     * @throws \ReflectionException
82
     */
83 215
    public function __construct(?PnrAddMultiElementsBase $params = null)
84
    {
85 215
        if (!is_null($params)) {
86 215
            if ($params instanceof PnrCreatePnrOptions) {
87 155
                $this->loadCreatePnr($params);
88 114
            } elseif ($params instanceof PnrAddMultiElementsOptions) {
89 60
                $this->loadBare($params);
90 24
            }
91 78
        }
92 195
    }
93
94
    /**
95
     * PNR_AddMultiElements call which only adds requested data to the message
96
     *
97
     * For doing specific actions like ignoring or saving PNR.
98
     *
99
     * @param PnrAddMultiElementsOptions $params
100
     * @throws \ReflectionException
101
     */
102 60
    protected function loadBare(PnrAddMultiElementsOptions $params)
103
    {
104 60
        $tattooCounter = 0;
105
106 60
        if (!is_null($params->actionCode)) {
0 ignored issues
show
introduced by
The condition is_null($params->actionCode) is always false.
Loading history...
107 60
            $this->pnrActions = new AddMultiElements\PnrActions(
108 60
                $params->actionCode
109 24
            );
110 24
        }
111
112 60
        if (!is_null($params->recordLocator)) {
0 ignored issues
show
introduced by
The condition is_null($params->recordLocator) is always false.
Loading history...
113 15
            $this->reservationInfo = new AddMultiElements\ReservationInfo($params->recordLocator);
114 6
        }
115
116 60
        if ($params->travellerGroup !== null) {
117 5
            $this->addTravellerGroup($params->travellerGroup);
118 2
        } else {
119 55
            $this->addTravellers($params->travellers);
120
        }
121
122 60
        $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: use $this->itinerary instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

122
        $this->addItineraries($params->itineraries, /** @scrutinizer ignore-deprecated */ $params->tripSegments, $tattooCounter);

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...
123
124 60
        if (!empty($params->elements)) {
125 30
            $this->addElements(
126 30
                $params->elements,
127 24
                $tattooCounter,
128 30
                $params->autoAddReceivedFrom,
129 30
                $params->defaultReceivedFrom,
0 ignored issues
show
Deprecated Code introduced by
The property Amadeus\Client\RequestOp...e::$defaultReceivedFrom has been deprecated: This is a workaround until we can decide on what happens with RF elements for the next major version. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

129
                /** @scrutinizer ignore-deprecated */ $params->defaultReceivedFrom,

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

136
                /** @scrutinizer ignore-deprecated */ $params->defaultReceivedFrom,

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...
137 12
                $tattooCounter
138 12
            );
139
        }
140 60
    }
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
     * @throws \ReflectionException
148
     */
149 155
    protected function loadCreatePnr(PnrCreatePnrOptions $params)
150
    {
151 155
        $this->pnrActions = new AddMultiElements\PnrActions(
152 155
            $params->actionCode
153 62
        );
154
155 155
        $tattooCounter = 0;
156
157 155
        if ($params->travellerGroup !== null) {
158 5
            $this->addTravellerGroup($params->travellerGroup);
159 2
        } else {
160 150
            $this->addTravellers($params->travellers);
161
        }
162
163 155
        $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: use $this->itinerary instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

163
        $this->addItineraries($params->itineraries, /** @scrutinizer ignore-deprecated */ $params->tripSegments, $tattooCounter);

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...
164
165 145
        $this->addElements(
166 145
            $params->elements,
167 116
            $tattooCounter,
168 145
            $params->autoAddReceivedFrom,
169 145
            $params->defaultReceivedFrom,
0 ignored issues
show
Deprecated Code introduced by
The property Amadeus\Client\RequestOp...e::$defaultReceivedFrom has been deprecated: This is a workaround until we can decide on what happens with RF elements for the next major version. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

169
            /** @scrutinizer ignore-deprecated */ $params->defaultReceivedFrom,

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 145
            $params->receivedFrom
171 58
        );
172 135
    }
173
174
    /**
175
     * Load Segment itinerary
176
     *
177
     * @param Itinerary[] $itineraries
178
     * @param Segment[] $legacySegments
179
     * @param int $tattooCounter (BYREF)
180
     * @throws \ReflectionException
181
     */
182 215
    protected function addItineraries($itineraries, $legacySegments, &$tattooCounter)
183
    {
184 215
        if (!empty($legacySegments)) {
185 120
            $this->addSegments($legacySegments, $tattooCounter);
186 44
        }
187
188 205
        foreach ($itineraries as $itinerary) {
189 35
            $this->addSegments(
190 35
                $itinerary->segments,
191 28
                $tattooCounter,
192 35
                $itinerary->origin,
193 35
                $itinerary->destination
194 14
            );
195 82
        }
196 205
    }
197
198
    /**
199
     * @param Segment[] $segments
200
     * @param int $tattooCounter
201
     * @param string|null $origin
202
     * @param string|null $destination
203
     * @throws \ReflectionException
204
     */
205 155
    protected function addSegments($segments, &$tattooCounter, $origin = null, $destination = null)
206
    {
207 155
        $tmpOrigDest = new OriginDestinationDetails();
208
209 155
        foreach ($segments as $segment) {
210 155
            $tmpOrigDest->itineraryInfo[] = $this->createSegment($segment, $tattooCounter);
211 58
        }
212
213 145
        if (!is_null($origin) && !is_null($destination)) {
214 25
            $tmpOrigDest->originDestination = new OriginDestination($origin, $destination);
215 10
        }
216
217 145
        $this->originDestinationDetails[] = $tmpOrigDest;
218 145
    }
219
220
    /**
221
     * @param Segment $segment
222
     * @param int $tattooCounter (BYREF)
223
     * @return ItineraryInfo
224
     * @throws \ReflectionException
225
     */
226 155
    protected function createSegment($segment, &$tattooCounter)
227
    {
228 155
        $createdSegment = null;
229
230 155
        $tattooCounter++;
231
232 155
        $reflect = new \ReflectionClass($segment);
233 155
        $segmentType = $reflect->getShortName();
234
235 62
        switch ($segmentType) {
236 155
            case 'Miscellaneous':
237
                /** @var Segment\Miscellaneous $segment */
238 120
                $createdSegment = new ItineraryInfo($tattooCounter, ElementManagementItinerary::SEGMENT_MISCELLANEOUS);
239 120
                $createdSegment->airAuxItinerary = new AirAuxItinerary($segmentType, $segment);
240 120
                break;
241 35
            case 'Air':
242
                /** @var Segment\Air $segment */
243 25
                $createdSegment = new ItineraryInfo($tattooCounter, ElementManagementItinerary::SEGMENT_AIR);
244 25
                $createdSegment->airAuxItinerary = new AirAuxItinerary($segmentType, $segment);
245 25
                break;
246 15
            case 'ArrivalUnknown':
247
                /** @var Segment\ArrivalUnknown $segment */
248 5
                $createdSegment = new ItineraryInfo($tattooCounter, ElementManagementItinerary::SEGMENT_AIR);
249 5
                $createdSegment->airAuxItinerary = new AirAuxItinerary($segmentType, $segment);
250 5
                break;
251 10
            case 'Ghost':
252 5
                throw new \RuntimeException('NOT YET IMPLEMENTED');
253
                break;
254 2
            default:
255 5
                throw new InvalidArgumentException('Segment type '.$segmentType.' is not supported');
256
                break;
257 2
        }
258
259 145
        if (count($segment->references) > 0) {
260 25
            $createdSegment->referenceForSegment = new ReferenceForSegment();
261 25
            foreach ($segment->references as $singleRef) {
262 25
                $createdSegment->referenceForSegment->reference[] = new Reference($singleRef->type, $singleRef->id);
263 10
            }
264 10
        }
265
266 145
        return $createdSegment;
267
    }
268
269
270
    /**
271
     * @param Traveller[] $travellers
272
     */
273 215
    protected function addTravellers($travellers)
274
    {
275 215
        foreach ($travellers as $traveller) {
276 190
            $this->travellerInfo[] = $this->createTraveller($traveller);
277 86
        }
278 215
    }
279
280
    /**
281
     * @param Traveller $traveller
282
     * @return TravellerInfo
283
     */
284 190
    protected function createTraveller($traveller)
285
    {
286 190
        return new TravellerInfo($traveller);
287
    }
288
289
    /**
290
     * @param TravellerGroup $group
291
     */
292 10
    protected function addTravellerGroup($group)
293
    {
294 10
        $this->travellerInfo[] = new TravellerInfo(null, $group);
295
296 10
        $this->addTravellers($group->travellers);
297 10
    }
298
299
    /**
300
     * @param Element[] $elements
301
     * @param int $tattooCounter (BYREF)
302
     * @param bool $autoAddRf
303
     * @param string|null $defaultRf
304
     * @param string|null $explicitRf
305
     * @throws \ReflectionException
306
     */
307 175
    protected function addElements($elements, &$tattooCounter, $autoAddRf, $defaultRf, $explicitRf = null)
308
    {
309 175
        if ($this->dataElementsMaster === null) {
310 175
            $this->dataElementsMaster = new DataElementsMaster();
311 70
        }
312
313
        //Only add a default RF element if there is no explicitly provided RF element!
314 175
        $hasReceivedFromElement = false;
315
316 175
        foreach ($elements as $element) {
317 130
            if ($element instanceof Element) {
318 130
                $this->dataElementsMaster->dataElementsIndiv[] = $this->createElement(
319 130
                    $element,
320 52
                    $tattooCounter
321 52
                );
322 48
            }
323
324 120
            if ($element instanceof ReceivedFrom) {
325 74
                $hasReceivedFromElement = true;
326 2
            }
327 66
        }
328
329 165
        if (!$hasReceivedFromElement) {
330 160
            $this->addReceivedFrom(
331 160
                $explicitRf,
332 128
                $autoAddRf,
333 128
                $defaultRf,
334 64
                $tattooCounter
335 64
            );
336 64
        }
337 165
    }
338
339
    /**
340
     * @param Element $element
341
     * @param int $tattooCounter (BYREF)
342
     * @throws InvalidArgumentException
343
     * @return DataElementsIndiv
344
     * @throws \ReflectionException
345
     */
346 200
    protected function createElement($element, &$tattooCounter)
347
    {
348 200
        $tattooCounter++;
349
350 200
        $createdElement = new DataElementsIndiv($element, $tattooCounter);
351
352 200
        if (!empty($element->references)) {
353
            $createdElement->referenceForDataElement = new ReferenceForDataElement($element->references);
354 190
        }
355 35
356 14
        return $createdElement;
357
    }
358 190
359
    /**
360
     * Add Received From field - if needed.
361
     *
362
     * @param string|null $explicitRf Explicitly provided RF string on request.
363
     * @param bool $doAutoAdd Wether to automatically add an RF field.
364
     * @param string|null $defaultRf The default RF string set in the client.
365
     * @param int $tattooCounter (BYREF)
366
     * @throws \ReflectionException
367
     */
368
    protected function addReceivedFrom($explicitRf, $doAutoAdd, $defaultRf, &$tattooCounter)
369
    {
370 190
        if ($this->dataElementsMaster === null) {
371
            $this->dataElementsMaster = new DataElementsMaster();
372 190
        }
373 30
374 12
        if (!empty($explicitRf) || ($doAutoAdd && !empty($defaultRf))) {
375
            //Set a received from if explicitly provided or if auto received from is enabled
376 190
377
            $tattooCounter++;
378
379 160
            $rfToAdd = (!empty($explicitRf)) ? $explicitRf : $defaultRf;
380
381 160
            $this->dataElementsMaster->dataElementsIndiv[] = $this->createElement(
382
                new ReceivedFrom(['receivedFrom' => $rfToAdd]),
383 160
                $tattooCounter
384 160
            );
385 64
        }
386 64
    }
387
}
388