Completed
Push — master ( fb702c...5242c7 )
by Dieter
06:48
created

PricePNRWithBookingClass13::loadCorpUniFares()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 16
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 9
nc 3
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\Fare;
24
25
use Amadeus\Client\RequestCreator\MessageVersionUnsupportedException;
26
use Amadeus\Client\RequestOptions\Fare\PricePnr\AwardPricing;
27
use Amadeus\Client\RequestOptions\Fare\PricePnr\ExemptTax;
28
use Amadeus\Client\RequestOptions\Fare\PricePnr\FareBasis;
29
use Amadeus\Client\RequestOptions\Fare\PricePnr\ObFee;
30
use Amadeus\Client\RequestOptions\Fare\PricePnr\PaxSegRef;
31
use Amadeus\Client\RequestOptions\Fare\PricePnr\Tax;
32
use Amadeus\Client\RequestOptions\FarePricePnrWithBookingClassOptions;
33
use Amadeus\Client\RequestOptions\FarePricePnrWithLowerFaresOptions as LowerFareOpt;
34
use Amadeus\Client\RequestOptions\FarePricePnrWithLowestFareOptions as LowestFareOpt;
35
use Amadeus\Client\Struct\BaseWsMessage;
36
use Amadeus\Client\Struct\Fare\PricePnr13\CarrierInformation;
37
use Amadeus\Client\Struct\Fare\PricePnr13\CriteriaDetails;
38
use Amadeus\Client\Struct\Fare\PricePnr13\Currency;
39
use Amadeus\Client\Struct\Fare\PricePnr13\DateInformation;
40
use Amadeus\Client\Struct\Fare\PricePnr13\DiscountPenaltyDetails;
41
use Amadeus\Client\Struct\Fare\PricePnr13\FirstCurrencyDetails;
42
use Amadeus\Client\Struct\Fare\PricePnr13\FrequentFlyerInformation;
43
use Amadeus\Client\Struct\Fare\PricePnr13\FrequentTravellerDetails;
44
use Amadeus\Client\Struct\Fare\PricePnr13\LocationInformation;
45
use Amadeus\Client\Struct\Fare\PricePnr13\OptionDetail;
46
use Amadeus\Client\Struct\Fare\PricePnr13\PaxSegTstReference;
47
use Amadeus\Client\Struct\Fare\PricePnr13\PenDisInformation;
48
use Amadeus\Client\Struct\Fare\PricePnr13\PricingOptionGroup;
49
use Amadeus\Client\Struct\Fare\PricePnr13\PricingOptionKey;
50
use Amadeus\Client\Struct\Fare\PricePnr13\TaxData;
51
use Amadeus\Client\Struct\Fare\PricePnr13\TaxInformation;
52
53
/**
54
 * Fare_PricePNRWithBookingClass v 13 and higher structure
55
 *
56
 * @package Amadeus\Client\Struct\Fare
57
 * @author dieter <[email protected]>
58
 */
