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