Completed
Push — master ( 11ade9...e98b8a )
by Josh
15:53
created

Curl   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 69
ccs 0
cts 30
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 9 1
A post() 0 14 2
A getHandle() 0 9 2
A getNewHandle() 0 10 1
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2016 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Configurator\Helpers\Http\Clients;
9
10
use s9e\TextFormatter\Configurator\Helpers\Http\Client;
11
12
class Curl extends Client
13
{
14
	/**
15
	* @var resource cURL handle, shared across instances
16
	*/
17
	protected static $handle;
18
19
	/**
20
	* {@inheritdoc}
21
	*/
22
	public function get($url, $headers = [])
23
	{
24
		$handle = $this->getHandle();
25
		curl_setopt($handle, CURLOPT_HTTPGET,    true);
26
		curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
27
		curl_setopt($handle, CURLOPT_URL,        $url);
28
29
		return curl_exec($handle);
30
	}
31
32
	/**
33
	* {@inheritdoc}
34
	*/
35
	public function post($url, $headers = [], $body = null)
36
	{
37
		$handle = $this->getHandle();
38
		if (isset($body))
39
		{
40
			curl_setopt($handle, CURLOPT_POSTFIELDS, $body);
41
			$headers[] = 'Content-Length: ' . strlen($body);
42
		}
43
		curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
44
		curl_setopt($handle, CURLOPT_POST,       true);
45
		curl_setopt($handle, CURLOPT_URL,        $url);
46
47
		return curl_exec($handle);
48
	}
49
50
	/**
51
	* Return a globally cached cURL handle
52
	*
53
	* @return resource
54
	*/
55
	protected function getHandle()
56
	{
57
		if (!isset(self::$handle))
58
		{
59
			self::$handle = $this->getNewHandle();
60
		}
61
62
		return self::$handle;
63
	}
64
65
	/**
66
	* Create and return a new cURL handle
67
	*
68
	* @return resource
69
	*/
70
	protected function getNewHandle()
71
	{
72
		$handle = curl_init();
73
		curl_setopt($handle, CURLOPT_ENCODING,       '');
74
		curl_setopt($handle, CURLOPT_FAILONERROR,    true);
75
		curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
76
		curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
77
78
		return $handle;
79
	}
80
}