Completed
Branch newinternal (ffe884)
by Simon
04:07
created

HttpHelper::post()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 4
Ratio 21.05 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 4
loc 19
ccs 0
cts 10
cp 0
rs 9.4285
cc 2
eloc 10
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 1
	public function __construct($userAgent, $disableVerifyPeer)
24
	{
25 1
		$this->curlHandle = curl_init();
26
27 1
		curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true);
28 1
		curl_setopt($this->curlHandle, CURLOPT_USERAGENT, $userAgent);
29 1
		curl_setopt($this->curlHandle, CURLOPT_FAILONERROR, true);
30
31 1
		if ($disableVerifyPeer) {
32 1
			curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, false);
33 1
		}
34 1
	}
35
36 1
	public function __destruct()
37
	{
38 1
		curl_close($this->curlHandle);
39 1
	}
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 1
	public function get($url, $parameters = null, $headers = array())
54
	{
55 1
		if ($parameters !== null && is_array($parameters)) {
56 1
			$getString = '?' . http_build_query($parameters);
57 1
			$url .= $getString;
58 1
		}
59
60 1
		curl_setopt($this->curlHandle, CURLOPT_URL, $url);
61
62
		// Make sure we're doing a GET
63 1
		curl_setopt($this->curlHandle, CURLOPT_POST, false);
64
65 1
		curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers);
66
67 1
		$result = curl_exec($this->curlHandle);
68
69 1 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...
70
			$error = curl_error($this->curlHandle);
71
			throw new CurlException('Remote request failed with error ' . $error);
72
		}
73
74 1
		return $result;
75
	}
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())
88
	{
89
		curl_setopt($this->curlHandle, CURLOPT_URL, $url);
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);
98
99 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...
100
			$error = curl_error($this->curlHandle);
101
			throw new CurlException('Remote request failed with error ' . $error);
102
		}
103
104
		return $result;
105
	}
106
107
	/**
108
	 * @return string
109
	 */
110
	public function getError()
111
	{
112
		return curl_error($this->curlHandle);
113
	}
114
}