makePricingOptionForSelection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 10
cc 2
nc 2
nop 1
crap 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\Ticket;
24
25
use Amadeus\Client\RequestOptions\Fare\PricePnr\AwardPricing;
26
use Amadeus\Client\RequestOptions\Fare\PricePnr\ExemptTax;
27
use Amadeus\Client\RequestOptions\Fare\PricePnr\FareBasis;
28
use Amadeus\Client\RequestOptions\Fare\PricePnr\Tax;
29
use Amadeus\Client\RequestOptions\Ticket\ExchangeInfoOptions;
30
use Amadeus\Client\RequestOptions\Ticket\MultiRefOpt;
31
use Amadeus\Client\RequestOptions\Ticket\PaxSegRef;
32
use Amadeus\Client\RequestOptions\TicketRepricePnrWithBookingClassOptions;
33
use Amadeus\Client\Struct\BaseWsMessage;
34
use Amadeus\Client\Struct\Fare\PricePnr13\CarrierInformation;
35
use Amadeus\Client\Struct\Fare\PricePnr13\Currency;
36
use Amadeus\Client\Struct\Fare\PricePnr13\FrequentFlyerInformation;
37
use Amadeus\Client\Struct\Fare\PricePnr13\FrequentTravellerDetails;
38
use Amadeus\Client\Struct\Fare\PricePnr13\LocationInformation;
39
use Amadeus\Client\Struct\Fare\PricePnr13\OptionDetail;
40
use Amadeus\Client\Struct\Fare\PricePnr13\PaxSegTstReference;
41
use Amadeus\Client\Struct\Fare\PricePnr13\PenDisInformation;
42
use Amadeus\Client\Struct\Fare\PricePnr13\TaxData;
43
use Amadeus\Client\Struct\Fare\PricePnr13\TaxInformation;
44
use Amadeus\Client\Struct\Ticket\RepricePnrWithBookingClass\ExchangeInformationGroup;
45
use Amadeus\Client\Struct\Ticket\RepricePnrWithBookingClass\PricingOption;
46
use Amadeus\Client\Struct\Ticket\RepricePnrWithBookingClass\PricingOptionKey;
47
48
/**
49
 * Ticket_RepricePNRWithBookingClass request structure
50
 *
51
 * @package Amadeus\Client\Struct\Ticket
52
 * @author Dieter Devlieghere <[email protected]>
53
 */
