| Conditions | 9 |
| Paths | 80 |
| Total Lines | 76 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 148 | public function send() |
||
| 149 | { |
||
| 150 | // Preparing Parameters |
||
| 151 | $paramInQuery = null; |
||
| 152 | if (!empty($this->query)) { |
||
| 153 | $paramInQuery = '?' . http_build_query($this->query); |
||
| 154 | } |
||
| 155 | |||
| 156 | // Preparing Header |
||
| 157 | if (empty($this->requestHeader)) { |
||
| 158 | $this->requestHeader = []; |
||
| 159 | } |
||
| 160 | $header = array_merge( |
||
| 161 | [ |
||
| 162 | 'Accept' => 'application/json' |
||
| 163 | ], |
||
| 164 | $this->requestHeader |
||
| 165 | ); |
||
| 166 | |||
| 167 | // Defining Variables |
||
| 168 | $serverUrl = $this->swaggerSchema->getServerUrl() . $paramInQuery; |
||
| 169 | $basePath = $this->swaggerSchema->getBasePath(); |
||
| 170 | $pathName = $this->path; |
||
| 171 | |||
| 172 | // Check if the body is the expected before request |
||
| 173 | $bodyRequestDef = $this->swaggerSchema->getRequestParameters("$basePath$pathName", $this->method); |
||
| 174 | $bodyRequestDef->match($this->requestBody); |
||
| 175 | |||
| 176 | // Make the request |
||
| 177 | $request = new Request( |
||
| 178 | $this->method, |
||
| 179 | $serverUrl . $pathName . $paramInQuery, |
||
| 180 | $header, |
||
| 181 | json_encode($this->requestBody) |
||
| 182 | ); |
||
| 183 | |||
| 184 | $statusReturned = null; |
||
| 185 | try { |
||
| 186 | $response = $this->guzzleHttpClient->send($request, ['allow_redirects' => false]); |
||
| 187 | $responseHeader = $response->getHeaders(); |
||
| 188 | $responseBody = json_decode((string) $response->getBody(), true); |
||
| 189 | $statusReturned = $response->getStatusCode(); |
||
| 190 | } catch (BadResponseException $ex) { |
||
| 191 | $responseHeader = $ex->getResponse()->getHeaders(); |
||
| 192 | $responseBody = json_decode((string) $ex->getResponse()->getBody(), true); |
||
| 193 | $statusReturned = $ex->getResponse()->getStatusCode(); |
||
| 194 | } |
||
| 195 | |||
| 196 | // Assert results |
||
| 197 | if ($this->statusExpected != $statusReturned) { |
||
| 198 | throw new StatusCodeNotMatchedException( |
||
| 199 | "Status code not matched $statusReturned", |
||
| 200 | $responseBody |
||
| 201 | ); |
||
| 202 | } |
||
| 203 | |||
| 204 | $bodyResponseDef = $this->swaggerSchema->getResponseParameters( |
||
| 205 | "$basePath$pathName", |
||
| 206 | $this->method, |
||
| 207 | $this->statusExpected |
||
| 208 | ); |
||
| 209 | $bodyResponseDef->match($responseBody); |
||
| 210 | |||
| 211 | if (count($this->assertHeader) > 0) { |
||
| 212 | foreach ($this->assertHeader as $key => $value) { |
||
| 213 | if (!isset($responseHeader[$key]) || strpos($responseHeader[$key][0], $value) === false) { |
||
| 214 | throw new NotMatchedException( |
||
| 215 | "Does not exists header '$key' with value '$value'", |
||
| 216 | $responseHeader |
||
| 217 | ); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | return $responseBody; |
||
| 223 | } |
||
| 224 | } |
||
| 225 |