Passed
Push — main ( ecd95e...dbd039 )
by Leandro
02:21
created

ItauCertificate   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 12
Bugs 0 Features 0
Metric Value
eloc 46
c 12
b 0
f 0
dl 0
loc 79
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B executeRequest() 0 55 7
A criarCertificado() 0 3 1
A renovarCertificado() 0 13 2
1
<?php
2
3
namespace Itau\API;
4
5
use Exception;
6
use Itau\API\Exception\ItauException;
7
8
/**
9
 * Class ItauCertificate
10
 *
11
 * @package Itau\API
12
 */
13
class ItauCertificate
14
{
15
    private string $endpoint = 'https://sts.itau.com.br/seguranca/v1/certificado/solicitacao';
16
17
    public function criarCertificado(string $token, string $certificadoCSR)
18
    {
19
        return $this->executeRequest($token, $certificadoCSR);
20
    }
21
22
    public function renovarCertificado(Itau $credentials, string $certificadoCSR)
23
    {
24
        echo "<h1>API</h1>";
25
        var_dump($credentials->getAuthorizationToken());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($credentials->getAuthorizationToken()) looks like debug code. Are you sure you do not want to remove it?
Loading history...
26
        if (!$credentials->getAuthorizationToken()) {
27
            echo '<hr>';
28
            var_dump($credentials);
29
            new Request($credentials);
30
        }
31
        $token = $credentials->getAuthorizationToken();
32
        echo '<hr>';
33
        var_dump($token);
34
        return $this->executeRequest($token, $certificadoCSR);
35
    }
36
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);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
            $obj = json_decode(/** @scrutinizer ignore-type */ $response);
Loading history...
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
    }
93
}
94