Completed
Push — master ( 84b838...db89dd )
by Dieter
08:17
created

IntegratedPricing::loadFrequentFlyerOverride()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 14
loc 14
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 7
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\Service;
24
25
use Amadeus\Client\RequestOptions\Fare\PricePnr\PaxSegRef;
26
use Amadeus\Client\RequestOptions\Service\FormOfPayment;
27
use Amadeus\Client\RequestOptions\Service\FrequentFlyer;
28
use Amadeus\Client\RequestOptions\ServiceIntegratedPricingOptions;
29
use Amadeus\Client\Struct\Fare\BasePricingMessage;
30
use Amadeus\Client\Struct\Fare\PricePnr13\CarrierInformation;
31
use Amadeus\Client\Struct\Fare\PricePnr13\Currency;
32
use Amadeus\Client\Struct\Fare\PricePnr13\DateInformation;
33
use Amadeus\Client\Struct\Fare\PricePnr13\FormOfPaymentInformation;
34
use Amadeus\Client\Struct\Fare\PricePnr13\FrequentFlyerInformation;
35
use Amadeus\Client\Struct\Fare\PricePnr13\LocationInformation;
36
use Amadeus\Client\Struct\Fare\PricePnr13\OptionDetail;
37
use Amadeus\Client\Struct\Fare\PricePnr13\PaxSegTstReference;
38
use Amadeus\Client\Struct\Service\IntegratedPricing\PricingOptionKey;
39
use Amadeus\Client\Struct\Service\IntegratedPricing\PricingOption;
40
41
/**
42
 * Service_IntegratedPricing request structure
43
 *
44
 * @package Amadeus\Client\Struct\Service
45
 * @author Dieter Devlieghere <[email protected]>
46
 */
