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