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

PricePNRWithBookingClass12::__construct()   F

Complexity

Conditions 23
Paths > 20000

Size

Total Lines 95
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 95
c 2
b 0
f 0
rs 2
cc 23
eloc 49
nc 74976
nop 1

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\Fare;
24
25
use Amadeus\Client\RequestOptions\Fare\PricePnr\AwardPricing;
26
use Amadeus\Client\RequestOptions\Fare\PricePnr\ExemptTax;
27
use Amadeus\Client\RequestOptions\Fare\PricePnr\PaxSegRef;
28
use Amadeus\Client\RequestOptions\Fare\PricePnr\Tax;
29
use Amadeus\Client\RequestOptions\FarePricePnrWithBookingClassOptions;
30
use Amadeus\Client\Struct\BaseWsMessage;
31
use Amadeus\Client\Struct\Fare\PricePnr12\AttributeDetails;
32
use Amadeus\Client\Struct\Fare\PricePnr12\CarrierAgreements;
33
use Amadeus\Client\Struct\Fare\PricePnr12\CityDetail;
34
use Amadeus\Client\Struct\Fare\PricePnr12\CurrencyOverride;
35
use Amadeus\Client\Struct\Fare\PricePnr12\DateOverride;
36
use Amadeus\Client\Struct\Fare\PricePnr12\DiscountInformation;
37
use Amadeus\Client\Struct\Fare\PricePnr12\FrequentFlyerInformation;
38
use Amadeus\Client\Struct\Fare\PricePnr12\OverrideInformation;
39
use Amadeus\Client\Struct\Fare\PricePnr12\PenDisData;
40
use Amadeus\Client\Struct\Fare\PricePnr12\PenDisInformation;
41
use Amadeus\Client\Struct\Fare\PricePnr12\PricingFareBase;
42
use Amadeus\Client\Struct\Fare\PricePnr12\RefDetails;
43
use Amadeus\Client\Struct\Fare\PricePnr12\ReferenceQualifier;
44
use Amadeus\Client\Struct\Fare\PricePnr12\TaxDetails;
45
use Amadeus\Client\Struct\Fare\PricePnr12\ValidatingCarrier;
46
use Amadeus\Client\Struct\OptionNotSupportedException;
47
48
/**
49
 * Fare_PricePNRWithBookingClass v 12 and lower structure
50
 *
51
 * @package Amadeus\Client\Struct\Fare
52
 * @author dieter <[email protected]>
53
 */
