Completed
Push — develop ( a79e07...abf317 )
by Dieter
04:48
created

ExtremeSearch   B

Complexity

Total Complexity 31

Size/Duplication

Total Lines 247
Duplicated Lines 4.45 %

Coupling/Cohesion

Components 5
Dependencies 17

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 31
c 2
b 0
f 1
lcom 5
cbo 17
dl 11
loc 247
rs 7.7

7 Methods

Rating   Name   Duplication   Size   Complexity  
D __construct() 0 40 9
A loadBudget() 0 10 4
A loadDestinationCountries() 0 14 2
A loadStayDuration() 0 10 3
B loadCheapestQualifiers() 0 30 5
A loadResultAggregation() 0 13 2
B makeAggregationGroupTypes() 11 30 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\PriceXplorer;
24
25
use Amadeus\Client\RequestOptions\PriceXplorerExtremeSearchOptions;
26
use Amadeus\Client\Struct\BaseWsMessage;
27
28
/**
29
 * ExtremeSearch
30
 *
31
 * @package Amadeus\Client\Struct\PriceXplorer
32
 * @author Dieter Devlieghere <[email protected]>
33
 */
34
class ExtremeSearch extends BaseWsMessage
35
{
36
    /**
37
     * Itinerary information group
38
     *
39
     * @var ItineraryGrp[]
40
     */
41
    public $itineraryGrp = [];
42
43
    /**
44
     * Budget info
45
     *
46
     * @var Budget
47
     */
48
    public $budget;
49
50
    /**
51
     * Departure dates ranges
52
     *
53
     * @var TravelDates
54
     */
55
    public $travelDates;
56
57
    /**
58
     * Stay duration and flexibility
59
     *
60
     * @var StayDuration
61
     */
62
    public $stayDuration;
63
64
    /**
65
     * Attribute Information
66
     *
67
     * @var AttributeInfo[]
68
     */
69
    public $attributeInfo = [];
70
71
    /**
72
     * Option description : Price result distribution, ...
73
     *
74
     * @var SelectionDetailsGroup[]
75
     */
76
    public $selectionDetailsGroup = [];
77
78
    /**
79
     * List of departure days
80
     *
81
     * @var DepartureDays[]
82
     */
83
    public $departureDays = [];
84
85
    /**
86
     * Airline information
87
     *
88
     * @var AirlineInfo[]
89
     */
90
    public $airlineInfo = [];
91
92
    /**
93
     * List of Office Id Details
94
     *
95
     * @var OfficeIdInfo[]
96
     */
97
    public $officeIdInfo = [];
98
99
    /**
100
     * Construct PriceXplorer_ExtremeSearch Request message
101
     *
102
     * @param PriceXplorerExtremeSearchOptions $params
103
     */
104
    public function __construct(PriceXplorerExtremeSearchOptions $params)
105
    {
106
        $this->itineraryGrp[] = new ItineraryGrp($params->origin);
107
108
        if ($params->earliestDepartureDate instanceof \DateTime || $params->latestDepartureDate instanceof \DateTime) {
109
            $this->travelDates = new TravelDates($params->earliestDepartureDate, $params->latestDepartureDate);
110
        }
111
112
        if ($params->searchOffice !== null) {
113
            $this->officeIdInfo[] = new OfficeIdInfo($params->searchOffice);
114
        }
115
116
        $this->loadBudget($params->maxBudget, $params->minBudget, $params->currency);
117
118
        if (!empty($params->destinations)) {
119
            foreach ($params->destinations as $destination) {
120
                $this->itineraryGrp[] = new ItineraryGrp(null, $destination);
121
            }
122
        }
123
124
        if (!empty($params->destinationCountries)) {
125
            $this->loadDestinationCountries($params->destinationCountries);
126
        }
127
128
        if (!empty($params->departureDaysInbound)) {
129
            $this->departureDays[] = new DepartureDays($params->departureDaysInbound, SelectionDetails::OPT_INBOUND_DEP_DAYS);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
130
        }
131
        if (!empty($params->departureDaysOutbound)) {
132
            $this->departureDays[] = new DepartureDays(
133
                $params->departureDaysOutbound,
134
                SelectionDetails::OPT_OUTBOUND_DEP_DAYS
135
            );
136
        }
137
138
        $this->loadStayDuration($params->stayDurationDays, $params->stayDurationFlexibilityDays);
139
140
        $this->loadCheapestQualifiers($params->returnCheapestNonStop, $params->returnCheapestOverall);
141
142
        $this->loadResultAggregation($params->resultAggregationOption);
143
    }
144
145
    /**
146
     * @param int|null $maxBudget
147
     * @param int|null $minBudget
148
     * @param string|null $currency
149
     */
150
    protected function loadBudget($maxBudget, $minBudget, $currency)
151
    {
152
        if (($maxBudget !== null || $minBudget !== null) && $currency !== null) {
153
            $this->budget = new Budget(
154
                $maxBudget,
155
                $minBudget,
156
                $currency
157
            );
158
        }
159
    }
160
161
    /**
162
     * @param array $destinationCountries
163
     */
164
    protected function loadDestinationCountries($destinationCountries)
165
    {
166
        foreach ($destinationCountries as $destinationCountry) {
167
            $tmpGrp = new ItineraryGrp();
168
169
            $tmpGrp->locationInfo = new LocationInfo(LocationInfo::LOC_COUNTRY);
170
171
            $tmpGrp->locationInfo->locationDescription = new LocationIdentificationType();
172
            $tmpGrp->locationInfo->locationDescription->qualifier = LocationIdentificationType::QUAL_DESTINATION;
173
            $tmpGrp->locationInfo->locationDescription->code = $destinationCountry;
174
175
            $this->itineraryGrp[] = $tmpGrp;
176
        }
177
    }
178
179
    /**
180
     * @param int|null $stayDuration
181
     * @param int|null $flexibility
182
     */
183
    protected function loadStayDuration($stayDuration, $flexibility)
184
    {
185
        if ($stayDuration !== null) {
186
            $this->stayDuration = new StayDuration($stayDuration);
187
188
            if ($flexibility !== null) {
189
                $this->stayDuration->flexibilityInfo = new FlexibilityInfo($flexibility);
190
            }
191
        }
192
    }
193
194
    /**
195
     * @param bool $cheapestNonStop
196
     * @param bool $cheapestOverall
197
     */
198
    protected function loadCheapestQualifiers($cheapestNonStop, $cheapestOverall)
199
    {
200
        if ($cheapestNonStop || $cheapestOverall) {
201
202
            $tmpSelDet = new SelectionDetailsGroup();
203
204
            $tmpSelDet->selectionDetailsInfo = new SelectionDetailsInfo();
205
            $tmpSelDet->selectionDetailsInfo->selectionDetails[] = new SelectionDetails(
206
                SelectionDetails::OPT_PRICE_RESULT_DISTRIBUTION
207
            );
208
209
            $tmpSelDet->nbOfUnitsInfo = new NbOfUnitsInfo();
210
211
            if ($cheapestNonStop) {
212
                $tmpSelDet->nbOfUnitsInfo->quantityDetails[] = new NumberOfUnitDetailsType(
213
                    null,
214
                    NumberOfUnitDetailsType::QUAL_CHEAPEST_NONSTOP
215
                );
216
            }
217
218
            if ($cheapestOverall) {
219
                $tmpSelDet->nbOfUnitsInfo->quantityDetails[] = new NumberOfUnitDetailsType(
220
                    null,
221
                    NumberOfUnitDetailsType::QUAL_CHEAPEST_OVERALL
222
                );
223
            }
224
225
            $this->selectionDetailsGroup[] = $tmpSelDet;
226
        }
227
    }
228
229
    /**
230
     * @param string $resultAggregationOption
231
     */
232
    protected function loadResultAggregation($resultAggregationOption)
233
    {
234
        if ($resultAggregationOption !== null) {
235
            $groupTypes = $this->makeAggregationGroupTypes($resultAggregationOption);
236
237
            $tmpAttrInfo = new AttributeInfo(
238
                AttributeInfo::FUNC_GROUPING,
239
                $groupTypes
240
            );
241
242
            $this->attributeInfo[] = $tmpAttrInfo;
243
        }
244
    }
245
246
    /**
247
     * @param string $groupTypeString
248
     * @return array
249
     */
250
    protected function makeAggregationGroupTypes($groupTypeString)
251
    {
252
        $result = [];
253
254
        switch($groupTypeString) {
255
            case PriceXplorerExtremeSearchOptions::AGGR_DEST:
256
                $result[] = AttributeDetails::TYPE_DESTINATION;
257
                break;
258
            case PriceXplorerExtremeSearchOptions::AGGR_COUNTRY:
259
                $result[] = AttributeDetails::TYPE_COUNTRY;
260
                break;
261
            case PriceXplorerExtremeSearchOptions::AGGR_DEST_WEEK:
262
                $result[] = AttributeDetails::TYPE_DESTINATION;
263
                $result[] = AttributeDetails::TYPE_WEEK;
264
                break;
265 View Code Duplication
            case PriceXplorerExtremeSearchOptions::AGGR_DEST_WEEK_DEPART:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
266
                $result[] = AttributeDetails::TYPE_DESTINATION;
267
                $result[] = AttributeDetails::TYPE_WEEK;
268
                $result[] = AttributeDetails::TYPE_DEPARTURE_DAY;
269
                break;
270 View Code Duplication
            case PriceXplorerExtremeSearchOptions::AGGR_DEST_WEEK_DEPART_STAY:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
271
                $result[] = AttributeDetails::TYPE_DESTINATION;
272
                $result[] = AttributeDetails::TYPE_WEEK;
273
                $result[] = AttributeDetails::TYPE_DEPARTURE_DAY;
274
                $result[] = AttributeDetails::TYPE_STAY_DURATION;
275
                break;
276
        }
277
278
        return $result;
279
    }
280
}
281