leandroferreirama /
google-calendar
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace LeandroFerreiraMa\GoogleCalendar; |
||||
| 4 | |||||
| 5 | abstract class GoogleCalendar |
||||
| 6 | { |
||||
| 7 | private string $apiUrl; |
||||
| 8 | private ?array $headers; |
||||
| 9 | private ?array $fields; |
||||
| 10 | private string $endpoint; |
||||
| 11 | private string $method; |
||||
| 12 | protected ?object $response; |
||||
| 13 | |||||
| 14 | public function __construct(string $token) |
||||
| 15 | { |
||||
| 16 | $this->apiUrl = 'https://www.googleapis.com/calendar'; |
||||
| 17 | $this->headers(['Authorization' => 'Bearer '. $token]); |
||||
| 18 | } |
||||
| 19 | |||||
| 20 | protected function request(string $method, string $endpoint, ?array $fields = null, ?array $headers = null): void |
||||
| 21 | { |
||||
| 22 | $this->method = $method; |
||||
| 23 | $this->endpoint = $endpoint; |
||||
| 24 | $this->fields = $fields; |
||||
| 25 | $this->headers($headers); |
||||
| 26 | |||||
| 27 | $this->dispatch(); |
||||
| 28 | } |
||||
| 29 | |||||
| 30 | public function response(): ?object |
||||
| 31 | { |
||||
| 32 | return $this->response; |
||||
| 33 | } |
||||
| 34 | |||||
| 35 | public function error(): ?object |
||||
| 36 | { |
||||
| 37 | if (!empty($this->response->errors)) { |
||||
| 38 | return $this->response->errors; |
||||
| 39 | } |
||||
| 40 | |||||
| 41 | return null; |
||||
| 42 | } |
||||
| 43 | |||||
| 44 | private function headers(?array $headers): void |
||||
| 45 | { |
||||
| 46 | if (!$headers) { |
||||
|
0 ignored issues
–
show
|
|||||
| 47 | return; |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | foreach ($headers as $key => $header) { |
||||
| 51 | $this->headers[] = "{$key}: {$header}"; |
||||
| 52 | } |
||||
| 53 | } |
||||
| 54 | |||||
| 55 | |||||
| 56 | private function dispatch(): void |
||||
| 57 | { |
||||
| 58 | $curl = curl_init(); |
||||
| 59 | $fields = (!empty($this->fields) ? json_encode($this->fields) : null); |
||||
| 60 | curl_setopt_array($curl, array( |
||||
| 61 | CURLOPT_URL => "{$this->apiUrl}/{$this->endpoint}", |
||||
| 62 | CURLOPT_RETURNTRANSFER => true, |
||||
| 63 | CURLOPT_MAXREDIRS => 10, |
||||
| 64 | CURLOPT_TIMEOUT => 30, |
||||
| 65 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
||||
| 66 | CURLOPT_CUSTOMREQUEST => $this->method, |
||||
| 67 | CURLOPT_POSTFIELDS => $fields, |
||||
| 68 | CURLOPT_HTTPHEADER => $this->headers, |
||||
| 69 | )); |
||||
| 70 | |||||
| 71 | $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...
|
|||||
| 72 | curl_close($curl); |
||||
| 73 | } |
||||
| 74 | } |
||||
| 75 |
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.