francis94c /
ci-gmail
| 1 | <?php |
||||
| 2 | declare(strict_types=1); |
||||
| 3 | defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
| 4 | |||||
| 5 | class GMailCURL {
|
||||
| 6 | |||||
| 7 | const GET = 'GET'; |
||||
| 8 | const POST = 'POST'; |
||||
| 9 | |||||
| 10 | private $method; |
||||
| 11 | private $userAgent; |
||||
| 12 | |||||
| 13 | function __construct(string $method, string $userAgent='CodeIgniter GMail API') {
|
||||
| 14 | $this->method = $method; |
||||
| 15 | $this->userAgent = $userAgent; |
||||
| 16 | } |
||||
| 17 | |||||
| 18 | function __invoke(string $url, array $header=[], array $body=null):array {
|
||||
|
0 ignored issues
–
show
|
|||||
| 19 | if ($body != null) $body = json_encode($body); |
||||
| 20 | |||||
| 21 | $ch = curl_init($url); |
||||
| 22 | |||||
| 23 | // Defaults. |
||||
| 24 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||||
|
0 ignored issues
–
show
It seems like
$ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, 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...
|
|||||
| 25 | curl_setopt($ch, CURLINFO_HEADER_OUT, true); |
||||
| 26 | if (ENVIRONMENT == 'development') {
|
||||
|
0 ignored issues
–
show
|
|||||
| 27 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
||||
| 28 | } |
||||
| 29 | // Header. |
||||
| 30 | $header[] = 'Content-Type: application/json'; |
||||
| 31 | if ($body != null) {
|
||||
|
0 ignored issues
–
show
|
|||||
| 32 | $header[] = 'Content-Length: '.strlen($body); |
||||
| 33 | } else {
|
||||
| 34 | $header[] = 'Content-Length: 0'; |
||||
| 35 | } |
||||
| 36 | curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
||||
| 37 | curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); |
||||
| 38 | // Request Method and Body. |
||||
| 39 | if ($this->method == self::POST) {
|
||||
| 40 | curl_setopt($ch, CURLOPT_POST, true); |
||||
| 41 | if ($body != null) {
|
||||
|
0 ignored issues
–
show
|
|||||
| 42 | curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
||||
| 43 | } |
||||
| 44 | } |
||||
| 45 | // Exec. |
||||
| 46 | $response = curl_exec($ch); |
||||
|
0 ignored issues
–
show
It seems like
$ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, 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...
|
|||||
| 47 | $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||||
|
0 ignored issues
–
show
It seems like
$ch can also be of type false; however, parameter $ch of curl_getinfo() does only seem to accept resource, 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...
|
|||||
| 48 | curl_close($ch); |
||||
|
0 ignored issues
–
show
It seems like
$ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, 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...
|
|||||
| 49 | return [$code, $response]; |
||||
| 50 | } |
||||
| 51 | } |
||||
| 52 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.