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

IntegratedPricing::loadPricingOptions()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 77
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 62
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 77
ccs 62
cts 62
cp 1
rs 8.9342
c 0
b 0
f 0
cc 2
eloc 44
nc 2
nop 1
crap 2

How to fix   Long Method   

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\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 IntegratedPricing\PricingOption[]
47
     */
48
    public $pricingOption = [];
49
50
    /**
51
     * IntegratedPricing constructor.
52
     *
53
     * @param ServiceIntegratedPricingOptions $options
0 ignored issues
show
Documentation introduced by
Should the type for parameter $options not be ServiceIntegratedPricingOptions|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
54
     */
55 12
    public function __construct($options = null)
56
    {
57 12
        if (!is_null($options)) {
58 12
            $this->pricingOption = $this->loadPricingOptions($options);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->loadPricingOptions($options) of type array<integer,object<Ama...Pricing\PricingOption>> is incompatible with the declared type array<integer,object<Ama...Pricing\PricingOption>> of property $pricingOption.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59 12
        }
60 12
    }
61
62
    /**
63
     * @param ServiceIntegratedPricingOptions $options
64
     * @return PricingOption[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array<\Amadeus\Client\St...ionGroup|PricingOption>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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)
0 ignored issues
show
Documentation introduced by
$priceOptions is of type array<integer,object<Ama...Pricing\PricingOption>>, but the function expects a array<integer,object<Ama...Pricing\PricingOption>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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