Failed Conditions
Push — rbac ( be68b4...52c28b )
by Michael
03:11
created

HttpHelper::post()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 18
ccs 0
cts 12
cp 0
rs 9.9666
cc 2
nc 2
nop 3
crap 6
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
     */
23
    public function __construct($userAgent, $disableVerifyPeer)
24
    {
25
        $this->curlHandle = curl_init();
26
27
        curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true);
0 ignored issues
show
Bug introduced by
It seems like $this->curlHandle can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

27
        curl_setopt(/** @scrutinizer ignore-type */ $this->curlHandle, CURLOPT_RETURNTRANSFER, true);
Loading history...
28
        curl_setopt($this->curlHandle, CURLOPT_USERAGENT, $userAgent);
29
        curl_setopt($this->curlHandle, CURLOPT_FAILONERROR, true);
30
31
        if ($disableVerifyPeer) {
32
            curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, false);
33
        }
34
    }
0 ignored issues
show
Coding Style introduced by
Expected //end __construct()
Loading history...
35
36
    public function __destruct()
37
    {
38
        curl_close($this->curlHandle);
0 ignored issues
show
Bug introduced by
It seems like $this->curlHandle can also be of type boolean; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

38
        curl_close(/** @scrutinizer ignore-type */ $this->curlHandle);
Loading history...
39
    }
0 ignored issues
show
Coding Style introduced by
Expected //end __destruct()
Loading history...
40
41
    /**
42
     * Fetches the content of a URL, with an optional parameter set.
43
     *
44
     * @param string     $url        The URL to fetch.
45
     * @param null|array $parameters Key/value pair of GET parameters to add to the request.
46
     *                               Null lets you handle it yourself.
47
     *
48
     * @param array      $headers
49
     *
50
     * @return string
51
     * @throws CurlException
52
     */
53
    public function get($url, $parameters = null, $headers = array())
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$parameters" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$parameters"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$headers" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$headers"; expected 0 but found 1
Loading history...
54
    {
55
        if ($parameters !== null && is_array($parameters)) {
56
            $getString = '?' . http_build_query($parameters);
57
            $url .= $getString;
58
        }
59
60
        curl_setopt($this->curlHandle, CURLOPT_URL, $url);
0 ignored issues
show
Bug introduced by
It seems like $this->curlHandle can also be of type boolean; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

60
        curl_setopt(/** @scrutinizer ignore-type */ $this->curlHandle, CURLOPT_URL, $url);
Loading history...
61
62
        // Make sure we're doing a GET
63
        curl_setopt($this->curlHandle, CURLOPT_POST, false);
64
65
        curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers);
66
67
        $result = curl_exec($this->curlHandle);
0 ignored issues
show
Bug introduced by
It seems like $this->curlHandle can also be of type boolean; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

67
        $result = curl_exec(/** @scrutinizer ignore-type */ $this->curlHandle);
Loading history...
68
69
        if ($result === false) {
70
            $error = curl_error($this->curlHandle);
0 ignored issues
show
Bug introduced by
It seems like $this->curlHandle can also be of type boolean; however, parameter $ch of curl_error() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

70
            $error = curl_error(/** @scrutinizer ignore-type */ $this->curlHandle);
Loading history...
71
            throw new CurlException('Remote request failed with error ' . $error);
72
        }
73
74
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result also could return the type true which is incompatible with the documented return type string.
Loading history...
75
    }
0 ignored issues
show
Coding Style introduced by
Expected //end get()
Loading history...
76
77
    /**
78
     * Posts data to a URL
79
     *
80
     * @param string $url        The URL to fetch.
81
     * @param array  $parameters Key/value pair of POST parameters to add to the request.
82
     * @param array  $headers
83
     *
84
     * @return string
85
     * @throws CurlException
86
     */
87
    public function post($url, $parameters, $headers = array())
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$headers" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$headers"; expected 0 but found 1
Loading history...
88
    {
89
        curl_setopt($this->curlHandle, CURLOPT_URL, $url);
0 ignored issues
show
Bug introduced by
It seems like $this->curlHandle can also be of type boolean; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

89
        curl_setopt(/** @scrutinizer ignore-type */ $this->curlHandle, CURLOPT_URL, $url);
Loading history...
90
91
        // Make sure we're doing a POST
92
        curl_setopt($this->curlHandle, CURLOPT_POST, true);
93
        curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, http_build_query($parameters));
94
95
        curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers);
96
97
        $result = curl_exec($this->curlHandle);
0 ignored issues
show
Bug introduced by
It seems like $this->curlHandle can also be of type boolean; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

97
        $result = curl_exec(/** @scrutinizer ignore-type */ $this->curlHandle);
Loading history...
98
99
        if ($result === false) {
100
            $error = curl_error($this->curlHandle);
0 ignored issues
show
Bug introduced by
It seems like $this->curlHandle can also be of type boolean; however, parameter $ch of curl_error() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

100
            $error = curl_error(/** @scrutinizer ignore-type */ $this->curlHandle);
Loading history...
101
            throw new CurlException('Remote request failed with error ' . $error);
102
        }
103
104
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result also could return the type true which is incompatible with the documented return type string.
Loading history...
105
    }
0 ignored issues
show
Coding Style introduced by
Expected //end post()
Loading history...
106
107
    /**
108
     * @return string
109
     */
110
    public function getError()
111
    {
112
        return curl_error($this->curlHandle);
0 ignored issues
show
Bug introduced by
It seems like $this->curlHandle can also be of type boolean; however, parameter $ch of curl_error() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

112
        return curl_error(/** @scrutinizer ignore-type */ $this->curlHandle);
Loading history...
113
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getError()
Loading history...
114
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...