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