| Conditions | 9 |
| Paths | 80 |
| Total Lines | 78 |
| Code Lines | 47 |
| 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 |
||
| 131 | public function send() |
||
| 132 | { |
||
| 133 | // Preparing Parameters |
||
| 134 | $paramInQuery = null; |
||
| 135 | if (!empty($this->query)) { |
||
| 136 | $paramInQuery = '?' . http_build_query($this->query); |
||
| 137 | } |
||
| 138 | |||
| 139 | // Preparing Header |
||
| 140 | if (empty($this->requestHeader)) { |
||
| 141 | $this->requestHeader = []; |
||
| 142 | } |
||
| 143 | $header = array_merge( |
||
| 144 | [ |
||
| 145 | 'Accept' => 'application/json' |
||
| 146 | ], |
||
| 147 | $this->requestHeader |
||
| 148 | ); |
||
| 149 | |||
| 150 | // Defining Variables |
||
| 151 | $httpSchema = $this->swaggerSchema->getHttpSchema(); |
||
| 152 | $host = $this->swaggerSchema->getHost(); |
||
| 153 | $basePath = $this->swaggerSchema->getBasePath(); |
||
| 154 | $path = $this->path; |
||
| 155 | |||
| 156 | // Check if the body is the expected before request |
||
| 157 | $bodyRequestDef = $this->swaggerSchema->getRequestParameters("$basePath$path", $this->method); |
||
| 158 | $bodyRequestDef->match($this->requestBody); |
||
| 159 | |||
| 160 | // Make the request |
||
| 161 | $request = new Request( |
||
| 162 | $this->method, |
||
| 163 | "$httpSchema://$host$basePath$path$paramInQuery", |
||
| 164 | $header, |
||
| 165 | json_encode($this->requestBody) |
||
| 166 | ); |
||
| 167 | |||
| 168 | $statusReturned = null; |
||
| 169 | $responseHeader = []; |
||
| 170 | try { |
||
| 171 | $response = $this->guzzleHttpClient->send($request, ['allow_redirects' => false]); |
||
| 172 | $responseHeader = $response->getHeaders(); |
||
| 173 | $responseBody = json_decode((string) $response->getBody(), true); |
||
| 174 | $statusReturned = $response->getStatusCode(); |
||
| 175 | } catch (BadResponseException $ex) { |
||
| 176 | $responseHeader = $ex->getResponse()->getHeaders(); |
||
| 177 | $responseBody = json_decode((string) $ex->getResponse()->getBody(), true); |
||
| 178 | $statusReturned = $ex->getResponse()->getStatusCode(); |
||
| 179 | } |
||
| 180 | |||
| 181 | // Assert results |
||
| 182 | if ($this->statusExpected != $statusReturned) { |
||
| 183 | throw new StatusCodeNotMatchedException( |
||
| 184 | "Status code not matched $statusReturned", |
||
| 185 | $responseBody |
||
| 186 | ); |
||
| 187 | } |
||
| 188 | |||
| 189 | $bodyResponseDef = $this->swaggerSchema->getResponseParameters( |
||
| 190 | "$basePath$path", |
||
| 191 | $this->method, |
||
| 192 | $this->statusExpected |
||
| 193 | ); |
||
| 194 | $bodyResponseDef->match($responseBody); |
||
| 195 | |||
| 196 | if (count($this->assertHeader) > 0) { |
||
| 197 | foreach ($this->assertHeader as $key => $value) { |
||
| 198 | if (!isset($responseHeader[$key]) || strpos($responseHeader[$key][0], $value) === false) { |
||
| 199 | throw new NotMatchedException( |
||
| 200 | "Does not exists header '$key' with value '$value'", |
||
| 201 | $responseHeader |
||
| 202 | ); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | return $responseBody; |
||
| 208 | } |
||
| 209 | } |
||
| 210 |