CurlHelper::getResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Bludata\Curl\Helpers;
4
5
class CurlHelper
6
{
7
    protected $init;
8
    protected $headers = [];
9
    protected $response;
10
    protected $info;
11
    protected $baseUrl;
12
    protected $posFixUrl;
13
    protected $options = [];
14
15
    public function __construct($baseUrl, array $headers = [])
16
    {
17
        $this->init = curl_init();
18
19
        $this->baseUrl = $baseUrl;
20
21
        $this->headers = $headers;
22
    }
23
24
    protected function exec()
25
    {
26
        curl_setopt($this->init, CURLOPT_URL, trim($this->baseUrl.$this->posFixUrl));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 85 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
27
        curl_setopt($this->init, CURLOPT_RETURNTRANSFER, true);
28
        curl_setopt($this->init, CURLOPT_HTTPHEADER, $this->headers);
29
        curl_setopt($this->init, CURLOPT_SSL_VERIFYPEER, false);
30
31
        foreach ($this->options as $key => $value) {
32
            curl_setopt($this->init, $key, $value);
33
        }
34
35
        $this->response = curl_exec($this->init);
36
37
        $this->info = curl_getinfo($this->init);
38
    }
39
40
    public function send($close = true)
41
    {
42
        $this->exec();
43
44
        if ($close === true) {
45
            curl_close($this->init);
46
        }
47
48
        return $this;
49
    }
50
51
    public function addHeader($header)
52
    {
53
        $this->headers[] = $header;
54
55
        return $this;
56
    }
57
58
    public function getResponse()
59
    {
60
        return [
61
            'code' => $this->info['http_code'],
62
            'data' => $this->response,
63
        ];
64
    }
65
66
    public function getInfo()
67
    {
68
        return $this->info;
69
    }
70
71
    public function setBaseUrl($baseUrl)
72
    {
73
        $this->baseUrl = $baseUrl;
74
75
        return $this;
76
    }
77
78
    public function setPosFixUrl($posFixUrl)
79
    {
80
        $this->posFixUrl = $posFixUrl;
81
82
        return $this;
83
    }
84
85
    public function post(array $data)
86
    {
87
        $this->options[CURLOPT_POST] = true;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
88
        $this->options[CURLOPT_POSTFIELDS] = json_encode($data);
89
90
        return $this;
91
    }
92
93
    public function put(array $data)
94
    {
95
        $this->options[CURLOPT_CUSTOMREQUEST] = 'PUT';
96
        $this->options[CURLOPT_POSTFIELDS] = json_encode($data);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
97
98
        return $this;
99
    }
100
101
    public function delete()
102
    {
103
        $this->options[CURLOPT_CUSTOMREQUEST] = 'DELETE';
104
105
        return $this;
106
    }
107
}
108