Completed
Push — master ( 4b1795...8ce79e )
by Dieter
09:49
created

IntegratedPricing::loadDateOverride()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 2

Importance

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