WagnerMontanini /
apinfefasa
| 1 | <?php |
||
| 2 | |||
| 3 | namespace WagnerMontanini\ApiNfeFasa; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Class ApiNfeFasa |
||
| 7 | * @package WagnerMontanini\ApiNfeFasa |
||
| 8 | */ |
||
| 9 | abstract class ApiNfeFasa |
||
| 10 | { |
||
| 11 | /** @var string */ |
||
| 12 | private $apiUrl; |
||
| 13 | |||
| 14 | /** @var array */ |
||
| 15 | private $headers; |
||
| 16 | |||
| 17 | /** @var array */ |
||
| 18 | private $fields; |
||
| 19 | |||
| 20 | /** @var string */ |
||
| 21 | private $endpoint; |
||
| 22 | |||
| 23 | /** @var string */ |
||
| 24 | private $method; |
||
| 25 | |||
| 26 | /** @var object */ |
||
| 27 | protected $response; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * ApiNfeFasa constructor. |
||
| 31 | * @param string $apiUrl |
||
| 32 | */ |
||
| 33 | public function __construct(string $apiUrl,?string $token = null) |
||
| 34 | { |
||
| 35 | $this->apiUrl = $apiUrl; |
||
| 36 | $headers = empty($token)?null:array("Authorization"=>"Bearer {$token}"); |
||
| 37 | $this->headers($headers); |
||
| 38 | |||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param string $method |
||
| 43 | * @param string $endpoint |
||
| 44 | * @param array|null $fields |
||
| 45 | * @param array|null $headers |
||
| 46 | */ |
||
| 47 | protected function request(string $method, string $endpoint, array $fields = null, array $headers = null): void |
||
| 48 | { |
||
| 49 | $this->method = $method; |
||
| 50 | $this->endpoint = $endpoint; |
||
| 51 | $this->fields = $fields; |
||
| 52 | $this->headers($headers); |
||
| 53 | |||
| 54 | $this->dispatch(); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @return object|null |
||
| 59 | */ |
||
| 60 | public function response() |
||
| 61 | { |
||
| 62 | return $this->response; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return object|null |
||
| 67 | */ |
||
| 68 | public function error() |
||
| 69 | { |
||
| 70 | if (!empty($this->response->errors)) { |
||
| 71 | return $this->response->errors; |
||
| 72 | } |
||
| 73 | |||
| 74 | return null; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param array|null $headers |
||
| 79 | */ |
||
| 80 | private function headers(?array $headers): void |
||
| 81 | { |
||
| 82 | if (!$headers) { |
||
|
0 ignored issues
–
show
|
|||
| 83 | return; |
||
| 84 | } |
||
| 85 | |||
| 86 | foreach ($headers as $key => $header) { |
||
| 87 | $this->headers[] = "{$key}: {$header}"; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * |
||
| 93 | */ |
||
| 94 | private function dispatch(): void |
||
| 95 | { |
||
| 96 | $curl = curl_init(); |
||
| 97 | |||
| 98 | if (empty($this->fields["files"])) { |
||
| 99 | $this->fields = (!empty($this->fields) ? http_build_query($this->fields) : null); |
||
| 100 | } |
||
| 101 | |||
| 102 | curl_setopt_array($curl, array( |
||
| 103 | CURLOPT_URL => "{$this->apiUrl}{$this->endpoint}", |
||
| 104 | CURLOPT_RETURNTRANSFER => true, |
||
| 105 | CURLOPT_ENCODING => '', |
||
| 106 | CURLOPT_MAXREDIRS => 10, |
||
| 107 | CURLOPT_TIMEOUT => 30, |
||
| 108 | CURLOPT_FOLLOWLOCATION => true, |
||
| 109 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
||
| 110 | CURLOPT_CUSTOMREQUEST => $this->method, |
||
| 111 | CURLOPT_POSTFIELDS => $this->fields, |
||
| 112 | CURLOPT_HTTPHEADER => $this->headers, |
||
| 113 | )); |
||
| 114 | |||
| 115 | $this->response = json_decode(curl_exec($curl)); |
||
| 116 | curl_close($curl); |
||
| 117 | } |
||
| 118 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.