| Total Complexity | 6 |
| Total Lines | 33 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 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') { |
||
| 16 | } |
||
| 17 | |||
| 18 | function __invoke(string $url, array $header=[], mixed $body=null):array { |
||
| 19 | if ($body != null) $body = json_encode($body); |
||
| 20 | |||
| 21 | $ch = curl_init($url); |
||
| 22 | |||
| 23 | // Defaults. |
||
| 24 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
| 25 | curl_setopt($ch, CURLINFO_HEADER_OUT, true); |
||
| 26 | // Header. |
||
| 27 | $header[] = 'Content-Type: application/json'; |
||
| 28 | $header[] = 'User-Agent: '.$this->userAgent; |
||
| 29 | if ($body != null) $header[] = 'Content-Length: '.strlen($body); |
||
| 30 | curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
||
| 31 | // Request Method and Body. |
||
| 32 | if ($this->method == self::POST) { |
||
| 33 | curl_setopt($ch, CURLOPT_POST, true); |
||
| 34 | if ($body != null) { |
||
| 35 | curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
||
| 36 | } |
||
| 37 | } |
||
| 38 | // Exec. |
||
| 48 |
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.