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

RemoteCache   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A minify() 0 17 4
A getHash() 0 4 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\JavaScript\Minifiers;
9
10
use RuntimeException;
11
use s9e\TextFormatter\Configurator\JavaScript\Minifier;
12
13
class RemoteCache extends Minifier
14
{
15
	/**
16
	* @var string
17
	*/
18
	public $url = 'http://s9e-textformatter.rhcloud.com/minifier/cache/';
19
20
	/**
21
	* {@inheritdoc}
22
	*/
23 2
	public function minify($src)
24
	{
25 2
		$url = $this->url . $this->getHash($src);
26 2
		if (extension_loaded('zlib'))
27 2
		{
28 2
			$url = 'compress.zlib://' . $url . '.gz';
29 2
		}
30
31 2
		$contextOptions = ['http' => ['ignore_errors'  => true]];
32 2
		$content = file_get_contents($url, false, stream_context_create($contextOptions));
33 2
		if (empty($http_response_header[0]) || strpos($http_response_header[0], '200') === false)
34 2
		{
35 1
			throw new RuntimeException;
36
		}
37
38 1
		return $content;
39
	}
40
41
	/**
42
	* Compute a source's hash
43
	*
44
	* @param  string $src Original source
45
	* @return string      36 bytes string
46
	*/
47 2
	protected function getHash($src)
48
	{
49 2
		return strtr(base64_encode(sha1($src, true) . md5($src, true)), '+/', '-_');
50
	}
51
}