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