47
class IntegratedPricing extends BasePricingMessage
48
{
49
    /**
50
     * @var PricingOption[]
51
     */
52
    public $pricingOption = [];
53
54
    /**
55
     * IntegratedPricing constructor.
56
     *
57
     * @param ServiceIntegratedPricingOptions|null $options
58
     */
59 14
    public function __construct($options = null)
60
    {
61 14
        if (!is_null($options)) {
62 14
            $this->pricingOption = $this->loadPricingOptions($options);
63 14
        }
64 14
    }
65
66
    /**
67
     * @param ServiceIntegratedPricingOptions $options
68
     * @return PricingOption[]
69
     */
70 14
    protected function loadPricingOptions(ServiceIntegratedPricingOptions $options)
71
    {
72 14
        $priceOptions = [];
73
74 14
        $priceOptions = self::mergeOptions(
75 14
            $priceOptions,
76 14
            self::makePricingOptionForValidatingCarrier($options->validatingCarrier)
77 14
        );
78
79 14
        $priceOptions = self::mergeOptions(
80 14
            $priceOptions,
81 14
            self::makePricingOptionForCurrencyOverride($options->currencyOverride)
82 14
        );
83
84 14
        $priceOptions = self::mergeOptions(
85 14
            $priceOptions,
86 14
            self::makePricingOptionWithOptionDetailAndRefs(
87 14
                PricingOptionKey::OVERRIDE_ACCOUNT_CODE,
88 14
                $options->accountCode,
89 14
                $options->accountCodeRefs
90 14
            )
91 14
        );
92
93 14
        $priceOptions = self::mergeOptions(
94 14
            $priceOptions,
95 14
            self::makePricingOptionWithOptionDetailAndRefs(
96 14
                PricingOptionKey::OVERRIDE_AWARD,
97 14
                $options->awardPricing,
98 14
                []
99 14
            )
100 14
        );
101
102 14
        $priceOptions = self::mergeOptions(
103 14
            $priceOptions,
104 14
            self::makePricingOptionWithOptionDetailAndRefs(
105 14
                PricingOptionKey::OVERRIDE_CORPORATION_NUMBER,
106 14
                $options->corporationNumber,
107 14
                []
108 14
            )
109 14
        );
110
111 14
        $priceOptions = self::mergeOptions(
112 14
            $priceOptions,
113 14
            self::loadDateOverride($options->overrideDate)
114 14
        );
115
116 14
        $priceOptions = self::mergeOptions(
117 14
            $priceOptions,
118 14
            self::makePricingOptionWithOptionDetailAndRefs(
119 14
                PricingOptionKey::OVERRIDE_TICKET_DESIGNATOR,
120 14
                $options->ticketDesignator,
121 14
                []
122 14
            )
123 14
        );
124
125 14
        $priceOptions = self::mergeOptions(
126 14
            $priceOptions,
127 14
            self::loadPointOverrides($options->pointOfSaleOverride)
128 14
        );
129
130 14
        $priceOptions = self::mergeOptions(
131 14
            $priceOptions,
132 14
            self::loadFormOfPaymentOverride($options->formOfPayment)
133 14
        );
134
135 14
        $priceOptions = self::mergeOptions(
136 14
            $priceOptions,
137 14
            self::loadFrequentFlyerOverride($options->frequentFlyers)
138 14
        );
139
140 14
        $priceOptions = self::mergeOptions(
141 14
            $priceOptions,
142 14
            self::loadReferences($options->references)
143 14
        );
144
145 14
        $priceOptions = self::mergeOptions(
146 14
            $priceOptions,
147 14
            self::makeOverrideOptions($options->overrideOptions, $priceOptions)
148 14
        );
149
150
        // All options processed, no options found:
151 14
        if (empty($priceOptions)) {
152 2
            $priceOptions[] = new PricingOption(PricingOptionKey::OVERRIDE_NO_OPTION);
153 2
        }
154
155 14
        return $priceOptions;
156
    }
157
158
    /**
159
     * @param string|null $validatingCarrier
160
     * @return PricingOption[]
161
     */
162 14 View Code Duplication
    protected static function makePricingOptionForValidatingCarrier($validatingCarrier)
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...
163
    {
164 14
        $opt = [];
165
166 14
        if ($validatingCarrier !== null) {
167 1
            $po = new PricingOption(PricingOptionKey::OVERRIDE_VALIDATING_CARRIER);
168
169 1
            $po->carrierInformation = new CarrierInformation($validatingCarrier);
170
171 1
            $opt[] = $po;
172 1
        }
173
174 14
        return $opt;
175
    }
176
177
    /**
178
     * @param string|null $currency
179
     * @return PricingOption[]
180
     */
181 14 View Code Duplication
    protected static function makePricingOptionForCurrencyOverride($currency)
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...
182
    {
183 14
        $opt = [];
184
185 14
        if ($currency !== null) {
186 1
            $po = new PricingOption(PricingOptionKey::OVERRIDE_CURRENCY);
187
188 1
            $po->currency = new Currency($currency);
189
190 1
            $opt[] = $po;
191 1
        }
192
193 14
        return $opt;
194
    }
195
196
    /**
197
     * @param string $overrideCode
198
     * @param string|array|null $options
199
     * @param PaxSegRef[] $references
200
     * @return PricingOption[]
201
     */
202 14
    protected function makePricingOptionWithOptionDetailAndRefs($overrideCode, $options, $references)
203
    {
204 14
        $opt = [];
205
206 14
        if ($options !== null) {
207 4
            $po = new PricingOption($overrideCode);
208
209 4
            $po->optionDetail = new OptionDetail($options);
210
211 4
            if (!empty($references)) {
212 1
                $po->paxSegTstReference = new PaxSegTstReference($references);
213 1
            }
214
215 4
            $opt[] = $po;
216 4
        }
217
218 14
        return $opt;
219
    }
220
221
    /**
222
     * @param \DateTime|null $dateOverride
223
     * @return PricingOption[]
224
     */
225 14 View Code Duplication
    protected static function loadDateOverride($dateOverride)
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...
226
    {
227 14
        $opt = [];
228
229 14
        if ($dateOverride instanceof \DateTime) {
230 1
            $po = new PricingOption(PricingOptionKey::OVERRIDE_PRICING_DATE);
231
232 1
            $po->dateInformation = new DateInformation(
233 1
                DateInformation::OPT_DATE_OVERRIDE,
234
                $dateOverride
235 1
            );
236
237 1
            $opt[] = $po;
238 1
        }
239
240 14
        return $opt;
241
    }
242
243
    /**
244
     * @param string|null $posOverride
245
     * @return PricingOption[]
246
     */
247 14 View Code Duplication
    protected static function loadPointOverrides($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...
248
    {
249 14
        $opt = [];
250
251 14
        if (!empty($posOverride)) {
252 1
            $po = new PricingOption(PricingOptionKey::OVERRIDE_POINT_OF_SALE);
253
254 1
            $po->locationInformation = new LocationInformation(
255 1
                LocationInformation::TYPE_POINT_OF_SALE,
256
                $posOverride
257 1
            );
258
259 1
            $opt[] = $po;
260 1
        }
261
262 14
        return $opt;
263
    }
264
265
    /**
266
     * @param FormOfPayment[] $formOfPayment
267
     * @return PricingOption[]
268
     */
269 14 View Code Duplication
    protected static function loadFormOfPaymentOverride($formOfPayment)
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 14
        $opt = [];
272
273 14
        if (!empty($formOfPayment)) {
274 1
            $po = new PricingOption(PricingOptionKey::OVERRIDE_FORM_OF_PAYMENT);
275
276 1
            $po->formOfPaymentInformation = new FormOfPaymentInformation($formOfPayment);
277
278 1
            $opt[] = $po;
279 1
        }
280
281 14
        return $opt;
282
    }
283
284
    /**
285
     * @param FrequentFlyer[] $frequentFlyers
286
     * @return PricingOption[]
287
     */
288 14 View Code Duplication
    protected static function loadFrequentFlyerOverride($frequentFlyers)
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...
289
    {
290 14
        $opt = [];
291
292 14
        if (!empty($frequentFlyers)) {
293 1
            $po = new PricingOption(PricingOptionKey::OVERRIDE_FREQUENT_FLYER_INFORMATION);
294
295 1
            $po->frequentFlyerInformation = new FrequentFlyerInformation($frequentFlyers);
296
297 1
            $opt[] = $po;
298 1
        }
299
300 14
        return $opt;
301
    }
302
303
    /**
304
     * @param PaxSegRef[] $references
305
     * @return PricingOption[]
306
     */
307 14 View Code Duplication
    protected static function loadReferences($references)
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...
308
    {
309 14
        $opt = [];
310
311 14
        if (!empty($references)) {
312 1
            $po = new PricingOption(PricingOptionKey::OVERRIDE_PAX_SEG_ELEMENT_SELECTION);
313
314 1
            $po->paxSegTstReference = new PaxSegTstReference($references);
315
316 1
            $opt[] = $po;
317 1
        }
318
319 14
        return $opt;
320
    }
321
322
    /**
323
     * @param string[] $overrideOptions
324
     * @param PricingOption[] $priceOptions
325
     * @return PricingOption[]
326
     */
327 14 View Code Duplication
    protected static function makeOverrideOptions($overrideOptions, $priceOptions)
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...
328
    {
329 14
        $opt = [];
330
331 14
        foreach ($overrideOptions as $overrideOption) {
332 1
            if (!self::hasPricingGroup($overrideOption, $priceOptions)) {
333 1
                $opt[] = new PricingOption($overrideOption);
334 1
            }
335 14
        }
336
337 14
        return $opt;
338
    }
339
340
    /**
341
     * Merges Pricing options
342
     *
343
     * @param PricingOption[] $existingOptions
344
     * @param PricingOption[] $newOptions
345
     * @return PricingOption[] merged array
346
     */
347 14
    protected static function mergeOptions($existingOptions, $newOptions)
348
    {
349 14
        if (!empty($newOptions)) {
350 12
            $existingOptions = array_merge(
351 12
                $existingOptions,
352
                $newOptions
353 12
            );
354 12
        }
355
356 14
        return $existingOptions;
357
    }
358
}
359