Completed
Push — develop ( 13e4b5...a8951f )
by Dieter
06:26
created

AddMultiElements::createTraveller()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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\Segment;
28
use Amadeus\Client\RequestOptions\Pnr\Traveller;
29
use Amadeus\Client\RequestOptions\Pnr\TravellerGroup;
30
use Amadeus\Client\RequestOptions\PnrAddMultiElementsBase;
31
use Amadeus\Client\RequestOptions\PnrAddMultiElementsOptions;
32
use Amadeus\Client\RequestOptions\PnrCreatePnrOptions;
33
use Amadeus\Client\Struct\BaseWsMessage;
34
use Amadeus\Client\Struct\InvalidArgumentException;
35
use Amadeus\Client\Struct\Pnr\AddMultiElements\Accounting;
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\ElementManagementData;
40
use Amadeus\Client\Struct\Pnr\AddMultiElements\ElementManagementItinerary;
41
use Amadeus\Client\Struct\Pnr\AddMultiElements\Fop;
42
use Amadeus\Client\Struct\Pnr\AddMultiElements\FopExtension;
43
use Amadeus\Client\Struct\Pnr\AddMultiElements\FormOfPayment;
44
use Amadeus\Client\Struct\Pnr\AddMultiElements\FreetextData;
45
use Amadeus\Client\Struct\Pnr\AddMultiElements\FreetextDetail;
46
use Amadeus\Client\Struct\Pnr\AddMultiElements\FrequentTravellerData;
47
use Amadeus\Client\Struct\Pnr\AddMultiElements\ItineraryInfo;
48
use Amadeus\Client\Struct\Pnr\AddMultiElements\MiscellaneousRemark;
49
use Amadeus\Client\Struct\Pnr\AddMultiElements\NewFopsDetails;
50
use Amadeus\Client\Struct\Pnr\AddMultiElements\OriginDestinationDetails;
51
use Amadeus\Client\Struct\Pnr\AddMultiElements\Reference;
52
use Amadeus\Client\Struct\Pnr\AddMultiElements\ReferenceForDataElement;
53
use Amadeus\Client\Struct\Pnr\AddMultiElements\ReferenceForSegment;
54
use Amadeus\Client\Struct\Pnr\AddMultiElements\ServiceRequest;
55
use Amadeus\Client\Struct\Pnr\AddMultiElements\StructuredAddress;
56
use Amadeus\Client\Struct\Pnr\AddMultiElements\TicketElement;
57
use Amadeus\Client\Struct\Pnr\AddMultiElements\TravellerInfo;
58
59
/**
60
 * Structure class for representing the PNR_AddMultiElements request message
61
 *
62
 * @package Amadeus\Client\Struct\Pnr
63
 * @author Dieter Devlieghere <[email protected]>
64
 */
