Passed
Push — main ( 7b06b9...aecaa1 )
by Leandro
11:32
created

ItauCertificate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 52
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B requestCertificate() 0 50 6
1
<?php
2
3
namespace Itau\API;
4
5
use Exception;
6
7
/**
8
 * Class Itau
9
 *
10
 * @package Itau\API
11
 */
12
class ItauCertificate
13
{
14
    public function requestCertificate(string $token, string $certificadoCSR)
15
    {
16
        $endpoint = 'sts.itau.com.br/seguranca/v1/certificado/solicitacao';
17
        $headers = [
18
            'Content-Type: text/plain',
19
            'Authorization: Bearer ' . $token
20
        ];
21
22
        $curl = curl_init();
23
24
        curl_setopt_array($curl, [
25
            CURLOPT_URL => $endpoint,
26
            CURLOPT_HTTPHEADER => $headers,
27
            CURLOPT_RETURNTRANSFER => true,
28
            CURLOPT_CUSTOMREQUEST => 'POST',
29
            CURLOPT_POSTFIELDS => $certificadoCSR,
30
        ]);
31
32
        $response = curl_exec($curl);
33
34
        if ($response === false) {
35
            $error = curl_error($curl);
36
            curl_close($curl);
37
            throw new Exception('CURL Error: ' . $error, 100);
38
        }
39
40
        // Verify error
41
        if ($response === false) {
42
            $errorMessage = curl_error($curl);
43
        }
44
45
        $statusCode = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE);
46
        curl_close($curl);
47
        
48
        if ($statusCode >= 400) {
49
            // TODO see what it means code 100
50
            throw new Exception($response, 100);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type true; however, parameter $message of Exception::__construct() 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

50
            throw new Exception(/** @scrutinizer ignore-type */ $response, 100);
Loading history...
51
        }
52
        // Status code 204 don't have content. That means $response will be always false
53
        // Provides a custom content for $response to avoid error in the next if logic
54
        if ($statusCode === 204) {
55
            return [
56
                'status_code' => 204
57
            ];
58
        }
59
60
        if (! $response) {
61
            throw new Exception("Empty response, curl_error: $errorMessage", $statusCode);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $errorMessage does not seem to be defined for all execution paths leading up to this point.
Loading history...
62
        }
63
        return $response;
64
    }
65
}
66