| Conditions | 20 |
| Paths | 37 |
| Total Lines | 88 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 | throw new Exception\InvalidCredentialsException('Your API key "' . $this->apiKey . '" is not valid for ' . $this->getName(), null, $ex); |
||
| 122 | } elseif($prevEx->hasResponse() !== true){ |
||
| 123 | var_dump($prevEx->getRequest()->getHeaders()); |
||
|
|
|||
| 124 | var_dump($prevEx->getRequest()->getBody()); |
||
| 125 | exit(); |
||
| 126 | } |
||
| 127 | |||
| 128 | throw $ex; |
||
| 129 | } |
||
| 130 | |||
| 131 | /* |
||
| 132 | * no json returned? |
||
| 133 | */ |
||
| 134 | $contentType = $response->getHeader('Content-Type'); |
||
| 135 | if (! isset($contentType[0]) || $contentType[0] != 'application/json; charset=utf-8') { |
||
| 136 | throw new Exception\RequestException('Could not get valid "application/json; charset=utf-8" response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"'); |
||
| 137 | } |
||
| 138 | |||
| 139 | $content = json_decode($response->getBody()->getContents()); |
||
| 140 | |||
| 141 | /* |
||
| 142 | * No result |
||
| 143 | */ |
||
| 144 | if (isset($content->MatchMethod) && $content->MatchMethod == 'None') { |
||
| 145 | throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
||
| 146 | } |
||
| 147 | |||
| 148 | /* |
||
| 149 | * Missing data? |
||
| 150 | */ |
||
| 151 | if (! $content instanceof stdClass || ! isset($content->Values)) { |
||
| 152 | throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Data is missing "' . $response->getBody()->getContents() . '"'); |
||
| 153 | } |
||
| 154 | |||
| 155 | /* |
||
| 156 | * Convert the values, to something useable |
||
| 157 | */ |
||
| 158 | $values = new \stdClass(); |
||
| 159 | $values->MatchMethod = $content->MatchMethod; |
||
| 160 | |||
| 161 | foreach ($content->Values as $key => $value) { |
||
| 162 | if (is_array($value) && count($value) === 1 && isset($value[0])) { |
||
| 163 | $values->{$key} = $value[0]; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | foreach ($values as $key => $value) { |
||
| 168 | if ($value === 'True') { |
||
| 169 | $values->{$key} = true; |
||
| 170 | } elseif ($value === 'False') { |
||
| 171 | $values->{$key} = false; |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | return $values; |
||
| 176 | } |
||
| 177 | |||
| 283 |