DataElementsIndiv::loadElement()   F
last analyzed

Complexity

Conditions 32
Paths 35

Size

Total Lines 141
Code Lines 104

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 113
CRAP Score 32

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 104
c 1
b 0
f 0
dl 0
loc 141
ccs 113
cts 113
cp 1
rs 3.3333
cc 32
nc 35
nop 2
crap 32

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\AddMultiElements;
24
25
use Amadeus\Client\RequestOptions\Pnr\Element;
26
use Amadeus\Client\Struct\InvalidArgumentException;
27
use Amadeus\Client\Struct\WsMessageUtility;
28
29
/**
30
 * DataElementsIndiv
31
 *
32
 * all the others segments
33
 *
34
 * @package Amadeus\Client\Struct\Pnr\AddMultiElements
35
 * @author Dieter Devlieghere <[email protected]>
36
 */
37
class DataElementsIndiv extends WsMessageUtility
38
{
39
    /**
40
     * To specify the PNR segments/elements references and action to apply
41
     *
42
     * @var ElementManagementData
43
     */
44
    public $elementManagementData;
45
    /**
46
     * @var PnrSecurity
47
     */
48
    public $pnrSecurity;
49
    /**
50
     * @var Accounting
51
     */
52
    public $accounting;
53
    /**
54
     * To specify different kinds of remarks
55
     *
56
     * @var MiscellaneousRemark
57
     */
58
    public $miscellaneousRemark;
59
    /**
60
     * @var ServiceRequest
61
     */
62
    public $serviceRequest;
63
    public $dateAndTimeInformation;
64
    /**
65
     * @var TourCode
66
     */
67
    public $tourCode;
68
    /**
69
     * To specify an Amadeus PNR Ticket element
70
     *
71
     * @var TicketElement
72
     */
73
    public $ticketElement;
74
    /**
75
     * To provide free form or coded long text information.
76
     *
77
     * @var FreetextData
78
     */
79
    public $freetextData;
80
    /**
81
     * @var StructuredAddress
82
     */
83
    public $structuredAddress;
84
    public $optionElement;
85
    public $printer;
86
    /**
87
     * This group handles Seat Request with possibly rail preferences
88
     *
89
     * @var SeatGroup
90
     */
91
    public $seatGroup;
92
    public $entity;
93
    public $fareElement;
94
    public $fareDiscount;
95
    public $manualFareDocument;
96
    /**
97
     * @var Commission
98
     */
99
    public $commission;
100
    public $originalIssue;
101
    /**
102
     * To convey details describing the form of payment
103
     *
104
     * @var FormOfPayment
105
     */
106
    public $formOfPayment;
107
    /**
108
     * To convey additional details of the form of payment
109
     *
110
     * @var FopExtension[]
111
     */
112
    public $fopExtension = [];
113
    /**
114
     * To convey:
115
     * - The FOP service details
116
     * - The Corporate Security option for Remarks
117
     * - The Timestamp indicator for Remarks
118
     *
119
     * @var ServiceDetails[]
120
     */
121
    public $serviceDetails = [];
122
    public $frequentTravellerVerification;
123
    public $ticketingCarrier;
124
    public $farePrintOverride;
125
    /**
126
     * Frequent Flyer info
127
     *
128
     * @var FrequentTravellerData
129
     */
130
    public $frequentTravellerData;
131
    public $accessLevel;
132
    /**
133
     * To provide specific reference identification
134
     *
135
     * @var ReferenceForDataElement
136
     */
137
    public $referenceForDataElement;
138
139
    /**
140
     * @param Element|string $element Either an element or an element name
141 200
     * @param int $tattoo Unique tattoo number for this element
142
     * @throws \ReflectionException
143 200
     */
144 200
    public function __construct($element, $tattoo)
145 200
    {
146
        if ($element instanceof Element) {
147 200
            $reflect = new \ReflectionClass($element);
148 200
            $elementType = $reflect->getShortName();
149 80
150 80
            $this->elementManagementData = new ElementManagementData(
151
                $this->makeSegmentNameForRequestElement($elementType, $element),
152 200
                $tattoo
153 79
            );
154 5
155 5
            $this->loadElement($element, $elementType);
156 2
        } elseif (is_string($element)) {
0 ignored issues
show
introduced by
The condition is_string($element) is always true.
Loading history...
157 2
            $this->elementManagementData = new ElementManagementData(
158 2
                $element,
159 190
                $tattoo
160
            );
161
        }
162
    }
163
164
    /**
165 200
     * @param Element $element
166
     * @param string $elementType
167 80
     */
168 200
    protected function loadElement($element, $elementType)
169
    {
170 30
        switch ($elementType) {
171 30
            case 'Contact':
172 30
                /** @var Element\Contact $element */
173 12
                $this->freetextData = new FreetextData(
174 30
                    $element->value,
175 200
                    $element->type
176
                );
177 30
                break;
178 30
            case 'FormOfPayment':
179 10
                /** @var Element\FormOfPayment $element */
180 10
                $this->formOfPayment = new FormOfPayment($element->type);
181 10
                if ($element->type === Fop::IDENT_CREDITCARD) {
182 10
                    $this->formOfPayment->fop->creditCardCode = $element->creditCardType;
183 10
                    $this->formOfPayment->fop->accountNumber = $element->creditCardNumber;
184 10
                    $this->formOfPayment->fop->expiryDate = $element->creditCardExpiry;
185 10
                    if ($element->freeText && $element->freeText != "") {
186 10
                        $this->formOfPayment->fop->freetext = $element->freeText;
187 4
                    }
188 4
                    if ($this->checkAnyNotEmpty($element->creditCardCvcCode, $element->creditCardHolder)) {
189 24
                        $this->fopExtension[] = new FopExtension(
190 5
                            1,
191 17
                            $element->creditCardCvcCode,
192 5
                            $element->creditCardHolder
193 12
                        );
194 5
                    }
195
                } elseif ($element->type === Fop::IDENT_CASH && $element->freeText != "") {
196
                    $this->formOfPayment->fop->freetext = $element->freeText;
197 25
                } elseif ($element->type === Fop::IDENT_MISC && $element->freeText != "NONREF") {
198 5
                    $this->formOfPayment->fop->freetext = $element->freeText;
199 3
                } elseif ($element->type === Fop::IDENT_MISC && $element->freeText === "NONREF") {
200 2
                    $this->fopExtension[] = new FopExtension(1);
201 2
                } elseif ($element->type === Fop::IDENT_CHECK) {
202 25
                    throw new \RuntimeException("FOP CHECK NOT YET IMPLEMENTED");
203 195
                }
204
205 5
                if ($element->isServiceFee) {
206 5
                    $this->serviceDetails[] = new ServiceDetails(
207 5
                        StatusDetails::IND_SERVICEFEE
208 5
                    );
209 2
                }
210 5
                break;
211 195
            case 'MiscellaneousRemark':
212
                /** @var Element\MiscellaneousRemark $element */
213 165
                $this->miscellaneousRemark = new MiscellaneousRemark(
214 165
                    $element->text,
215 99
                    $element->type,
216 66
                    $element->category
217 165
                );
218 95
                break;
219
            case 'ReceivedFrom':
220 5
                /** @var Element\ReceivedFrom $element */
221 5
                $this->freetextData = new FreetextData(
222 95
                    $element->receivedFrom,
223
                    FreetextDetail::TYPE_RECEIVE_FROM
224 30
                );
225 30
                break;
226 65
            case 'ServiceRequest':
227
                /** @var Element\ServiceRequest $element */
228 5
                $this->serviceRequest = new ServiceRequest($element);
229 5
                break;
230 60
            case 'Ticketing':
231
                /** @var Element\Ticketing $element */
232 15
                $this->ticketElement = new TicketElement($element);
233 11
                break;
234 6
            case 'AccountingInfo':
235 10
                /** @var Element\AccountingInfo $element */
236 10
                $this->accounting = new Accounting($element);
237 6
                break;
238 4
            case 'Address':
239 4
                /** @var Element\Address $element */
240 5
                if ($element->type === ElementManagementData::SEGNAME_ADDRESS_BILLING_UNSTRUCTURED ||
241
                    $element->type === ElementManagementData::SEGNAME_ADDRESS_MAILING_UNSTRUCTURED
242 15
                ) {
243 45
                    $this->freetextData = new FreetextData(
244
                        $element->freeText,
245 5
                        FreetextDetail::TYPE_MAILING_ADDRESS
246 5
                    );
247 5
                } else {
248 5
                    $this->structuredAddress = new StructuredAddress($element);
249 5
                }
250 40
                break;
251
            case 'FrequentFlyer':
252 5
                /** @var Element\FrequentFlyer $element */
253 5
                $this->serviceRequest = new ServiceRequest();
254 3
                $this->serviceRequest->ssr->type = 'FQTV';
255 2
                $this->serviceRequest->ssr->companyId = $element->airline;
256 5
                $this->frequentTravellerData = new FrequentTravellerData($element);
257 5
                break;
258 5
            case 'OtherServiceInfo':
259 35
                /** @var Element\OtherServiceInfo $element */
260
                $this->freetextData = new FreetextData(
261 5
                    $element->freeText,
262 5
                    FreetextDetail::TYPE_OSI_ELEMENT
263 30
                );
264
                $this->freetextData->freetextDetail->companyId = $element->airline;
265 15
                $this->freetextData->freetextDetail->subjectQualifier = FreetextDetail::QUALIFIER_LITERALTEXT;
266 15
                break;
267 15
            case 'ManualCommission':
268
                /** @var Element\ManualCommission $element */
269 5
                $this->commission = new Commission($element);
270 5
                break;
271 10
            case 'SeatRequest':
272
                /** @var Element\SeatRequest $element */
273 5
                $this->seatGroup = new SeatGroup($element);
274 5
                break;
275 5
            case 'TourCode':
276 5
                /** @var Element\TourCode $element */
277 2
                $this->tourCode = new TourCode($element);
278 5
                break;
279 2
            case 'ManualIssuedTicket':
280 5
                /** @var Element\ManualIssuedTicket $element */
281 2
                $this->manualFareDocument = new ManualDocumentRegistration(
282 190
                    $element->passengerType,
283
                    $element->companyId,
284
                    $element->ticketNumber
285
                );
286
                break;
287
            case 'ScheduleChange':
288
                /** @var Element\ScheduleChange $element */
289
                $this->freetextData = new FreetextData(
290
                    'SCHGTOOL',
291 200
                    FreetextDetail::TYPE_RECEIVE_FROM
292
                );
293 200
                break;
294
            case 'FareMiscellaneousInformation':
295
                /** @var Element\FareMiscellaneousInformation $element */
296 200
                $this->fareElement = new FareElement(
297 80
                    $element->indicator,
298 80
                    $element->passengerType,
299 80
                    $element->freeText,
300 80
                    $element->officeId,
301 80
                );
302 80
                break;
303 80
            case 'PnrSecurity':
304 80
                /** @var Element\PnrSecurity $element */
305 80
                $this->pnrSecurity = new PnrSecurity($element);
306 80
                break;
307 80
            default:
308 80
                throw new InvalidArgumentException('Element type '.$elementType.' is not supported');
309
        }
310 80
    }
311
312 200
    /**
313 195
     * Create the correct element identifier for a given element
314
     *
315 195
     * @param string $elementType
316
     * @param Element $element
317 15
     * @return string
318 6
     */
319 78
    protected function makeSegmentNameForRequestElement($elementType, $element)
320
    {
321 200
        $elementName = '';
322
323
        $sourceArray = [
324
            'Contact' => ElementManagementData::SEGNAME_CONTACT_ELEMENT,
325
            'FormOfPayment' => ElementManagementData::SEGNAME_FORM_OF_PAYMENT,
326
            'MiscellaneousRemark' => ElementManagementData::SEGNAME_GENERAL_REMARK,
327
            'ReceivedFrom' => ElementManagementData::SEGNAME_RECEIVE_FROM,
328
            'ServiceRequest' => ElementManagementData::SEGNAME_SPECIAL_SERVICE_REQUEST,
329
            'Ticketing' => ElementManagementData::SEGNAME_TICKETING_ELEMENT,
330
            'AccountingInfo' => ElementManagementData::SEGNAME_ACCOUNTING_INFORMATION,
331
            'Address' => null, // Special case - the type is a parameter.
332
            'FrequentFlyer' => ElementManagementData::SEGNAME_SPECIAL_SERVICE_REQUEST,
333
            'OtherServiceInfo' => ElementManagementData::SEGNAME_OTHER_SERVICE_INFORMATION,
334
            'ManualCommission' => ElementManagementData::SEGNAME_COMMISSION,
335
            'SeatRequest' => ElementManagementData::SEGNAME_SEAT_REQUEST,
336
            'TourCode' => ElementManagementData::SEGNAME_TOUR_CODE,
337
            'ManualIssuedTicket' => ElementManagementData::SEGNAME_MANUAL_DOCUMENT_REGISTRATION_WITH_ET_NUMBER,
338
            'ScheduleChange' => ElementManagementData::SEGNAME_RECEIVE_FROM,
339
            'FareMiscellaneousInformation' => null, // Special case - the type is a parameter.
340
            'PnrSecurity' => ElementManagementData::SEGNAME_INDIVIDUAL_SECURITY,
341
        ];
342
343
        if (array_key_exists($elementType, $sourceArray)) {
344
            $elementName = $sourceArray[$elementType];
345
346
            if ($elementType === 'Address') {
347
                /** @var Element\Address $element */
348
                $elementName = $element->type;
349
            }
350
351
            if ($elementType === 'FareMiscellaneousInformation') {
352
                /** @var Element\FareMiscellaneousInformation $element */
353
354
                switch ($element->indicator) {
355
                    case Element\FareMiscellaneousInformation::GENERAL_INDICATOR_FS:
356
                        $elementName = ElementManagementData::SEGNAME_MISC_TICKET_INFO;
357
                        break;
358
                    case Element\FareMiscellaneousInformation::GENERAL_INDICATOR_FE:
359
                        $elementName = ElementManagementData::SEGNAME_ENDORSEMENT;
360
                        break;
361
                    case Element\FareMiscellaneousInformation::GENERAL_INDICATOR_FK:
362
                        $elementName = ElementManagementData::SEGNAME_AIR_OFFICE_ID;
363
                        break;
364
                    case Element\FareMiscellaneousInformation::GENERAL_INDICATOR_FZ:
365
                        $elementName = ElementManagementData::SEGNAME_MISC_INFO;
366
                        break;
367
                }
368
            }
369
        }
370
371
        return $elementName;
372
    }
373
}
374