59
class PricePNRWithBookingClass13 extends BaseWsMessage
60
{
61
    /**
62
     * @var PricePnr13\PricingOptionGroup[]
63
     */
64
    public $pricingOptionGroup = [];
65
66
    /**
67
     * PricePNRWithBookingClass13 constructor.
68
     *
69
     * @param FarePricePnrWithBookingClassOptions|LowerFareOpt|LowestFareOpt $options
70
     * @throws MessageVersionUnsupportedException
71
     */
72
    public function __construct($options)
73
    {
74
        $this->pricingOptionGroup = $this->loadPricingOptionsFromRequestOptions($options);
75
    }
76
77
    /**
78
     * Load an array of PricingOptionGroup objects from the Pricing request options.
79
     *
80
     * Extracted because this method is also used in the InformativePricingWithoutPnr messages.
81
     *
82
     * @param FarePricePnrWithBookingClassOptions|LowerFareOpt|LowestFareOpt $options
83
     * @return PricingOptionGroup[]
84
     */
85
    public static function loadPricingOptionsFromRequestOptions($options)
86
    {
87
        $priceOptions = [];
88
89
        $priceOptions = self::mergeOptions(
90
            $priceOptions,
91
            self::makePricingOptionForValidatingCarrier($options->validatingCarrier)
92
        );
93
94
        $priceOptions = self::mergeOptions(
95
            $priceOptions,
96
            self::makePricingOptionForCurrencyOverride($options->currencyOverride)
97
        );
98
99
        $priceOptions = self::mergeOptions(
100
            $priceOptions,
101
            self::makePricingOptionFareBasisOverride($options->pricingsFareBasis)
102
        );
103
104
        $priceOptions = self::mergeOptions(
105
            $priceOptions,
106
            self::makeOverrideOptions($options->overrideOptions, $priceOptions)
107
        );
108
109
        $priceOptions = self::mergeOptions(
110
            $priceOptions,
111
            self::loadCorpNegoFare($options->corporateNegoFare)
112
        );
113
114
        $priceOptions = self::mergeOptions(
115
            $priceOptions,
116
            self::loadCorpUniFares($options->corporateUniFares, $options->awardPricing)
117
        );
118
119
        $priceOptions = self::mergeOptions(
120
            $priceOptions,
121
            self::loadObFees($options->obFees, $options->obFeeRefs)
122
        );
123
124
        $priceOptions = self::mergeOptions(
125
            $priceOptions,
126
            self::loadPaxDiscount($options->paxDiscountCodes, $options->paxDiscountCodeRefs)
127
        );
128
129
        $priceOptions = self::mergeOptions(
130
            $priceOptions,
131
            self::loadPointOverrides(
132
                $options->pointOfSaleOverride,
133
                $options->pointOfTicketingOverride
134
            )
135
        );
136
137
        $priceOptions = self::mergeOptions(
138
            $priceOptions,
139
            self::loadPricingLogic($options->pricingLogic)
140
        );
141
142
        $priceOptions = self::mergeOptions(
143
            $priceOptions,
144
            self::loadTicketType($options->ticketType)
145
        );
146
147
        $priceOptions = self::mergeOptions(
148
            $priceOptions,
149
            self::loadTaxes($options->taxes)
150
        );
151
152
        $priceOptions = self::mergeOptions(
153
            $priceOptions,
154
            self::loadExemptTaxes($options->exemptTaxes)
155
        );
156
157
        $priceOptions = self::mergeOptions(
158
            $priceOptions,
159
            self::loadPastDate($options->pastDatePricing)
160
        );
161
162
        $priceOptions = self::mergeOptions(
163
            $priceOptions,
164
            self::loadReferences($options->references)
165
        );
166
167
        // All options processed, no options found:
168
        if (empty($priceOptions)) {
169
            $priceOptions[] = new PricingOptionGroup(PricingOptionKey::OPTION_NO_OPTION);
170
        }
171
172
        return $priceOptions;
173
    }
174
175
    /**
176
     * @param string[] $overrideOptions
177
     * @param PricingOptionGroup[] $priceOptions
178
     * @return PricingOptionGroup[]
179
     */
180
    protected static function makeOverrideOptions($overrideOptions, $priceOptions)
181
    {
182
        $opt = [];
183
184
        if (!empty($overrideOptions)) {
185
            foreach ($overrideOptions as $overrideOption) {
186
                if (!self::hasPricingGroup($overrideOption, $priceOptions)) {
187
                    $opt[] = new PricingOptionGroup($overrideOption);
188
                }
189
            }
190
        }
191
192
        return $opt;
193
    }
194
195
    /**
196
     * @param string|null $validatingCarrier
197
     * @return PricePnr13\PricingOptionGroup[]
198
     */
199
    protected static function makePricingOptionForValidatingCarrier($validatingCarrier)
200
    {
201
        $opt = [];
202
203
        if ($validatingCarrier !== null) {
204
            $po = new PricingOptionGroup(PricingOptionKey::OPTION_VALIDATING_CARRIER);
205
206
            $po->carrierInformation = new CarrierInformation($validatingCarrier);
207
208
            $opt[] = $po;
209
        }
210
211
        return $opt;
212
    }
213
214
    /**
215
     * @param string|null $currency
216
     * @return PricePnr13\PricingOptionGroup[]
217
     */
218
    protected static function makePricingOptionForCurrencyOverride($currency)
219
    {
220
        $opt = [];
221
222
        if ($currency !== null) {
223
            $po = new PricingOptionGroup(PricingOptionKey::OPTION_FARE_CURRENCY_OVERRIDE);
224
225
            $po->currency = new Currency($currency, FirstCurrencyDetails::QUAL_CURRENCY_OVERRIDE);
226
227
            $opt[] = $po;
228
        }
229
230
        return $opt;
231
    }
232
233
    /**
234
     * @param FareBasis[] $pricingsFareBasis
235
     * @return PricePnr13\PricingOptionGroup[]
236
     */
237
    protected static function makePricingOptionFareBasisOverride($pricingsFareBasis)
238
    {
239
        $opt = [];
240
241
        if ($pricingsFareBasis !== null) {
242
            foreach ($pricingsFareBasis as $pricingFareBasis) {
243
                $po = new PricingOptionGroup(PricingOptionKey::OPTION_FARE_BASIS_SIMPLE_OVERRIDE);
244
245
                //Support for legacy fareBasisPrimaryCode to be removed when breaking BC:
246
                $po->optionDetail = new OptionDetail(
247
                    $pricingFareBasis->fareBasisPrimaryCode . $pricingFareBasis->fareBasisCode
0 ignored issues
show
Deprecated Code introduced by
The property Amadeus\Client\RequestOp...::$fareBasisPrimaryCode has been deprecated with message: put the full fare basis in $this->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...
248
                );
249
250
                //Support for legacy segmentReference to be removed when breaking BC:
251
                $po->paxSegTstReference = new PaxSegTstReference(
252
                    $pricingFareBasis->segmentReference,
0 ignored issues
show
Deprecated Code introduced by
The property Amadeus\Client\RequestOp...asis::$segmentReference has been deprecated with message: use $this->references 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...
253
                    $pricingFareBasis->references
254
                );
255
256
                $opt[] = $po;
257
            }
258
        }
259
260
        return $opt;
261
    }
262
263
    /**
264
     * Load corporate negofare
265
     *
266
     * @param string|null $corporateNegoFare
267
     * @return PricingOptionGroup[]
268
     */
269 View Code Duplication
    protected static function loadCorpNegoFare($corporateNegoFare)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
270
    {
271
        $opt = [];
272
273
        if ($corporateNegoFare !== null) {
274
            $po = new PricingOptionGroup(PricingOptionKey::OPTION_CORPORATE_NEGOTIATED_FARES);
275
276
            $po->optionDetail = new OptionDetail($corporateNegoFare);
277
278
            $opt[] = $po;
279
        }
280
281
        return $opt;
282
    }
283
284
    /**
285
     * Load corporate unifares
286
     *
287
     * @param string[] $corporateUniFares
288
     * @param AwardPricing|null $awardPricing
289
     * @return PricingOptionGroup[]
290
     */
291
    protected static function loadCorpUniFares($corporateUniFares, $awardPricing)
292
    {
293
        $opt = [];
294
295
        if (!empty($corporateUniFares)) {
296
            $po = new PricingOptionGroup(PricingOptionKey::OPTION_CORPORATE_UNIFARES);
297
            $po->optionDetail = new OptionDetail($corporateUniFares);
298
            $opt[] = $po;
299
300
            if (!empty($awardPricing)) {
301
                $opt[] = self::loadAwardPricing($awardPricing);
302
            }
303
        }
304
305
        return $opt;
306
    }
307
308
    /**
309
     * @param AwardPricing $awardPricing
310
     * @return PricingOptionGroup
311
     */
312
    protected static function loadAwardPricing($awardPricing)
313
    {
314
        $po = new PricingOptionGroup(PricingOptionKey::OPTION_AWARD_PRICING);
315
316
        $po->carrierInformation = new CarrierInformation($awardPricing->carrier);
317
318
        $po->frequentFlyerInformation = new FrequentFlyerInformation();
319
        $po->frequentFlyerInformation->frequentTravellerDetails[] = new FrequentTravellerDetails(
320
            $awardPricing->tierLevel
321
        );
322
323
        return $po;
324
    }
325
326
    /**
327
     * Load OB Fees
328
     *
329
     * @param ObFee[] $obFees
330
     * @param PaxSegRef[] $obFeeRefs
331
     * @return PricingOptionGroup[]
332
     */
333
    protected static function loadObFees($obFees, $obFeeRefs)
334
    {
335
        $opt = [];
336
337
        if (!empty($obFees)) {
338
            $po = new PricingOptionGroup(PricingOptionKey::OPTION_OB_FEES);
339
340
            $po->penDisInformation = new PenDisInformation(PenDisInformation::QUAL_OB_FEES);
341
342
            foreach ($obFees as $obFee) {
343
                $amountType = (!empty($obFee->amount)) ?
344
                    DiscountPenaltyDetails::AMOUNTTYPE_FIXED_WHOLE_AMOUNT :
345
                    DiscountPenaltyDetails::AMOUNTTYPE_PERCENTAGE;
346
347
                $rate = (!empty($obFee->amount)) ? $obFee->amount : $obFee->percentage;
348
349
                $po->penDisInformation->discountPenaltyDetails[] = new DiscountPenaltyDetails(
350
                    $obFee->rate,
351
                    self::makeObFeeFunction($obFee->include),
352
                    $amountType,
353
                    $rate,
354
                    $obFee->currency
355
                );
356
            }
357
358
            if (!empty($obFeeRefs)) {
359
                $po->paxSegTstReference = new PaxSegTstReference(null, $obFeeRefs);
360
            }
361
362
            $opt[] = $po;
363
        }
364
365
        return $opt;
366
    }
367
368
    /**
369
     * @param string[] $paxDiscount
370
     * @param PaxSegRef[] $paxDiscountCodeRefs
371
     * @return PricingOptionGroup[]
372
     */
373
    protected static function loadPaxDiscount($paxDiscount, $paxDiscountCodeRefs)
374
    {
375
        $opt = [];
376
377
        if (!empty($paxDiscount)) {
378
            $po = new PricingOptionGroup(PricingOptionKey::OPTION_PASSENGER_DISCOUNT_PTC);
379
380
            $po->penDisInformation = new PenDisInformation(PenDisInformation::QUAL_DISCOUNT);
381
382
            foreach ($paxDiscount as $discount) {
383
                $po->penDisInformation->discountPenaltyDetails[] = new DiscountPenaltyDetails($discount);
384
            }
385
386
            if (!empty($paxDiscountCodeRefs)) {
387
                $po->paxSegTstReference = new PaxSegTstReference(null, $paxDiscountCodeRefs);
388
            }
389
390
            $opt[] = $po;
391
        }
392
393
        return $opt;
394
    }
395
396
    /**
397
     * @param string|null $posOverride
398
     * @param string|null $potOverride
399
     * @return PricingOptionGroup[]
400
     */
401
    protected static function loadPointOverrides($posOverride, $potOverride)
402
    {
403
        $opt = [];
404
405 View Code Duplication
        if (!empty($posOverride)) {
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...
406
            $po = new PricingOptionGroup(PricingOptionKey::OPTION_POINT_OF_SALE_OVERRIDE);
407
408
            $po->locationInformation = new LocationInformation(
409
                LocationInformation::TYPE_POINT_OF_SALE,
410
                $posOverride
411
            );
412
413
            $opt[] = $po;
414
        }
415
416 View Code Duplication
        if (!empty($potOverride)) {
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...
417
            $po2 = new PricingOptionGroup(PricingOptionKey::OPTION_POINT_OF_TICKETING_OVERRIDE);
418
419
            $po2->locationInformation = new LocationInformation(
420
                LocationInformation::TYPE_POINT_OF_TICKETING,
421
                $potOverride
422
            );
423
424
            $opt[] = $po2;
425
        }
426
427
        return $opt;
428
    }
429
430
    /**
431
     * @param string|null $pricingLogic
432
     * @return PricingOptionGroup[]
433
     */
434 View Code Duplication
    protected static function loadPricingLogic($pricingLogic)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
435
    {
436
        $opt = [];
437
438
        if (!empty($pricingLogic)) {
439
            $po = new PricingOptionGroup(PricingOptionKey::OPTION_PRICING_LOGIC);
440
            $po->optionDetail = new OptionDetail($pricingLogic);
441
            $opt[] = $po;
442
        }
443
444
        return $opt;
445
    }
446
447
    /**
448
     * @param string|null $ticketType
449
     * @return PricingOptionGroup[]
450
     */
451 View Code Duplication
    protected static function loadTicketType($ticketType)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
452
    {
453
        $opt = [];
454
455
        if (!empty($ticketType)) {
456
            $po = new PricingOptionGroup(PricingOptionKey::OPTION_TICKET_TYPE);
457
458
            $po->optionDetail = new OptionDetail($ticketType);
459
460
            $opt[] = $po;
461
        }
462
463
        return $opt;
464
    }
465
466
    /**
467
     * @param Tax[] $taxes
468
     * @return PricingOptionGroup[]
469
     */
470
    protected static function loadTaxes($taxes)
471
    {
472
        $opt = [];
473
474
        if (!empty($taxes)) {
475
            $po = new PricingOptionGroup(PricingOptionKey::OPTION_ADD_TAX);
476
477
            foreach ($taxes as $tax) {
478
                $qualifier = (!empty($tax->amount)) ? TaxData::QUALIFIER_AMOUNT : TaxData::QUALIFIER_PERCENTAGE;
479
                $rate = (!empty($tax->amount)) ? $tax->amount : $tax->percentage;
480
481
                $po->taxInformation[] = new TaxInformation(
482
                    $tax->countryCode,
483
                    $tax->taxNature,
484
                    $qualifier,
485
                    $rate
486
                );
487
            }
488
            $opt[] = $po;
489
        }
490
491
        return $opt;
492
    }
493
494
    /**
495
     * @param ExemptTax[] $exemptTaxes
496
     * @return PricingOptionGroup[]
497
     */
498
    protected static function loadExemptTaxes($exemptTaxes)
499
    {
500
        $opt = [];
501
502
        if (!empty($exemptTaxes)) {
503
            $po = new PricingOptionGroup(PricingOptionKey::OPTION_EXEMPT_FROM_TAX);
504
505
            foreach ($exemptTaxes as $tax) {
506
                $po->taxInformation[] = new TaxInformation(
507
                    $tax->countryCode,
508
                    $tax->taxNature
509
                );
510
            }
511
512
            $opt[] = $po;
513
        }
514
515
        return $opt;
516
    }
517
518
    /**
519
     * @param \DateTime|null $pastDate
520
     * @return PricingOptionGroup[]
521
     */
522
    protected static function loadPastDate($pastDate)
523
    {
524
        $opt = [];
525
526
        if ($pastDate instanceof \DateTime) {
527
            $po = new PricingOptionGroup(PricingOptionKey::OPTION_PAST_DATE_PRICING);
528
529
            $po->dateInformation = new DateInformation(
530
                DateInformation::OPT_DATE_OVERRIDE,
531
                $pastDate
532
            );
533
534
            $opt[] = $po;
535
        }
536
537
        return $opt;
538
    }
539
540
    /**
541
     * @param PaxSegRef[] $references
542
     * @return PricingOptionGroup[]
543
     */
544
    protected static function loadReferences($references)
545
    {
546
        $opt = [];
547
548 View Code Duplication
        if (!empty($references)) {
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...
549
            $po = new PricingOptionGroup(PricingOptionKey::OPTION_PAX_SEGMENT_TST_SELECTION);
550
551
            $po->paxSegTstReference = new PaxSegTstReference(null, $references);
552
553
            $opt[] = $po;
554
        }
555
556
        return $opt;
557
    }
558
559
    /**
560
     * Make the correct function code
561
     *
562
     * @param bool $include
563
     * @return string
564
     */
565
    protected static function makeObFeeFunction($include)
566
    {
567
        return ($include === true) ? ObFee::FUNCTION_INCLUDE : ObFee::FUNCTION_EXCLUDE;
568
    }
569
570
571
572
    /**
573
     * Avoid double pricing groups when combining an explicitly provided override option with a specific parameter
574
     * that uses the same override option.
575
     *
576
     * Backwards compatibility with PricePnrWithBookingClass12
577
     *
578
     * @param string $optionKey
579
     * @param PricingOptionGroup[] $priceOptions
580
     * @return bool
581
     */
582
    protected static function hasPricingGroup($optionKey, $priceOptions)
583
    {
584
        $found = false;
585
586
        foreach ($priceOptions as $pog) {
587
            if ($pog->pricingOptionKey->pricingOptionKey === $optionKey) {
588
                $found = true;
589
            }
590
        }
591
592
        return $found;
593
    }
594
595
    /**
596
     * Merges Pricing options
597
     *
598
     * @param PricingOptionGroup[] $existingOptions
599
     * @param PricingOptionGroup[] $newOptions
600
     * @return PricingOptionGroup[] merged array
601
     */
602
    protected static function mergeOptions($existingOptions, $newOptions)
603
    {
604
        if (!empty($newOptions)) {
605
            $existingOptions = array_merge(
606
                $existingOptions,
607
                $newOptions
608
            );
609
        }
610
611
        return $existingOptions;
612
    }
613
}
614