65
class AddMultiElements extends BaseWsMessage
66
{
67
    /**
68
     * @var AddMultiElements\ReservationInfo
69
     */
70
    public $reservationInfo;
71
    /**
72
     * @var AddMultiElements\PnrActions
73
     */
74
    public $pnrActions;
75
    /**
76
     * @var AddMultiElements\TravellerInfo[]
77
     */
78
    public $travellerInfo = [];
79
    /**
80
     * @var AddMultiElements\OriginDestinationDetails[]
81
     */
82
    public $originDestinationDetails = [];
83
    /**
84
     * @var AddMultiElements\DataElementsMaster
85
     */
86
    public $dataElementsMaster;
87
88
    /**
89
     * Create PNR_AddMultiElements object
90
     *
91
     * @param PnrAddMultiElementsBase|null $params
92
     */
93
    public function __construct(PnrAddMultiElementsBase $params = null)
94
    {
95
        if (!is_null($params)) {
96
            if ($params instanceof PnrCreatePnrOptions) {
97
                $this->loadCreatePnr($params);
98
            } elseif ($params instanceof PnrAddMultiElementsOptions) {
99
                $this->loadBare($params);
100
            }
101
        }
102
    }
103
104
    /**
105
     * PNR_AddMultiElements call which only adds requested data to the message
106
     *
107
     * For doing specific actions like ignoring or saving PNR.
108
     *
109
     * @param PnrAddMultiElementsOptions $params
110
     */
111
    protected function loadBare(PnrAddMultiElementsOptions $params)
112
    {
113
        if (!is_null($params->actionCode)) {
114
            $this->pnrActions = new AddMultiElements\PnrActions(
115
                $params->actionCode
116
            );
117
        }
118
119
        if (!is_null($params->recordLocator)) {
120
            $this->reservationInfo = new AddMultiElements\ReservationInfo($params->recordLocator);
121
        }
122
123
        $tattooCounter = 0;
124
125
        $this->addTravellers($params->travellers);
126
127
        if (!empty($params->tripSegments)) {
128
            $this->addSegments($params->tripSegments, $tattooCounter);
129
        }
130
131
        if (!empty($params->elements)) {
132
            $this->addElements(
133
                $params->elements,
134
                $tattooCounter,
135
                $params->receivedFrom
136
            );
137
        } elseif (!is_null($params->receivedFrom)) {
138
            if ($this->dataElementsMaster === null) {
139
                $this->dataElementsMaster = new DataElementsMaster();
140
            }
141
142
            $tattooCounter++;
143
144
            $this->dataElementsMaster->dataElementsIndiv[] = $this->createElement(
145
                new ReceivedFrom(['receivedFrom' => $params->receivedFrom]), $tattooCounter
146
            );
147
        }
148
    }
149
150
    /**
151
     * Make PNR_AddMultiElements structure from a PnrCreatePnrOptions input.
152
     *
153
     * @throws InvalidArgumentException When invalid input is provided
154
     * @param PnrCreatePnrOptions $params
155
     */
156
    protected function loadCreatePnr(PnrCreatePnrOptions $params)
157
    {
158
        $this->pnrActions = new AddMultiElements\PnrActions(
159
            $params->actionCode
160
        );
161
162
        $tattooCounter = 0;
163
164
        if ($params->travellerGroup !== null) {
165
            $this->addTravellerGroup($params->travellerGroup);
166
        } else {
167
            $this->addTravellers($params->travellers);
168
        }
169
170
        $this->addSegments($params->tripSegments, $tattooCounter);
171
172
        $this->addElements(
173
            $params->elements,
174
            $tattooCounter,
175
            $params->receivedFrom
176
        );
177
    }
178
179
    /**
180
     * @param Segment[] $segments
181
     * @param int $tattooCounter
182
     */
183
    protected function addSegments($segments, &$tattooCounter)
184
    {
185
        $tmpOrigDest = new OriginDestinationDetails();
186
187
        foreach ($segments as $segment) {
188
            $tmpOrigDest->itineraryInfo[] = $this->createSegment($segment, $tattooCounter);
189
        }
190
191
        $this->originDestinationDetails[] = $tmpOrigDest;
192
    }
193
194
    /**
195
     * @param Segment $segment
196
     * @param $tattooCounter
197
     * @return ItineraryInfo
198
     */
199
    protected function createSegment($segment, &$tattooCounter)
200
    {
201
        $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...
202
203
        $tattooCounter++;
204
205
        $reflect = new \ReflectionClass($segment);
206
        $segmentType = $reflect->getShortName();
207
208
        switch ($segmentType) {
209
            case 'Miscellaneous':
210
                /** @var Segment\Miscellaneous $segment */
211
                $createdSegment = new ItineraryInfo($tattooCounter, ElementManagementItinerary::SEGMENT_MISCELLANEOUS);
212
                $createdSegment->airAuxItinerary = new AirAuxItinerary($segmentType, $segment);
213
                break;
214
            case 'Air':
215
                throw new \RuntimeException('NOT YET IMPLEMENTED');
216
                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...
217
            case 'Ghost':
218
                throw new \RuntimeException('NOT YET IMPLEMENTED');
219
                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...
220
            default:
221
                throw new InvalidArgumentException('Segment type ' . $segmentType . ' is not supported');
222
                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...
223
        }
224
225
        if (count($segment->references) > 0) {
226
            $createdSegment->referenceForSegment = new ReferenceForSegment();
227
            foreach ($segment->references as $singleRef) {
228
                $createdSegment->referenceForSegment->reference[] = new Reference($singleRef->type, $singleRef->id);
229
            }
230
        }
231
232
        return $createdSegment;
233
    }
234
235
236
    /**
237
     * @param Traveller[] $travellers
238
     */
239
    protected function addTravellers($travellers)
240
    {
241
        foreach ($travellers as $traveller) {
242
            $this->travellerInfo[] = $this->createTraveller($traveller);
243
        }
244
    }
245
246
    /**
247
     * @param Traveller $traveller
248
     * @return TravellerInfo
249
     */
250
    protected function createTraveller($traveller)
251
    {
252
        return new TravellerInfo($traveller);
253
    }
254
255
    /**
256
     * @param TravellerGroup $group
257
     */
258
    protected function addTravellerGroup($group)
259
    {
260
        $this->travellerInfo[] = new TravellerInfo(null, $group);
261
262
        $this->addTravellers($group->travellers);
263
    }
264
265
    /**
266
     * @param Element[] $elements
267
     * @param int $tattooCounter (BYREF)
268
     * @param string|null $receivedFromString
269
     */
270
    protected function addElements($elements, &$tattooCounter, $receivedFromString = null)
271
    {
272
        if ($this->dataElementsMaster === null) {
273
            $this->dataElementsMaster = new DataElementsMaster();
274
        }
275
276
        //Only add a default RF element if there is no explicitly provided RF element!
277
        $explicitRf = false;
278
279
        foreach ($elements as $element) {
280
            if ($element instanceof Element) {
281
                $this->dataElementsMaster->dataElementsIndiv[] = $this->createElement($element, $tattooCounter);
282
            }
283
284
            if ($element instanceof ReceivedFrom) {
285
                $explicitRf = true;
286
            }
287
        }
288
289
        if ($receivedFromString !== null && !$explicitRf) {
290
            $this->dataElementsMaster->dataElementsIndiv[] = $this->createElement(
291
                new ReceivedFrom(['receivedFrom' => $receivedFromString]), $tattooCounter
292
            );
293
        }
294
    }
295
296
    /**
297
     * @param Element $element
298
     * @param int $tattooCounter (BYREF)
299
     * @throws InvalidArgumentException
300
     * @return DataElementsIndiv
301
     */
302
    protected function createElement($element, &$tattooCounter)
303
    {
304
        $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...
305
306
        $tattooCounter++;
307
308
        $reflect = new \ReflectionClass($element);
309
        $elementType = $reflect->getShortName();
310
311
        switch ($elementType) {
312
            case 'Contact':
313
                /** @var Element\Contact $element */
314
                $createdElement = new DataElementsIndiv(ElementManagementData::SEGNAME_CONTACT_ELEMENT, $tattooCounter);
315
                $createdElement->freetextData = new FreetextData(
316
                    $element->value,
317
                    $element->type
318
                );
319
                break;
320
            case 'FormOfPayment':
321
                /** @var Element\FormOfPayment $element */
322
                $createdElement = new DataElementsIndiv(ElementManagementData::SEGNAME_FORM_OF_PAYMENT, $tattooCounter);
323
                $createdElement->formOfPayment = new FormOfPayment($element->type);
324
                if ($element->type === Fop::IDENT_CREDITCARD) {
325
                    $createdElement->formOfPayment->fop->creditCardCode = $element->creditCardType;
326
                    $createdElement->formOfPayment->fop->accountNumber = $element->creditCardNumber;
327
                    $createdElement->formOfPayment->fop->expiryDate = $element->creditCardExpiry;
328
                    if (!is_null($element->creditCardCvcCode)) {
329
                        $ext = new FopExtension(1);
330
                        $ext->newFopsDetails = new NewFopsDetails();
331
                        $ext->newFopsDetails->cvData = $element->creditCardCvcCode;
332
                        $createdElement->fopExtension[] = $ext;
333
                    }
334
                } elseif ($element->type === Fop::IDENT_MISC && $element->freeText != "NONREF") {
335
                    $createdElement->formOfPayment->fop->freetext = $element->freeText;
336
                } elseif ($element->type === Fop::IDENT_MISC && $element->freeText === "NONREF") {
337
                    $createdElement->fopExtension[] = new FopExtension(1);
338
                } elseif ($element->type === Fop::IDENT_CHECK) {
339
                    throw new \RuntimeException("FOP CHECK NOT YET IMPLEMENTED");
340
                }
341
                break;
342
            case 'MiscellaneousRemark':
343
                /** @var Element\MiscellaneousRemark $element */
344
                $createdElement = new DataElementsIndiv(ElementManagementData::SEGNAME_GENERAL_REMARK, $tattooCounter);
345
                $createdElement->miscellaneousRemark = new MiscellaneousRemark(
346
                    $element->text,
347
                    $element->type,
348
                    $element->category
349
                );
350
                break;
351
            case 'ReceivedFrom':
352
                /** @var Element\ReceivedFrom $element */
353
                $createdElement = new DataElementsIndiv(ElementManagementData::SEGNAME_RECEIVE_FROM, $tattooCounter);
354
                $createdElement->freetextData = new FreetextData(
355
                    $element->receivedFrom,
356
                    FreetextDetail::TYPE_RECEIVE_FROM
357
                );
358
                break;
359
            case 'ServiceRequest':
360
                /** @var Element\ServiceRequest $element */
361
                $createdElement = new DataElementsIndiv(ElementManagementData::SEGNAME_SPECIAL_SERVICE_REQUEST, $tattooCounter);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
362
                $createdElement->serviceRequest = new ServiceRequest($element);
363
                break;
364
            case 'Ticketing':
365
                /** @var Element\Ticketing $element */
366
                $createdElement = new DataElementsIndiv(ElementManagementData::SEGNAME_TICKETING_ELEMENT, $tattooCounter);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
367
                $createdElement->ticketElement = new TicketElement($element);
368
                break;
369
            case 'AccountingInfo':
370
                /** @var Element\AccountingInfo $element */
371
                $createdElement = new DataElementsIndiv(ElementManagementData::SEGNAME_ACCOUNTING_INFORMATION, $tattooCounter);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
372
                $createdElement->accounting = new Accounting($element);
373
                break;
374
            case 'Address':
375
                /** @var Element\Address $element */
376
                $createdElement = new DataElementsIndiv($element->type, $tattooCounter);
377
                if ($element->type === ElementManagementData::SEGNAME_ADDRESS_BILLING_UNSTRUCTURED || $element->type === ElementManagementData::SEGNAME_ADDRESS_MAILING_UNSTRUCTURED) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 183 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
378
                    $createdElement->freetextData = new FreetextData($element->freeText, FreetextDetail::TYPE_MAILING_ADDRESS);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
379
                } else {
380
                    $createdElement->structuredAddress = new StructuredAddress($element);
381
                }
382
                break;
383
            case 'FrequentFlyer':
384
                /** @var Element\FrequentFlyer $element */
385
                $createdElement = new DataElementsIndiv(ElementManagementData::SEGNAME_SPECIAL_SERVICE_REQUEST, $tattooCounter);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
386
                $createdElement->serviceRequest = new ServiceRequest();
387
                $createdElement->serviceRequest->ssr->type = 'FQTV';
388
                $createdElement->serviceRequest->ssr->companyId = $element->airline;
389
                $createdElement->frequentTravellerData = new FrequentTravellerData($element);
390
                break;
391
            default:
392
                throw new InvalidArgumentException('Element type ' . $elementType . ' is not supported');
393
        }
394
395
        if (!empty($element->references)) {
396
            $createdElement->referenceForDataElement = new ReferenceForDataElement($element->references);
397
        }
398
399
        return $createdElement;
400
    }
401
}
402