Passed
Push — master ( 3e41fe...15a2ff )
by Francis
01:32
created

GMailCURL::__invoke()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 21
c 4
b 0
f 0
dl 0
loc 32
rs 8.9617
cc 6
nc 24
nop 3
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
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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
Bug introduced by
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 ignore-type  annotation

24
    curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, true);
Loading history...
25
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
26
    if (ENVIRONMENT == 'development') {
0 ignored issues
show
Bug introduced by
The constant ENVIRONMENT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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
Bug introduced by
It seems like you are loosely comparing $body of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
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
Bug introduced by
It seems like you are loosely comparing $body of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
42
        curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
43
      }
44
    }
45
    // Exec.
46
    $response = curl_exec($ch);
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

46
    $response = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
47
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

47
    $code = curl_getinfo(/** @scrutinizer ignore-type */ $ch, CURLINFO_HTTP_CODE);
Loading history...
48
    curl_close($ch);
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

48
    curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
49
    return [$code, $response];
50
  }
51
}
52