leandroferreirama /
itau-auth
| 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
Loading history...
|
|||
| 60 | curl_close($curl); |
||
| 61 | } |
||
| 62 | } |