Auth   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 30
c 1
b 0
f 0
dl 0
loc 56
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A sandbox() 0 4 1
A __construct() 0 3 1
A production() 0 4 1
A dispatch() 0 18 1
A token() 0 9 1
1
<?php
2
3
namespace LeandroFerreiraMa\ItauAuth;
4
5
class Auth
6
{
7
    const PRODUCTION = '';
8
    const SANDBOX = 'https://devportal.itau.com.br/api/jwt';
9
10
    private string $apiUrl;
11
    private array $fields;
12
    private string $method;
13
    protected ?object $response;
14
15
    public function __construct()
16
    {
17
        $this->method = 'POST';
18
    }
19
20
    public function production(): self
21
    {
22
        $this->apiUrl = self::PRODUCTION;
23
        return $this;
24
    }
25
26
    public function sandbox(): self
27
    {
28
        $this->apiUrl = self::SANDBOX;
29
        return $this;
30
    }
31
32
    public function token(string $clientId, string $clientSecret): ?object
33
    {
34
        $this->fields = array_map("strip_tags", [
35
            'client_id' => $clientId,
36
            'client_secret' => $clientSecret
37
        ]);
38
39
        $this->dispatch();
40
        return $this->response;
41
    }
42
43
    private function dispatch(): void
44
    {
45
        $curl = curl_init();
46
        $fields = http_build_query($this->fields);
47
48
        curl_setopt_array($curl, array(
49
            CURLOPT_URL => $this->apiUrl,
50
            CURLOPT_RETURNTRANSFER => true,
51
            CURLOPT_MAXREDIRS => 10,
52
            CURLOPT_TIMEOUT => 30,
53
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
54
            CURLOPT_CUSTOMREQUEST => $this->method,
55
            CURLOPT_POSTFIELDS => $fields,
56
            CURLOPT_HTTPHEADER => [],
57
        ));
58
59
        $this->response = json_decode(curl_exec($curl));
0 ignored issues
show
Bug introduced by
It seems like curl_exec($curl) 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

59
        $this->response = json_decode(/** @scrutinizer ignore-type */ curl_exec($curl));
Loading history...
60
        curl_close($curl);
61
    }
62
}