| Conditions | 6 |
| Paths | 6 |
| Total Lines | 51 |
| Code Lines | 29 |
| 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 |
||
| 15 | public function requestCertificate(string $token, string $certificadoCSR) |
||
| 16 | { |
||
| 17 | $endpoint = 'https://sts.itau.com.br/seguranca/v1/certificado/solicitacao'; |
||
| 18 | $headers = [ |
||
| 19 | 'Content-Type: text/plain', |
||
| 20 | 'Authorization: Bearer ' . $token |
||
| 21 | ]; |
||
| 22 | |||
| 23 | $curl = curl_init(); |
||
| 24 | |||
| 25 | curl_setopt_array($curl, [ |
||
| 26 | CURLOPT_URL => $endpoint, |
||
| 27 | CURLOPT_HTTPHEADER => $headers, |
||
| 28 | CURLOPT_RETURNTRANSFER => true, |
||
| 29 | CURLOPT_CUSTOMREQUEST => 'POST', |
||
| 30 | CURLOPT_POSTFIELDS => $certificadoCSR, |
||
| 31 | ]); |
||
| 32 | |||
| 33 | try { |
||
| 34 | $response = curl_exec($curl); |
||
| 35 | } catch (Exception $e) { |
||
| 36 | throw new ItauException($e->getMessage(), 100); |
||
| 37 | } |
||
| 38 | |||
| 39 | if ($response === false) { |
||
| 40 | $error = curl_error($curl); |
||
| 41 | curl_close($curl); |
||
| 42 | throw new ItauException('CURL Error: ' . $error, 100); |
||
| 43 | } |
||
| 44 | |||
| 45 | $statusCode = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
| 46 | curl_close($curl); |
||
| 47 | |||
| 48 | // Verifica status HTTP |
||
| 49 | if ($statusCode >= 400) { |
||
| 50 | throw new ItauException("HTTP Error: $statusCode - $response", $statusCode); |
||
| 51 | } |
||
| 52 | |||
| 53 | // Lógica para 204 |
||
| 54 | if ($statusCode === 204) { |
||
| 55 | return [ |
||
| 56 | 'status_code' => 204 |
||
| 57 | ]; |
||
| 58 | } |
||
| 59 | |||
| 60 | // Verifica resposta vazia |
||
| 61 | if (empty($response)) { |
||
| 62 | throw new ItauException('Empty response received from server.', $statusCode); |
||
| 63 | } |
||
| 64 | |||
| 65 | return $response; |
||
| 66 | } |
||
| 68 |