britosql /
cafeapi
| 1 | <?php |
||
| 2 | |||
| 3 | namespace CarlosBrito\CafeApi; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Class CafeApi |
||
| 7 | * @package RobsonVLeite\CafeApi |
||
| 8 | * Callse Abstract e não pode ser instanciada, apenas herdar dela suas propriedades |
||
| 9 | */ |
||
| 10 | abstract class CafeApi |
||
| 11 | { |
||
| 12 | /** @var string */ |
||
| 13 | private $apiUrl; |
||
| 14 | |||
| 15 | /** @var array */ |
||
| 16 | private $headers; |
||
| 17 | |||
| 18 | /** @var array */ |
||
| 19 | private $fields; |
||
| 20 | |||
| 21 | /** @var string */ |
||
| 22 | private $endpoint; |
||
| 23 | |||
| 24 | /** @var string */ |
||
| 25 | private $method; |
||
| 26 | |||
| 27 | /** @var object */ |
||
| 28 | protected $response; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * CafeApi constructor. |
||
| 32 | * @param string $apiUrl |
||
| 33 | * @param string $email |
||
| 34 | * @param string $password |
||
| 35 | */ |
||
| 36 | public function __construct(string $apiUrl, string $email, string $password) |
||
| 37 | { |
||
| 38 | $this->apiUrl = $apiUrl; |
||
| 39 | $this->headers([ |
||
| 40 | "email" => $email, |
||
| 41 | "password" => $password |
||
| 42 | ]); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param string $method |
||
| 47 | * @param string $endpoint |
||
| 48 | * @param array|null $fields |
||
| 49 | * @param array|null $headers |
||
| 50 | */ |
||
| 51 | protected function request(string $method, string $endpoint, array $fields = null, array $headers = null): void |
||
| 52 | { |
||
| 53 | $this->method = $method; |
||
| 54 | $this->endpoint = $endpoint; |
||
| 55 | $this->fields = $fields; |
||
| 56 | $this->headers($headers); |
||
| 57 | |||
| 58 | $this->dispatch(); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return object|null |
||
| 63 | */ |
||
| 64 | public function response() |
||
| 65 | { |
||
| 66 | return $this->response; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return object|null |
||
| 71 | */ |
||
| 72 | public function error() |
||
| 73 | { |
||
| 74 | if (!empty($this->response->errors)) { |
||
| 75 | return $this->response->errors; |
||
| 76 | } |
||
| 77 | |||
| 78 | return null; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param array|null $headers |
||
| 83 | */ |
||
| 84 | private function headers(?array $headers): void |
||
| 85 | { |
||
| 86 | if (!$headers) { |
||
|
0 ignored issues
–
show
|
|||
| 87 | return; |
||
| 88 | } |
||
| 89 | |||
| 90 | foreach ($headers as $key => $header) { |
||
| 91 | $this->headers[] = "{$key}: {$header}"; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * |
||
| 97 | */ |
||
| 98 | private function dispatch(): void |
||
| 99 | { |
||
| 100 | $curl = curl_init(); |
||
| 101 | |||
| 102 | if (empty($this->fields["files"])) { |
||
| 103 | $this->fields = (!empty($this->fields) ? http_build_query($this->fields) : null); |
||
| 104 | } |
||
| 105 | |||
| 106 | curl_setopt_array($curl, array( |
||
| 107 | CURLOPT_URL => "{$this->apiUrl}/{$this->endpoint}", |
||
| 108 | CURLOPT_RETURNTRANSFER => true, |
||
| 109 | CURLOPT_MAXREDIRS => 10, |
||
| 110 | CURLOPT_TIMEOUT => 30, |
||
| 111 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
||
| 112 | CURLOPT_CUSTOMREQUEST => $this->method, |
||
| 113 | CURLOPT_POSTFIELDS => $this->fields, |
||
| 114 | CURLOPT_HTTPHEADER => $this->headers, |
||
| 115 | )); |
||
| 116 | |||
| 117 | $this->response = json_decode(curl_exec($curl)); |
||
| 118 | curl_close($curl); |
||
| 119 | } |
||
| 120 | } |
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.