GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Client::prepareUrl()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4888
c 0
b 0
f 0
cc 5
nc 3
nop 1
1
<?php
2
3
namespace dHttp;
4
5
use RuntimeException;
6
7
/**
8
 * dHttp - http client based curl
9
 *
10
 * @author Askar Fuzaylov <[email protected]>
11
 */
12
class Client
13
{
14
    /**
15
     * @var array
16
     */
17
    private $_default = [
18
        CURLOPT_ENCODING       => 'utf-8',
19
        CURLOPT_RETURNTRANSFER => true,
20
        CURLOPT_FOLLOWLOCATION => false,
21
        CURLOPT_SSL_VERIFYPEER => false,
22
        CURLOPT_USERAGENT      => 'PHP dHttp/Client 1.3'
23
    ];
24
    /**
25
     * @var array
26
     */
27
    private $_options = [];
28
29
    /**
30
     * Construct
31
     *
32
     * @param string $url
33
     * @param array $options
34
     * @throws RuntimeException
35
     */
36
    public function __construct($url = null, array $options = [])
37
    {
38
        if (!extension_loaded('curl')) {
39
            throw new RuntimeException('The PHP cURL extension must be installed to use dHttp');
40
        }
41
42
        // Force IPv4, since this class isn't yet compatible with IPv6
43
        if (self::v('features') & CURLOPT_IPRESOLVE) {
44
            $this->addOptions([CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4]);
45
        }
46
47
        // Merge with default options
48
        $this->addOptions($options);
49
        // Set URL
50
        $this->setUrl($url);
51
    }
52
53
    /**
54
     * Set URL
55
     *
56
     * @param string $url
57
     * @return Client
58
     */
59
    public function setUrl($url)
60
    {
61
        if ($url !== null) {
62
            $this->_options[CURLOPT_URL] = $this->prepareUrl($url);
63
        }
64
65
        return $this;
66
    }
67
68
    /**
69
     * Set user agent
70
     *
71
     * @param string $agent
72
     * @return Client
73
     */
74
    public function setUserAgent($agent)
75
    {
76
        $this->_options[CURLOPT_USERAGENT] = $agent;
77
        return $this;
78
    }
79
80
    /**
81
     * Set cookies
82
     *
83
     * @param string $cookie
84
     * @return Client
85
     */
86
    public function setCookie($cookie)
87
    {
88
        $this->_options[CURLOPT_COOKIEFILE] = $cookie;
89
        $this->_options[CURLOPT_COOKIEJAR]  = $cookie;
90
        return $this;
91
    }
92
93
    /**
94
     * Set referer
95
     *
96
     * @param string $referer
97
     * @return Client
98
     */
99
    public function setReferer($referer)
100
    {
101
        $this->_options[CURLOPT_REFERER] = $referer;
102
        return $this;
103
    }
104
105
    /**
106
     * The maximum amount of HTTP redirects to follow
107
     *
108
     * @param int $redirects
109
     * @return Client
110
     */
111
    public function setMaxRedirects($redirects)
112
    {
113
        $this->_options[CURLOPT_MAXREDIRS] = $redirects;
114
        return $this;
115
    }
116
117
    /**
118
     * The maximum number of seconds to allow cURL functions to execute.
119
     *
120
     * @param int $timeout
121
     * @return Client
122
     */
123
    public function setTimeout($timeout)
124
    {
125
        $this->_options[CURLOPT_TIMEOUT] = $timeout;
126
        return $this;
127
    }
128
129
    /**
130
     * The number of seconds to wait while trying to connect.
131
     *
132
     * @param int $timeout
133
     * @return Client
134
     */
135
    public function setConnectionTimeout($timeout)
136
    {
137
        $this->_options[CURLOPT_CONNECTTIMEOUT] = $timeout;
138
        return $this;
139
    }
140
141
    /**
142
     * Include the header in the output
143
     *
144
     * @param bool $show
145
     * @return Client
146
     */
147
    public function showHeaders($show)
148
    {
149
        $this->_options[CURLOPT_HEADER] = $show;
150
        return $this;
151
    }
152
153
    /**
154
     * Add options
155
     *
156
     * @param array $params
157
     * @return Client
158
     */
159
    public function addOptions(array $params)
160
    {
161
        if (!count($this->_options)) {
162
            $this->_options = $this->_default;
163
        }
164
165
        foreach ($params as $key => $val) {
166
            $this->_options[$key] = $val;
167
        }
168
169
        return $this;
170
    }
171
172
    /**
173
     * Send post request
174
     *
175
     * @param array $fields
176
     * @param array $options
177
     * @return Response
178
     */
179
    public function post($fields = [], array $options = [])
180
    {
181
        $this->addOptions($options + [
182
                CURLOPT_POST       => true,
183
                CURLOPT_POSTFIELDS => $fields
184
            ]);
185
        return $this->exec();
186
    }
187
188
    /**
189
     * Send put request
190
     *
191
     * @param array $fields
192
     * @param array $options
193
     * @return Response
194
     */
195
    public function put(array $fields = [], array $options = [])
196
    {
197
        $this->addOptions($options + [
198
                CURLOPT_CUSTOMREQUEST => 'PUT',
199
                CURLOPT_POSTFIELDS    => is_array($fields) ? http_build_query($fields) : $fields
200
            ]);
201
        return $this->exec();
202
    }
203
204
    /**
205
     * Send get request
206
     *
207
     * @param array $options
208
     * @return Response
209
     */
210
    public function get(array $options = [])
211
    {
212
        $this->addOptions($options);
213
        return $this->exec();
214
    }
215
216
    /**
217
     * Send delete request
218
     *
219
     * @param array $options
220
     * @return Response
221
     */
222
    public function delete(array $options = [])
223
    {
224
        return $this->get($options + [CURLOPT_CUSTOMREQUEST => 'DELETE']);
225
    }
226
227
    /**
228
     * Send multithread queries
229
     *
230
     * @param Client[] $handlers
231
     * @return array
232
     * @throws RuntimeException
233
     */
234
    public function multi(array $handlers)
235
    {
236
        //create the multiple cURL handle
237
        $mc        = curl_multi_init();
238
        $resources = [];
239
240
        foreach ($handlers as $item) {
241
            if (!$item instanceof Client) {
242
                throw new RuntimeException('Handler should be object instance of dHttp\Client');
243
            }
244
            $res = $item->init();
245
246
            curl_multi_add_handle($mc, $res);
247
            $resources[] = $res;
248
        }
249
250
        $running = null;
251
        do {
252
            usleep(100);
253
            curl_multi_exec($mc, $running);
254
        } while ($running > 0);
255
256
        $result = [];
257
        foreach ($resources as $item) {
258
            $resp = new Response([
259
                'response' => curl_multi_getcontent($item),
260
                'options'  => $this->_options,
261
                'info'     => curl_getinfo($item)
262
            ]);
263
264
            $errno = curl_errno($item);
265
            if ($errno) {
266
                $resp->setError([curl_errno($item) => curl_error($item)]);
267
            }
268
269
            $result[] = $resp;
270
            curl_multi_remove_handle($mc, $item);
271
        }
272
273
        curl_multi_close($mc);
274
        return $result;
275
    }
276
277
    /**
278
     * Execute the query
279
     *
280
     * @return Response
281
     */
282
    private function exec()
283
    {
284
        $ch = $this->init();
285
        // Collect response data
286
        $response = new Response([
287
            'response' => curl_exec($ch),
288
            'options'  => $this->_options,
289
            'info'     => curl_getinfo($ch)
290
        ]);
291
292
        $errno = curl_errno($ch);
293
        if ($errno) {
294
            $response->setError([$errno => curl_error($ch)]);
295
        }
296
        curl_close($ch);
297
298
        return $response;
299
    }
300
301
    /**
302
     * Initialize curl
303
     *
304
     * @return resource
305
     */
306
    public function init()
307
    {
308
        $ch = curl_init();
309
        // The initial parameters
310
        $this->setCurlOptions($ch, $this->_options);
311
        return $ch;
312
    }
313
314
    /**
315
     * Set curl options
316
     *
317
     * @param resource $ch
318
     * @param array $options
319
     * @return void
320
     */
321
    private function setCurlOptions(&$ch, array $options)
322
    {
323
        curl_setopt_array($ch, $options);
324
    }
325
326
    /**
327
     * Reset options
328
     *
329
     * @return Client
330
     */
331
    public function reset()
332
    {
333
        $this->_options = [];
334
        return $this;
335
    }
336
337
    /**
338
     * Generate url
339
     *
340
     * @param string |array $url
341
     * @return string
342
     */
343
    public function prepareUrl($url)
344
    {
345
        if (is_array($url) && count($url)) {
346
            $newUrl = $url[0];
347
348
            if (isset($url[1]) && is_array($url[1])) {
349
                $newUrl = '?' . http_build_query($url[1]);
350
            }
351
        } else {
352
            $newUrl = $url;
353
        }
354
355
        return $newUrl;
356
    }
357
358
    /**
359
     * Return curl information
360
     *
361
     * @param string $type
362
     * @return mixed
363
     */
364
    public static function v($type = 'version')
365
    {
366
        $info = curl_version();
367
        return array_key_exists($type, $info) ? $info[$type] : null;
368
    }
369
}
370