54
class PricePNRWithBookingClass12 extends BaseWsMessage
55
{
56
    public $pnrLocatorData;
57
58
    /**
59
     * @var PricePnr12\PaxSegReference
60
     */
61
    public $paxSegReference;
62
63
    /**
64
     * @var PricePnr12\OverrideInformation
65
     */
66
    public $overrideInformation;
67
68
    /**
69
     * @var PricePnr12\DateOverride
70
     */
71
    public $dateOverride;
72
73
    /**
74
     * @var PricePnr12\ValidatingCarrier
75
     */
76
    public $validatingCarrier;
77
78
    /**
79
     * @var PricePnr12\CityOverride
80
     */
81
    public $cityOverride;
82
83
    /**
84
     * @var PricePnr12\CurrencyOverride
85
     */
86
    public $currencyOverride;
87
88
    /**
89
     * @var PricePnr12\TaxDetails[]
90
     */
91
    public $taxDetails = [];
92
93
    /**
94
     * @var PricePnr12\DiscountInformation[]
95
     */
96
    public $discountInformation = [];
97
    /**
98
     * @var PricePnr12\PricingFareBase[]
99
     */
100
    public $pricingFareBase = [];
101
102
    public $flightInformation;
103
104
    public $openSegmentsInformation;
105
106
    public $bookingClassSelection;
107
108
    public $fopInformation;
109
110
    /**
111
     * @var CarrierAgreements
112
     */
113
    public $carrierAgreements;
114
115
    /**
116
     * @var FrequentFlyerInformation
117
     */
118
    public $frequentFlyerInformation;
119
120
    public $instantPricingOption;
121
122
    /**
123
     * PricePNRWithBookingClass12 constructor.
124
     *
125
     * @param FarePricePnrWithBookingClassOptions $options
126
     */
127
    public function __construct(FarePricePnrWithBookingClassOptions $options)
128
    {
129
        $this->overrideInformation = new OverrideInformation();
130
131
        if (count($options->overrideOptions) !== 0) {
132
            foreach ($options->overrideOptions as $overrideOption) {
133
                $this->overrideInformation->attributeDetails[] = new AttributeDetails($overrideOption);
134
            }
135
        }
136
137
        if (!empty($options->currencyOverride)) {
138
            $this->currencyOverride = new CurrencyOverride($options->currencyOverride);
139
        }
140
141
        if (is_string($options->validatingCarrier)) {
142
            $this->validatingCarrier = new ValidatingCarrier($options->validatingCarrier);
143
        }
144
145
        $short = AttributeDetails::OVERRIDE_FAREBASIS; //Short var name because I get complaints from phpcs. No judging.
146
        if (in_array($short, $options->overrideOptions) && !empty($options->pricingsFareBasis)) {
147
            foreach ($options->pricingsFareBasis as $pricingFareBasis) {
148
                $this->pricingFareBase[] = new PricingFareBase($pricingFareBasis);
149
            }
150
        }
151
152
        if ($options->corporateNegoFare !== null) {
153
            $this->loadCorporateNegoFare($options->corporateNegoFare);
154
        }
155
156
        if (!empty($options->corporateUniFares)) {
157
            $this->loadCorporateUniFares($options->corporateUniFares);
158
159
            if (!empty($options->awardPricing)) {
160
                $this->loadAwardPricing($options->awardPricing);
161
            }
162
        }
163
164
        if (!empty($options->obFees)) {
165
            throw new OptionNotSupportedException('OB Fees option not supported in version 12 or lower');
166
        }
167
168
        if (!empty($options->paxDiscountCodes)) {
169
            $this->loadPaxDiscount($options->paxDiscountCodes, $options->paxDiscountCodeRefs);
170
        }
171
172
        if (!empty($options->pointOfSaleOverride)) {
173
            $this->cityOverride = new PricePnr12\CityOverride(
174
                $options->pointOfSaleOverride,
175
                CityDetail::QUAL_POINT_OF_SALE
176
            );
177
        }
178
179
        if (!empty($options->pointOfTicketingOverride)) {
180
            if (empty($this->cityOverride)) {
181
                $this->cityOverride = new PricePnr12\CityOverride();
182
            }
183
            $this->cityOverride->cityDetail[] = new PricePnr12\CityDetail(
184
                $options->pointOfTicketingOverride,
185
                CityDetail::QUAL_POINT_OF_TICKETING
186
            );
187
        }
188
189
        if (!empty($options->pricingLogic)) {
190
            throw new OptionNotSupportedException('Pricing Logic option not supported in version 12 or lower');
191
        }
192
193
        if (!empty($options->ticketType)) {
194
            $this->overrideInformation->attributeDetails[] = new AttributeDetails(
195
                $this->convertTicketType($options->ticketType)
196
            );
197
        }
198
199
        if (!empty($options->taxes)) {
200
            $this->loadTaxes($options->taxes);
201
        }
202
203
        if (!empty($options->exemptTaxes)) {
204
            $this->loadExemptTaxes($options->exemptTaxes);
205
        }
206
207
        if (!empty($options->references)) {
208
            $this->loadReferences($options->references);
209
        }
210
211
        if ($options->pastDatePricing instanceof \DateTime) {
212
            $this->loadPastDate($options->pastDatePricing);
213
        }
214
215
        //No Options?
216
        if (empty($this->overrideInformation->attributeDetails)) {
217
            $this->overrideInformation->attributeDetails[] = new AttributeDetails(
218
                AttributeDetails::OVERRIDE_NO_OPTION
219
            );
220
        }
221
    }
222
223
    /**
224
     * @param string $corporateNegoFare
225
     */
226
    protected function loadCorporateNegoFare($corporateNegoFare)
227
    {
228
        $this->overrideInformation->attributeDetails[] = new AttributeDetails(
229
            AttributeDetails::OVERRIDE_FARETYPE_CORPNR,
230
            $corporateNegoFare
231
        );
232
    }
233
234
    /**
235
     * @param string[] $corporateUniFares
236
     */
237
    protected function loadCorporateUniFares($corporateUniFares)
238
    {
239
        foreach ($corporateUniFares as $corporateUniFare) {
240
            $this->overrideInformation->attributeDetails[] = new AttributeDetails(
241
                AttributeDetails::OVERRIDE_FARETYPE_CORPUNI,
242
                $corporateUniFare
243
            );
244
        }
245
    }
246
247
    /**
248
     * @param AwardPricing $awardPricing
249
     */
250
    protected function loadAwardPricing($awardPricing)
251
    {
252
        $this->carrierAgreements = new CarrierAgreements($awardPricing->carrier);
253
        $this->frequentFlyerInformation = new FrequentFlyerInformation($awardPricing->tierLevel);
254
    }
255
256
    /**
257
     * @param string[] $paxDiscountCodes
258
     * @param PaxSegRef[] $refs
259
     */
260
    protected function loadPaxDiscount($paxDiscountCodes, $refs)
261
    {
262
        $tmp = new DiscountInformation();
263
        $tmp->penDisInformation = new PenDisInformation();
264
        $tmp->penDisInformation->infoQualifier = PenDisInformation::QUAL_DISCOUNT;
265
266
        foreach ($paxDiscountCodes as $discountCode) {
267
            $tmp->penDisInformation->penDisData[] = new PenDisData($discountCode);
268
        }
269
270
        if (!empty($refs)) {
271
            $tmp->referenceQualifier = new ReferenceQualifier();
272
273
            foreach ($refs as $ref) {
274
                $tmp->referenceQualifier->refDetails[] = new RefDetails($ref->reference, $ref->type);
275
            }
276
        }
277
278
        $this->discountInformation[] = $tmp;
279
    }
280
281
    /**
282
     * Convert new codes (Price PNR version 13+) to old format
283
     *
284
     * @param string $optionsTicketType
285
     * @return string|null
286
     */
287
    protected function convertTicketType($optionsTicketType)
288
    {
289
        $converted = null;
290
291
        $map = [
292
            FarePricePnrWithBookingClassOptions::TICKET_TYPE_ELECTRONIC => AttributeDetails::OVERRIDE_ELECTRONIC_TICKET,
293
            FarePricePnrWithBookingClassOptions::TICKET_TYPE_PAPER => AttributeDetails::OVERRIDE_PAPER_TICKET,
294
            FarePricePnrWithBookingClassOptions::TICKET_TYPE_BOTH => AttributeDetails::OVERRIDE_BOTH_TICKET,
295
        ];
296
297
        if (array_key_exists($optionsTicketType, $map)) {
298
            $converted = $map[$optionsTicketType];
299
        }
300
301
        return $converted;
302
    }
303
304
    /**
305
     * @param Tax[] $taxes
306
     */
307
    protected function loadTaxes($taxes)
308
    {
309
        foreach ($taxes as $tax) {
310
            $this->taxDetails[] = new TaxDetails($tax);
311
        }
312
    }
313
314
    /**
315
     * @param ExemptTax[] $exemptTaxes
316
     */
317
    protected function loadExemptTaxes($exemptTaxes)
318
    {
319
        foreach ($exemptTaxes as $exemptTax) {
320
            $this->taxDetails[] = new TaxDetails($exemptTax);
321
        }
322
    }
323
324
    /**
325
     * @param PaxSegRef[] $references
326
     */
327
    protected function loadReferences($references)
328
    {
329
        $this->paxSegReference = new PricePnr12\PaxSegReference();
330
331
        foreach ($references as $ref) {
332
            $this->paxSegReference->refDetails[] = new RefDetails(
333
                $ref->reference,
334
                $ref->type
335
            );
336
        }
337
    }
338
339
    /**
340
     * @param \DateTime $pastDate
341
     */
342
    protected function loadPastDate(\DateTime $pastDate)
343
    {
344
        $this->dateOverride = new DateOverride(DateOverride::OPT_DATE_OVERRIDE, $pastDate);
345
    }
346
}
347