Passed
Push — main ( 3f278f...b572d4 )
by Leandro
04:17
created

ItauCertificate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 30
c 4
b 0
f 0
dl 0
loc 53
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B requestCertificate() 0 51 6
1
<?php
2
3
namespace Itau\API;
4
5
use Exception;
6
use Itau\API\Exception\ItauException;
7
8
/**
9
 * Class Itau
10
 *
11
 * @package Itau\API
12
 */
13
class ItauCertificate
14
{
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
    }
67
}
68