| Conditions | 7 |
| Paths | 7 |
| Total Lines | 55 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 37 | private function executeRequest(string $token, string $certificadoCSR) |
||
| 38 | { |
||
| 39 | $headers = [ |
||
| 40 | 'Content-Type: text/plain', |
||
| 41 | 'Authorization: Bearer ' . $token |
||
| 42 | ]; |
||
| 43 | |||
| 44 | $curl = curl_init(); |
||
| 45 | |||
| 46 | curl_setopt_array($curl, [ |
||
| 47 | CURLOPT_URL => $this->endpoint, |
||
| 48 | CURLOPT_HTTPHEADER => $headers, |
||
| 49 | CURLOPT_RETURNTRANSFER => true, |
||
| 50 | CURLOPT_CUSTOMREQUEST => 'POST', |
||
| 51 | CURLOPT_POSTFIELDS => $certificadoCSR, |
||
| 52 | ]); |
||
| 53 | |||
| 54 | try { |
||
| 55 | $response = curl_exec($curl); |
||
| 56 | } catch (Exception $e) { |
||
| 57 | curl_close($curl); |
||
| 58 | throw new ItauException($e->getMessage(), 100); |
||
| 59 | } |
||
| 60 | |||
| 61 | if ($response === false) { |
||
| 62 | $error = curl_error($curl); |
||
| 63 | curl_close($curl); |
||
| 64 | throw new ItauException('CURL Error: ' . $error, 100); |
||
| 65 | } |
||
| 66 | |||
| 67 | $statusCode = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
| 68 | curl_close($curl); |
||
| 69 | |||
| 70 | // Verifica status HTTP |
||
| 71 | if ($statusCode >= 400) { |
||
| 72 | $obj = json_decode($response); |
||
| 73 | $acao = $obj->acao ?? ''; |
||
| 74 | $mensagem = $obj->mensagem ?? 'Erro desconhecido'; |
||
| 75 | $acaoText = $acao ? "- {$acao}" : ''; |
||
| 76 | throw new ItauException("HTTP Error: $statusCode - $mensagem {$acaoText}", $statusCode); |
||
| 77 | } |
||
| 78 | |||
| 79 | // Lógica para 204 |
||
| 80 | if ($statusCode === 204) { |
||
| 81 | return [ |
||
| 82 | 'status_code' => 204 |
||
| 83 | ]; |
||
| 84 | } |
||
| 85 | |||
| 86 | // Verifica resposta vazia |
||
| 87 | if (empty($response)) { |
||
| 88 | throw new ItauException('Empty response received from server.', $statusCode); |
||
| 89 | } |
||
| 90 | |||
| 91 | return $response; |
||
| 92 | } |
||
| 94 |