Completed
Push — master ( 5feeae...0d739f )
by Josh
18:19 queued 12:34
created

Native::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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\Utils\Http\Clients;
9
10
use s9e\TextFormatter\Utils\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 7
	public function __construct()
23
	{
24 7
		$this->gzipEnabled = extension_loaded('zlib');
25 7
	}
26
27
	/**
28
	* {@inheritdoc}
29
	*/
30 4
	public function get($url, $headers = [])
31
	{
32 4
		return $this->request('GET', $url, $headers);
33
	}
34
35
	/**
36
	* {@inheritdoc}
37
	*/
38 3
	public function post($url, $headers = [], $body = '')
39
	{
40 3
		return $this->request('POST', $url, $headers, $body);
41
	}
42
43
	/**
44
	* Create a stream context for given request
45
	*
46
	* @param  string   $method  Request method
47
	* @param  string[] $headers Request headers
48
	* @param  string   $body    Request body
49
	* @return resource
50
	*/
51 7
	protected function createContext($method, array $headers, $body)
52
	{
53
		$contextOptions = [
54 7
			'ssl'  => ['verify_peer' => $this->sslVerifyPeer],
55
			'http' => [
56 7
				'method'  => $method,
57 7
				'timeout' => $this->timeout,
58 7
				'header'  => $this->generateHeaders($headers, $body),
59
				'content' => $body
60 7
			]
61 7
		];
62
63 7
		return stream_context_create($contextOptions);
64
	}
65
66
	/**
67
	* Decompress given page if applicable
68
	*
69
	* @param  string $content Response body, potentially compressed
70
	* @return string          Response body, uncompressed
71
	*/
72 6
	protected function decompress($content)
73
	{
74 6
		if ($this->gzipEnabled && substr($content, 0, 2) === "\x1f\x8b")
75 6
		{
76 5
			return gzdecode($content);
77
		}
78
79 1
		return $content;
80
	}
81
82
	/**
83
	* Generate a list of headers for given request
84
	*
85
	* @param  string[] $headers Request headers
86
	* @param  string   $body    Request body
87
	* @return string[]
88
	*/
89 7
	protected function generateHeaders(array $headers, $body)
90
	{
91 7
		if ($this->gzipEnabled)
92 7
		{
93 6
			$headers[] = 'Accept-Encoding: gzip';
94 6
		}
95 7
		$headers[] = 'Content-Length: ' . strlen($body);
96
97 7
		return $headers;
98
	}
99
100
	/**
101
	* Execute an HTTP request
102
	*
103
	* @param  string      $method  Request method
104
	* @param  string      $url     Request URL
105
	* @param  string[]    $headers Request headers
106
	* @return string|bool          Response body or FALSE
107
	*/
108 7
	protected function request($method, $url, $headers, $body = '')
109
	{
110 7
		$response = @file_get_contents($url, false, $this->createContext($method, $headers, $body));
111
112 7
		return (is_string($response)) ? $this->decompress($response) : $response;
113
	}
114
}