Completed
Push — master ( 6421a3...9ea2c2 )
by Dieter
06:07
created

PricePNRWithBookingClass13::loadObFees()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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