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

HttpHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 102
Duplicated Lines 7.84 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 62.16%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 8
loc 102
ccs 23
cts 37
cp 0.6216
rs 10
c 5
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A __destruct() 0 4 1
B get() 4 23 4
A post() 4 19 2
A getError() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}