Completed
Push — master ( ff15e3...b2481c )
by Josh
17:08
created

Native::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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