Test Failed
Branch develop (1fb6dd)
by Andrew
04:12
created

Curl::setOpt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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

200
        return curl_setopt($this->handle, /** @scrutinizer ignore-type */ $option, $value);
Loading history...
201
    }
202
}