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

Native   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 4 1
A post() 0 4 1
A decompress() 0 9 3
A request() 0 19 4
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 Native extends Client
13
{
14
	/**
15
	* @var bool Whether to use gzip encoding
16
	*/
17
	public $gzipEnabled;
18
19
	/**
20
	* Constructor
21
	*
22
	* @return void
1 ignored issue
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
23
	*/
24
	public function __construct()
25
	{
26
		$this->gzipEnabled = extension_loaded('zlib');
27
	}
28
29
	/**
30
	* {@inheritdoc}
31
	*/
32
	public function get($url, $headers = [])
33
	{
34
		return $this->request('GET', $url, $headers);
35
	}
36
37
	/**
38
	* {@inheritdoc}
39
	*/
40
	public function post($url, $headers = [], $body = null)
41
	{
42
		return $this->request('POST', $url, $headers, $body);
43
	}
44
45
	/**
46
	* Decompress given page if applicable
47
	*
48
	* @param  string|bool $content Request's response body or FALSE
49
	* @return string|bool
50
	*/
51
	protected function decompress($content)
52
	{
53
		if ($this->gzipEnabled && substr($content, 0, 2) === "\x1f\x8b")
54
		{
55
			return gzdecode($content);
56
		}
57
58
		return $content;
59
	}
60
61
	/**
62
	* Execute an HTTP request
63
	*
64
	* @param  string      $method  Request method
65
	* @param  string      $url     Request URL
66
	* @param  string[]    $headers Request headers
67
	* @return string|bool          Response body or FALSE
68
	*/
69
	protected function request($method, $url, $headers, $body = null)
70
	{
71
		$contextOptions = ['http' => ['method' => $method]];
72
		if ($this->gzipEnabled)
73
		{
74
			$headers[] = 'Accept-Encoding: gzip';
75
		}
76
		if (isset($body))
77
		{
78
			$headers[] = 'Content-Length: ' . strlen($body);
79
			$contextOptions['http']['content'] = $body;
80
		}
81
		if (!empty($headers))
82
		{
83
			$contextOptions['http']['header'] = $headers;
84
		}
85
86
		return $this->decompress(file_get_contents($url, false, stream_context_create($contextOptions)));
87
	}
88
}