54
class RepricePnrWithBookingClass extends BaseWsMessage
55
{
56
    /**
57
     * @var ExchangeInformationGroup[]
58
     */
59
    public $exchangeInformationGroup = [];
60
61
    /**
62
     * @var PricingOption[]
63
     */
64
    public $pricingOption = [];
65
66
    /**
67
     * RepricePnrWithBookingClass constructor.
68
     *
69
     * @param TicketRepricePnrWithBookingClassOptions $options
70
     */
71 95
    public function __construct($options)
72
    {
73 95
        if (!is_null($options)) {
74 95
            $this->loadExchangeInfo($options->exchangeInfo);
75
76 95
            $this->pricingOption = $this->loadPricingOptionsFromRequestOptions($options);
77 38
        }
78 95
    }
79
80
    /**
81
     * @param ExchangeInfoOptions[] $exchangeInfo
82
     */
83 95
    protected function loadExchangeInfo($exchangeInfo)
84
    {
85 95
        foreach ($exchangeInfo as $info) {
86 95
            if ($info instanceof ExchangeInfoOptions) {
87 95
                $this->exchangeInformationGroup[] = new ExchangeInformationGroup($info);
88 38
            }
89 38
        }
90 95
    }
91
92
    /**
93
     * @param TicketRepricePnrWithBookingClassOptions $options
94
     * @return PricingOption[]
95
     */
96 95
    protected function loadPricingOptionsFromRequestOptions(TicketRepricePnrWithBookingClassOptions $options)
97
    {
98 95
        $priceOptions = [];
99
100 95
        $priceOptions = $this->mergeOptions(
101 95
            $priceOptions,
102 95
            $this->makePricingOptionForCarrier(
103 95
                $options->validatingCarrier,
104 57
                PricingOptionKey::OPTION_VALIDATING_CARRIER
105 38
            )
106 38
        );
107
108 95
        $priceOptions = $this->mergeOptions(
109 95
            $priceOptions,
110 95
            $this->makePricingOptionForCarrier(
111 95
                $options->controllingCarrier,
112 57
                PricingOptionKey::OPTION_OVERRIDE_CONTROLLING_CARRIER
113 38
            )
114 38
        );
115
116 95
        $priceOptions = $this->mergeOptions(
117 95
            $priceOptions,
118 95
            $this->loadCorpUniFares($options->corporateUniFares, $options->awardPricing)
119 38
        );
120
121 95
        $priceOptions = $this->mergeOptions(
122 95
            $priceOptions,
123 95
            $this->makePricingOptionFareBasisOverride($options->pricingsFareBasis)
124 38
        );
125
126 95
        $priceOptions = $this->mergeOptions(
127 95
            $priceOptions,
128 95
            $this->loadTaxes($options->taxes)
129 38
        );
130
131 95
        $priceOptions = $this->mergeOptions(
132 95
            $priceOptions,
133 95
            $this->loadExemptTaxes($options->exemptTaxes)
134 38
        );
135
136 95
        $priceOptions = $this->mergeOptions(
137 95
            $priceOptions,
138 95
            $this->makePricingOptionForCurrencyOverride($options->currencyOverride)
139 38
        );
140
141 95
        $priceOptions = $this->mergeOptions(
142 95
            $priceOptions,
143 95
            $this->makePricingOptionForSelection($options->multiReferences)
144 38
        );
145
146 95
        $priceOptions = $this->mergeOptions(
147 95
            $priceOptions,
148 95
            $this->makeBreakpointOptions(
149 95
                $options->forceBreakPointRefs,
150 95
                $options->noBreakPointRefs
151 38
            )
152 38
        );
153
154 95
        $priceOptions = $this->mergeOptions(
155 95
            $priceOptions,
156 95
            $this->loadPaxDiscount($options->paxDiscountCodes, $options->paxDiscountCodeRefs)
157 38
        );
158
159 95
        $priceOptions = $this->mergeOptions(
160 95
            $priceOptions,
161 95
            $this->loadPointOverrides(
162 95
                $options->pointOfSaleOverride,
163 95
                $options->pointOfTicketingOverride
164 38
            )
165 38
        );
166
167 95
        $priceOptions = $this->mergeOptions(
168 95
            $priceOptions,
169 95
            $this->loadWaiverCode($options->waiverCode)
170 38
        );
171
172 95
        $priceOptions = $this->mergeOptions(
173 95
            $priceOptions,
174 95
            $this->loadOverrideReusableAmount($options->overrideReusableAmountRefs)
175 38
        );
176
177 95
        $priceOptions = $this->mergeOptions(
178 95
            $priceOptions,
179 95
            $this->makeOverrideOptions($options->overrideOptions, $priceOptions)
180 38
        );
181
182
        // All options processed, no options found:
183 95
        if (empty($priceOptions)) {
184 5
            $priceOptions[] = new PricingOption(PricingOptionKey::OPTION_NO_OPTION);
185 2
        }
186
187 95
        return $priceOptions;
188
    }
189
190
191
    /**
192
     * Merges Pricing options
193
     *
194
     * @param PricingOption[] $existingOptions
195
     * @param PricingOption[] $newOptions
196
     * @return PricingOption[] merged array
197
     */
198 95
    protected function mergeOptions($existingOptions, $newOptions)
199
    {
200 95
        if (!empty($newOptions)) {
201 90
            $existingOptions = array_merge(
202 90
                $existingOptions,
203 36
                $newOptions
204 36
            );
205 36
        }
206
207 95
        return $existingOptions;
208
    }
209
210
    /**
211
     * @param string|null $carrier
212
     * @param string $pricingOptionKey PricingOptionKey::OPTION_*
213
     * @return PricingOption[]
214
     */
215 95
    protected function makePricingOptionForCarrier($carrier, $pricingOptionKey)
216
    {
217 95
        $opt = [];
218
219 95
        if ($carrier !== null) {
220 10
            $po = new PricingOption($pricingOptionKey);
221
222 10
            $po->carrierInformation = new CarrierInformation($carrier);
223
224 10
            $opt[] = $po;
225 4
        }
226
227 95
        return $opt;
228
    }
229
230
    /**
231
     * @param MultiRefOpt[] $multiReferences
232
     * @return PricingOption[]
233
     */
234 95
    protected function makePricingOptionForSelection($multiReferences)
235
    {
236 95
        $opt = [];
237
238 95
        foreach ($multiReferences as $multiReference) {
239 20
            $opt[] = $this->makePricingOptionWithPaxSegTstRefs(
240 20
                $multiReference->references,
241 12
                PricingOptionKey::OPTION_PAX_SEG_LINE_TST_SELECTION
242 8
            );
243 38
        }
244
245 95
        return $opt;
246
    }
247
248
249
    /**
250
     * @param PaxSegRef[] $forceBreakPointRefs
251
     * @param PaxSegRef[] $noBreakPointRefs
252
     * @return PricingOption[]
253
     */
254 95
    protected function makeBreakpointOptions($forceBreakPointRefs, $noBreakPointRefs)
255
    {
256 95
        $opt = [];
257
258 95
        if (!empty($forceBreakPointRefs)) {
259 5
            $opt[] = $this->makePricingOptionWithPaxSegTstRefs(
260 5
                $forceBreakPointRefs,
261 3
                PricingOptionKey::OPTION_FORCE_FEE_BREAK_POINT
262 2
            );
263 2
        }
264
265 95
        if (!empty($noBreakPointRefs)) {
266 5
            $opt[] = $this->makePricingOptionWithPaxSegTstRefs(
267 5
                $noBreakPointRefs,
268 3
                PricingOptionKey::OPTION_NO_BREAKPOINT
269 2
            );
270 2
        }
271
272 95
        return $opt;
273
    }
274
275
    /**
276
     * @param PaxSegRef[] $refs
277
     * @param string $pricingOptionKey PricingOptionKey::OPTION_*
278
     * @return PricingOption
279
     */
280 35
    protected function makePricingOptionWithPaxSegTstRefs($refs, $pricingOptionKey)
281
    {
282 35
        $po = new PricingOption($pricingOptionKey);
283
284 35
        $po->paxSegTstReference = new PaxSegTstReference($refs);
285
286 35
        return $po;
287
    }
288
289
    /**
290
     * Load corporate unifares
291
     *
292
     * @param string[] $corporateUniFares
293
     * @param AwardPricing|null $awardPricing
294
     * @return PricingOption[]
295
     */
296 95
    protected function loadCorpUniFares($corporateUniFares, $awardPricing)
297
    {
298 95
        $opt = [];
299
300 95
        if (!empty($corporateUniFares)) {
301 5
            $po = new PricingOption(PricingOptionKey::OPTION_CORPORATE_UNIFARES);
302 5
            $po->optionDetail = new OptionDetail($corporateUniFares);
303 5
            $opt[] = $po;
304
305 5
            if (!empty($awardPricing)) {
306 5
                $opt[] = $this->loadAwardPricing($awardPricing);
307 2
            }
308 2
        }
309
310 95
        return $opt;
311
    }
312
313
    /**
314
     * @param AwardPricing $awardPricing
315
     * @return PricingOption
316
     */
317 5
    protected function loadAwardPricing($awardPricing)
318
    {
319 5
        $po = new PricingOption(PricingOptionKey::OPTION_AWARD);
320
321 5
        $po->carrierInformation = new CarrierInformation($awardPricing->carrier);
322
323 5
        $po->frequentFlyerInformation = new FrequentFlyerInformation();
324 5
        $po->frequentFlyerInformation->frequentTravellerDetails[] = new FrequentTravellerDetails(
325 5
            $awardPricing->tierLevel
326 2
        );
327
328 5
        return $po;
329
    }
330
331
    /**
332
     * @param FareBasis[] $pricingsFareBasis
333
     * @return PricingOption[]
334
     */
335 95
    protected function makePricingOptionFareBasisOverride($pricingsFareBasis)
336
    {
337 95
        $opt = [];
338
339 95
        if ($pricingsFareBasis !== null) {
0 ignored issues
show
introduced by
The condition $pricingsFareBasis !== null is always true.
Loading history...
340 95
            foreach ($pricingsFareBasis as $pricingFareBasis) {
341 10
                $po = new PricingOption(PricingOptionKey::OPTION_FARE_BASIS_SIMPLE_OVERRIDE);
342
343
                //Support for legacy fareBasisPrimaryCode to be removed when breaking BC:
344 10
                $po->optionDetail = new OptionDetail(
345 10
                    $pricingFareBasis->fareBasisPrimaryCode.$pricingFareBasis->fareBasisCode
0 ignored issues
show
Deprecated Code introduced by
The property Amadeus\Client\RequestOp...::$fareBasisPrimaryCode has been deprecated: put the full fare basis in $this->fareBasisCode ( Ignorable by Annotation )

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

345
                    /** @scrutinizer ignore-deprecated */ $pricingFareBasis->fareBasisPrimaryCode.$pricingFareBasis->fareBasisCode

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...
346 4
                );
347
348
                //Support for legacy segmentReference to be removed when breaking BC:
349 10
                $po->paxSegTstReference = new PaxSegTstReference(
350 10
                    $pricingFareBasis->references,
351 10
                    $pricingFareBasis->segmentReference
0 ignored issues
show
Deprecated Code introduced by
The property Amadeus\Client\RequestOp...asis::$segmentReference has been deprecated: use $this->references instead ( Ignorable by Annotation )

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

351
                    /** @scrutinizer ignore-deprecated */ $pricingFareBasis->segmentReference

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...
352 4
                );
353
354 10
                $opt[] = $po;
355 38
            }
356 38
        }
357
358 95
        return $opt;
359
    }
360
361
    /**
362
     * @param Tax[] $taxes
363
     * @return PricingOption[]
364
     */
365 95
    protected function loadTaxes($taxes)
366
    {
367 95
        $opt = [];
368
369 95
        if (!empty($taxes)) {
370 5
            $po = new PricingOption(PricingOptionKey::OPTION_ADD_TAX);
371
372 5
            foreach ($taxes as $tax) {
373 5
                $qualifier = (!empty($tax->amount)) ? TaxData::QUALIFIER_AMOUNT : TaxData::QUALIFIER_PERCENTAGE;
374 5
                $rate = (!empty($tax->amount)) ? $tax->amount : $tax->percentage;
375
376 5
                $po->taxInformation[] = new TaxInformation(
377 5
                    $tax->countryCode,
378 5
                    $tax->taxNature,
379 4
                    $qualifier,
380 2
                    $rate
381 2
                );
382 2
            }
383 5
            $opt[] = $po;
384 2
        }
385
386 95
        return $opt;
387
    }
388
389
    /**
390
     * @param ExemptTax[] $exemptTaxes
391
     * @return PricingOption[]
392
     */
393 95
    protected function loadExemptTaxes($exemptTaxes)
394
    {
395 95
        $opt = [];
396
397 95
        if (!empty($exemptTaxes)) {
398 5
            $po = new PricingOption(PricingOptionKey::OPTION_EXEMPT_TAXES);
399
400 5
            foreach ($exemptTaxes as $tax) {
401 5
                $po->taxInformation[] = new TaxInformation(
402 5
                    $tax->countryCode,
403 5
                    $tax->taxNature
404 2
                );
405 2
            }
406
407 5
            $opt[] = $po;
408 2
        }
409
410 95
        return $opt;
411
    }
412
413
    /**
414
     * @param string|null $currency
415
     * @return PricingOption[]
416
     */
417 95
    protected function makePricingOptionForCurrencyOverride($currency)
418
    {
419 95
        $opt = [];
420
421 95
        if ($currency !== null) {
422 10
            $po = new PricingOption(PricingOptionKey::OPTION_FARE_CURRENCY_OVERRIDE);
423
424 10
            $po->currency = new Currency($currency);
425
426 10
            $opt[] = $po;
427 4
        }
428
429 95
        return $opt;
430
    }
431
432
    /**
433
     * @param string[] $paxDiscount
434
     * @param PaxSegRef[] $paxDiscountCodeRefs
435
     * @return PricingOption[]
436
     */
437 95
    protected function loadPaxDiscount($paxDiscount, $paxDiscountCodeRefs)
438
    {
439 95
        $opt = [];
440
441 95
        if (!empty($paxDiscount)) {
442 5
            $po = new PricingOption(PricingOptionKey::OPTION_PASSENGER_DISCOUNT_PTC);
443
444 5
            $po->penDisInformation = new PenDisInformation(
445 5
                PenDisInformation::QUAL_DISCOUNT,
446 2
                $paxDiscount
447 2
            );
448
449 5
            if (!empty($paxDiscountCodeRefs)) {
450 5
                $po->paxSegTstReference = new PaxSegTstReference($paxDiscountCodeRefs);
451 2
            }
452
453 5
            $opt[] = $po;
454 2
        }
455
456 95
        return $opt;
457
    }
458
459
    /**
460
     * @param string|null $posOverride
461
     * @param string|null $potOverride
462
     * @return PricingOption[]
463
     */
464 95
    protected function loadPointOverrides($posOverride, $potOverride)
465
    {
466 95
        $opt = [];
467
468 95
        if (!empty($posOverride)) {
469 5
            $po = new PricingOption(PricingOptionKey::OPTION_POINT_OF_SALE_OVERRIDE);
470
471 5
            $po->locationInformation = new LocationInformation(
472 5
                LocationInformation::TYPE_POINT_OF_SALE,
473 2
                $posOverride
474 2
            );
475
476 5
            $opt[] = $po;
477 2
        }
478
479 95
        if (!empty($potOverride)) {
480 5
            $po2 = new PricingOption(PricingOptionKey::OPTION_POINT_OF_TICKETING_OVERRIDE);
481
482 5
            $po2->locationInformation = new LocationInformation(
483 5
                LocationInformation::TYPE_POINT_OF_TICKETING,
484 2
                $potOverride
485 2
            );
486
487 5
            $opt[] = $po2;
488 2
        }
489
490 95
        return $opt;
491
    }
492
493
494
    /**
495
     * @param string|null $waiverCode
496
     * @return PricingOption[]
497
     */
498 95
    protected function loadWaiverCode($waiverCode)
499
    {
500 95
        $opt = [];
501
502 95
        if (!empty($waiverCode)) {
503 5
            $po = new PricingOption(PricingOptionKey::OPTION_WAIVER_OPTION);
504
505 5
            $po->optionDetail = new OptionDetail($waiverCode);
506
507 5
            $opt[] = $po;
508 2
        }
509
510 95
        return $opt;
511
    }
512
513
    /**
514
     * @param PaxSegRef[] $overrideReusableAmountRefs
515
     * @return PricingOption[]
516
     */
517 95
    protected function loadOverrideReusableAmount($overrideReusableAmountRefs)
518
    {
519 95
        $opt = [];
520
521 95
        if (!empty($overrideReusableAmountRefs)) {
522 5
            $opt[] = $this->makePricingOptionWithPaxSegTstRefs(
523 5
                $overrideReusableAmountRefs,
524 3
                PricingOptionKey::OPTION_OVERRIDE_REUSABLE_AMOUNT
525 2
            );
526 2
        }
527
528 95
        return $opt;
529
    }
530
531
    /**
532
     * @param string[] $overrideOptions
533
     * @param PricingOption[] $priceOptions
534
     * @return PricingOption[]
535
     */
536 95
    protected function makeOverrideOptions($overrideOptions, $priceOptions)
537
    {
538 95
        $opt = [];
539
540 95
        foreach ($overrideOptions as $overrideOption) {
541 10
            if (!$this->hasPricingOption($overrideOption, $priceOptions)) {
542 10
                $opt[] = new PricingOption($overrideOption);
543 4
            }
544 38
        }
545
546 95
        return $opt;
547
    }
548
549
    /**
550
     * Avoid double pricing groups when combining an explicitly provided override option with a specific parameter
551
     * that uses the same override option.
552
     *
553
     * @param string $optionKey
554
     * @param PricingOption[] $priceOptions
555
     * @return bool
556
     */
557 10
    protected function hasPricingOption($optionKey, $priceOptions)
558
    {
559 10
        $found = false;
560
561 10
        foreach ($priceOptions as $pog) {
562 5
            if ($pog->pricingOptionKey->pricingOptionKey === $optionKey) {
563 5
                $found = true;
564 2
            }
565 4
        }
566
567 10
        return $found;
568
    }
569
}
570