Passed
Push — develop ( 7bef67...c4e703 )
by Andrew
02:08
created

Curl::getResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
4
namespace ddlzz\AmoAPI\Request;
5
6
use ddlzz\AmoAPI\Exceptions\CurlException;
7
8
9
/**
10
 * An abstraction layer for interacting with cURL library.
11
 * @package ddlzz\AmoAPI
12
 * @author ddlzz
13
 */
14
class Curl
15
{
16
    /** @var resource */
17
    private $curl;
18
19 4
    public function init()
20
    {
21 4
        $this->curl = curl_init();
22 4
    }
23
24 3
    public function close()
25
    {
26 3
        curl_close($this->curl);
27 3
    }
28
29
    /**
30
     * @return array|false
31
     * @throws CurlException
32
     */
33 2
    public function exec()
34
    {
35 2
        if (!is_resource($this->curl)) {
36 1
            throw new CurlException('Curl class is not properly initialized');
37
        }
38
39 1
        return curl_exec($this->curl);
40
    }
41
42
    /**
43
     * @return resource
44
     */
45 1
    public function getResource()
46
    {
47 1
        return $this->curl;
48
    }
49
50
    ///////////////////////////////
51
    // The list of possible options
52
    ///////////////////////////////
53
54
    /**
55
     * @param string $url
56
     * @return bool
57
     * @throws CurlException
58
     */
59 1
    public function setUrl($url)
60
    {
61 1
        return $this->setOpt(CURLOPT_URL, $url);
62
    }
63
64
    /**
65
     * @param bool $value
66
     * @return bool
67
     * @throws CurlException
68
     */
69 1
    public function setReturnTransfer($value)
70
    {
71 1
        return $this->setOpt(CURLOPT_RETURNTRANSFER, $value);
72
    }
73
74
    /**
75
     * @param string $agent
76
     * @return bool
77
     * @throws CurlException
78
     */
79
    public function setUserAgent($agent)
80
    {
81
        return $this->setOpt(CURLOPT_USERAGENT, $agent);
82
    }
83
84
    /**
85
     * @param array $header
86
     * @return bool
87
     * @throws CurlException
88
     */
89
    public function setHttpHeader(array $header)
90
    {
91
        return $this->setOpt(CURLOPT_HTTPHEADER, $header);
92
    }
93
94
    /**
95
     * @param bool $value
96
     * @return bool
97
     * @throws CurlException
98
     */
99
    public function setHeader($value)
100
    {
101
        return $this->setOpt(CURLOPT_HEADER, $value);
102
    }
103
104
    /**
105
     * @param string $cookie
106
     * @return bool
107
     * @throws CurlException
108
     */
109
    public function setCookieFile($cookie)
110
    {
111
        return $this->setOpt(CURLOPT_COOKIEFILE, $cookie);
112
    }
113
114
    /**
115
     * @param string $cookieJar
116
     * @return bool
117
     * @throws CurlException
118
     */
119
    public function setCookieJar($cookieJar)
120
    {
121
        return $this->setOpt(CURLOPT_COOKIEJAR, $cookieJar);
122
    }
123
124
    /**
125
     * @param bool $value
126
     * @return bool
127
     * @throws CurlException
128
     */
129
    public function setSSLVerifyPeer($value)
130
    {
131
        return $this->setOpt(CURLOPT_SSL_VERIFYPEER, $value);
132
    }
133
134
    /**
135
     * @param bool $value
136
     * @return bool
137
     * @throws CurlException
138
     */
139
    public function setSSLVerifyHost($value)
140
    {
141
        return $this->setOpt(CURLOPT_SSL_VERIFYHOST, $value);
142
    }
143
144
    /**
145
     * @param string $request
146
     * @return bool
147
     * @throws CurlException
148
     */
149
    public function setCustomRequest($request)
150
    {
151
        return $this->setOpt(CURLOPT_CUSTOMREQUEST, $request);
152
    }
153
154
    /**
155
     * @param mixed $fields
156
     * @return bool
157
     * @throws CurlException
158
     */
159
    public function setPostFields($fields)
160
    {
161
        return $this->setOpt(CURLOPT_POSTFIELDS, $fields);
162
    }
163
164
    //////////////////////
165
    // The end of the list
166
    //////////////////////
167
168
    /**
169
     * @return int
170
     */
171
    public function getHttpCode()
172
    {
173
        return curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
174
    }
175
176
    /**
177
     * Closes the connection in case of abnormal termination
178
     * @throws CurlException
179
     */
180 1
    public function __destruct()
181
    {
182 1
        if (is_resource($this->curl)) {
183 1
            curl_close($this->curl);
184 1
            throw new CurlException('Curl class was not properly closed');
185
        }
186
    }
187
188
    /**
189
     * Sets various cURL options.
190
     *
191
     * @param string $option
192
     * @param mixed $value
193
     * @return bool
194
     * @throws CurlException
195
     */
196 1
    private function setOpt($option, $value)
197
    {
198 1
        if (!is_resource($this->curl)) {
199
            throw new CurlException('Curl class is not properly initialized');
200
        }
201
202 1
        return curl_setopt($this->curl, $option, $value);
0 ignored issues
show
Bug introduced by
$option of type string is incompatible with the type integer expected by parameter $option of curl_setopt(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

202
        return curl_setopt($this->curl, /** @scrutinizer ignore-type */ $option, $value);
Loading history...
203
    }
204
}