Conditions | 20 |
Paths | 37 |
Total Lines | 92 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
89 | protected function getResult($userAgent, array $headers) |
||
90 | { |
||
91 | /* |
||
92 | * an empty UserAgent makes no sense |
||
93 | */ |
||
94 | if ($userAgent == '') { |
||
95 | throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
||
96 | } |
||
97 | |||
98 | $headers['User-Agent'] = $userAgent; |
||
99 | |||
100 | $parameters = '/' . $this->apiKey; |
||
101 | $parameters .= '/match?'; |
||
102 | |||
103 | $headerString = []; |
||
104 | foreach ($headers as $key => $value) { |
||
105 | $headerString[] = $key . '=' . rawurlencode($value); |
||
106 | } |
||
107 | |||
108 | $parameters .= implode('&', $headerString); |
||
109 | |||
110 | $uri = self::$uri . $parameters; |
||
111 | |||
112 | $request = new Request('GET', $uri); |
||
113 | |||
114 | try { |
||
115 | $response = $this->getResponse($request); |
||
116 | } catch (Exception\RequestException $ex) { |
||
117 | /* @var $prevEx \GuzzleHttp\Exception\ClientException */ |
||
118 | $prevEx = $ex->getPrevious(); |
||
119 | |||
120 | if ($prevEx->hasResponse() === true && $prevEx->getResponse()->getStatusCode() === 403) { |
||
121 | var_dump($prevEx->getRequest()); |
||
|
|||
122 | exit(); |
||
123 | |||
124 | throw new Exception\InvalidCredentialsException('Your API key "' . $this->apiKey . '" is not valid for ' . $this->getName(), null, $ex); |
||
125 | } elseif ($prevEx->hasResponse() !== true) { |
||
126 | var_dump($prevEx->getRequest()); |
||
127 | var_dump($prevEx); |
||
128 | var_dump($ex); |
||
129 | exit(); |
||
130 | } |
||
131 | |||
132 | throw $ex; |
||
133 | } |
||
134 | |||
135 | /* |
||
136 | * no json returned? |
||
137 | */ |
||
138 | $contentType = $response->getHeader('Content-Type'); |
||
139 | if (! isset($contentType[0]) || $contentType[0] != 'application/json; charset=utf-8') { |
||
140 | throw new Exception\RequestException('Could not get valid "application/json; charset=utf-8" response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"'); |
||
141 | } |
||
142 | |||
143 | $content = json_decode($response->getBody()->getContents()); |
||
144 | |||
145 | /* |
||
146 | * No result |
||
147 | */ |
||
148 | if (isset($content->MatchMethod) && $content->MatchMethod == 'None') { |
||
149 | throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
||
150 | } |
||
151 | |||
152 | /* |
||
153 | * Missing data? |
||
154 | */ |
||
155 | if (! $content instanceof stdClass || ! isset($content->Values)) { |
||
156 | throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Data is missing "' . $response->getBody()->getContents() . '"'); |
||
157 | } |
||
158 | |||
159 | /* |
||
160 | * Convert the values, to something useable |
||
161 | */ |
||
162 | $values = new \stdClass(); |
||
163 | $values->MatchMethod = $content->MatchMethod; |
||
164 | |||
165 | foreach ($content->Values as $key => $value) { |
||
166 | if (is_array($value) && count($value) === 1 && isset($value[0])) { |
||
167 | $values->{$key} = $value[0]; |
||
168 | } |
||
169 | } |
||
170 | |||
171 | foreach ($values as $key => $value) { |
||
172 | if ($value === 'True') { |
||
173 | $values->{$key} = true; |
||
174 | } elseif ($value === 'False') { |
||
175 | $values->{$key} = false; |
||
176 | } |
||
177 | } |
||
178 | |||
179 | return $values; |
||
180 | } |
||
181 | |||
287 |