Issues (5)

src/Request/Curl.php (1 issue)

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

205
        return curl_setopt($this->handle, /** @scrutinizer ignore-type */ $option, $value);
Loading history...
206
    }
207
}
208