EncodeDecodeCity::__construct()   B
last analyzed

Complexity

Conditions 9
Paths 48

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 7.4698
c 0
b 0
f 0
cc 9
nc 48
nop 1

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\Info;
24
25
use Amadeus\Client\RequestCreator\MessageVersionUnsupportedException;
26
use Amadeus\Client\RequestOptions\InfoEncodeDecodeCityOptions;
27
use Amadeus\Client\Struct\BaseWsMessage;
28
29
/**
30
 * Info_EncodeDecodeCity
31
 *
32
 * @package Amadeus\Client\Struct\Info
33
 */
34
class EncodeDecodeCity extends BaseWsMessage
35
{
36
    /**
37
     * @var LocationInformation
38
     */
39
    public $locationInformation;
40
41
    /**
42
     * @var RequestOption
43
     */
44
    public $requestOption;
45
46
    /**
47
     * @var Language
48
     */
49
    public $language;
50
51
    /**
52
     * @var CountryStateRestriction
53
     */
54
    public $countryStateRestriction;
55
56
    /**
57
     * EncodeDecodeCity constructor.
58
     *
59
     * @param InfoEncodeDecodeCityOptions $params
60
     * @throws MessageVersionUnsupportedException
61
     */
62
    public function __construct(InfoEncodeDecodeCityOptions $params)
63
    {
64
        if (!empty($params->locationCode)) {
65
            $this->locationInformation = new LocationInformation(
66
                LocationInformation::TYPE_LOCATION,
67
                strtoupper($params->locationCode),
68
                null
69
            );
70
        } elseif (!empty($params->locationName)) {
71
            //Only allow lowercase input for phonetic searches
72
            if ($params->searchMode !== InfoEncodeDecodeCityOptions::SEARCHMODE_PHONETIC) {
73
                $params->locationName = strtoupper($params->locationName);
74
            }
75
76
            $this->locationInformation = new LocationInformation(
77
                LocationInformation::TYPE_ALL,
78
                null,
79
                $params->locationName
80
            );
81
        }
82
83
        if (!empty($params->searchMode)) {
84
            $this->requestOption = new RequestOption(
85
                $params->searchMode,
86
                SelectionDetails::OPT_SEARCH_ALGORITHM
87
            );
88
        }
89
90
        if (!empty($params->selectResult)) {
91
            $selDet = new SelectionDetails(
92
                $params->selectResult,
93
                SelectionDetails::OPT_LOCATION_TYPE
94
            );
95
96
            if (!($this->requestOption instanceof RequestOption)) {
97
                $this->requestOption = new RequestOption();
98
                $this->requestOption->selectionDetails = $selDet;
99
            } else {
100
                $this->requestOption->otherSelectionDetails[] = $selDet;
101
            }
102
        }
103
104
        if (!is_null($params->restrictCountry) || !is_null($params->restrictState)) {
105
            $params->restrictState = $this->upperOrNull($params->restrictState);
106
107
            $params->restrictCountry = $this->upperOrNull($params->restrictCountry);
108
109
            $this->countryStateRestriction = new CountryStateRestriction(
110
                $params->restrictCountry,
111
                $params->restrictState
112
            );
113
        }
114
    }
115
116
    /**
117
     * Converts string to uppercase or null if null.
118
     *
119
     * @param string|null $param
120
     * @return string|null
121
     */
122
    protected function upperOrNull($param)
123
    {
124
        return (!is_null($param)) ? strtoupper($param) : $param;
125
    }
126
}
127