Completed
Push — master ( fd08fc...b6e3ce )
by Josh
15:13
created

HostedMinifier::minify()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 34
ccs 22
cts 22
cp 1
rs 8.5806
cc 4
eloc 22
nc 4
nop 1
crap 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\JavaScript\Minifiers;
9
10
use RuntimeException;
11
use s9e\TextFormatter\Configurator\JavaScript\Minifier;
12
13
class HostedMinifier extends Minifier
14
{
15
	/**
16
	* @var integer Compression level used to compress the request's payload
17
	*/
18
	public $gzLevel = 5;
19
20
	/**
21
	* @var integer
22
	*/
23
	public $timeout = 20;
24
25
	/**
26
	* @var string
27
	*/
28
	public $url = 'http://s9e-textformatter.rhcloud.com/minifier/minify.php';
29
30
	/**
31
	* {@inheritdoc}
32
	*/
33 2
	public function minify($src)
34
	{
35 2
		$content     = $src;
36 2
		$gzipHeaders = '';
37 2
		$url         = $this->url;
38 2
		if (extension_loaded('zlib'))
39 2
		{
40 2
			$content     = gzencode($content, $this->gzLevel);
41 2
			$gzipHeaders = "\r\nContent-encoding: gzip\r\nAccept-Encoding: gzip";
42 2
			$url         = 'compress.zlib://' . $url;
43 2
		}
44
45
		$contextOptions = [
46
			'http' => [
47 2
				'method'        => 'POST',
48
				'header'        => "Connection: close\r\n"
49
				                 . "Content-type: application/octet-stream\r\n"
50 2
				                 . 'Content-length: ' . strlen($content)
51 2
				                 . $gzipHeaders,
52 2
				'content'       => $content,
53 2
				'timeout'       => $this->timeout,
54
				'ignore_errors' => true
55 2
			]
56 2
		];
57
58 2
		$content = file_get_contents($url, false, stream_context_create($contextOptions));
59
60 2
		if (empty($http_response_header[0]) || strpos($http_response_header[0], '200') === false)
61 2
		{
62 1
			throw new RuntimeException($content);
63
		}
64
65 1
		return $content;
66
	}
67
}