Failed Conditions
Push — newinternal ( b66232...216d62 )
by Simon
16:33 queued 06:35
created

HttpHelper::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 3
dl 0
loc 17
ccs 0
cts 13
cp 0
crap 12
rs 9.7
c 0
b 0
f 0
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Helpers;
10
11
use Waca\Exceptions\CurlException;
12
13
class HttpHelper
14
{
15
    private $curlHandle;
16
17
    /**
18
     * HttpHelper constructor.
19
     *
20
     * @param string  $userAgent
21
     * @param boolean $disableVerifyPeer
22
     * @param string  $cookieJar
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cookieJar not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
23
     */
24
    public function __construct($userAgent, $disableVerifyPeer, $cookieJar = null)
25
    {
26
        $this->curlHandle = curl_init();
27
28
        curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true);
29
        curl_setopt($this->curlHandle, CURLOPT_USERAGENT, $userAgent);
30
        curl_setopt($this->curlHandle, CURLOPT_FAILONERROR, true);
31
32
        if ($disableVerifyPeer) {
33
            curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, false);
34
        }
35
36
        if($cookieJar !== null) {
37
            curl_setopt($this->curlHandle, CURLOPT_COOKIEFILE, $cookieJar);
38
            curl_setopt($this->curlHandle, CURLOPT_COOKIEJAR, $cookieJar);
39
        }
40
    }
41
42
    public function __destruct()
43
    {
44
        curl_close($this->curlHandle);
45
    }
46
47
    /**
48
     * Fetches the content of a URL, with an optional parameter set.
49
     *
50
     * @param string     $url        The URL to fetch.
51
     * @param null|array $parameters Key/value pair of GET parameters to add to the request.
52
     *                               Null lets you handle it yourself.
53
     *
54
     * @param array      $headers
55
     *
56
     * @return string
57
     * @throws CurlException
58
     */
59
    public function get($url, $parameters = null, $headers = array())
60
    {
61
        if ($parameters !== null && is_array($parameters)) {
62
            $getString = '?' . http_build_query($parameters);
63
            $url .= $getString;
64
        }
65
66
        curl_setopt($this->curlHandle, CURLOPT_URL, $url);
67
68
        // Make sure we're doing a GET
69
        curl_setopt($this->curlHandle, CURLOPT_POST, false);
70
71
        curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers);
72
73
        $result = curl_exec($this->curlHandle);
74
75 View Code Duplication
        if ($result === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
            $error = curl_error($this->curlHandle);
77
            throw new CurlException('Remote request failed with error ' . $error);
78
        }
79
80
        return $result;
81
    }
82
83
    /**
84
     * Posts data to a URL
85
     *
86
     * @param string $url        The URL to fetch.
87
     * @param array  $parameters Key/value pair of POST parameters to add to the request.
88
     * @param array  $headers
89
     *
90
     * @return string
91
     * @throws CurlException
92
     */
93
    public function post($url, $parameters, $headers = array())
94
    {
95
        curl_setopt($this->curlHandle, CURLOPT_URL, $url);
96
97
        // Make sure we're doing a POST
98
        curl_setopt($this->curlHandle, CURLOPT_POST, true);
99
        curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, http_build_query($parameters));
100
101
        curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers);
102
103
        $result = curl_exec($this->curlHandle);
104
105 View Code Duplication
        if ($result === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
            $error = curl_error($this->curlHandle);
107
            throw new CurlException('Remote request failed with error ' . $error);
108
        }
109
110
        return $result;
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getError()
117
    {
118
        return curl_error($this->curlHandle);
119
    }
120
}