Passed
Push — master ( c14ede...c50061 )
by Ferry
03:59
created

CurlHelper::headers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: User
5
 * Date: 8/13/2019
6
 * Time: 10:33 PM
7
 */
8
9
namespace crocodicstudio\crudbooster\helpers;
10
11
12
class CurlHelper
13
{
14
15
    private $url;
16
    private $headers;
17
    private $data;
18
    private $type;
19
20
    public function __construct($url, $type = "POST")
21
    {
22
        $this->url = $url;
23
        $this->type = $type;
24
    }
25
26
    public function headers(array $headers) {
27
        $newHeaders = [];
28
        foreach($headers as $key=>$val) {
29
            $newHeaders[] = $key.": ".$val;
30
        }
31
        $this->headers = $newHeaders;
32
    }
33
34
    public function data(array $data) {
35
        $this->data = $data;
36
    }
37
38
    public function send() {
39
        $ch = curl_init($this->url);
40
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->type);
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

40
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_CUSTOMREQUEST, $this->type);
Loading history...
41
        curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
42
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
43
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
44
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->data);
45
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
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
        $err = curl_error($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_error() 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
        $err = curl_error(/** @scrutinizer ignore-type */ $ch);
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
50
        if($err) {
51
            return $err;
52
        }else {
53
            return $response;
54
        }
55
    }
56
}