Completed
Push — master ( 48abf8...8f7739 )
by C
13:50
created

Dlc::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 13
nc 2
nop 1
crap 2
1
<?php
2
namespace Tartana\Component\Decrypter;
3
use GuzzleHttp\Client;
4
use GuzzleHttp\ClientInterface;
5
use Monolog\Logger;
6
7
class Dlc extends BaseDecrypter
8
{
9
10
	private $client = null;
11
12 6
	public function __construct (ClientInterface $client = null)
13
	{
14 6
		if (! $client)
15
		{
16 1
			$client = new Client(
17
					[
18
							'headers' => [
19
									'Accept' => 'application/json, text/javascript, */*',
20
									'Content-Type' => 'application/x-www-form-urlencoded',
21
									'Host' => 'dcrypt.it',
22
									'Origin' => 'http://dcrypt.it',
23
									'Referer' => 'http://dcrypt.it/',
24
									'X-Requested-With' => 'XMLHttpRequest',
25
									'Connection' => 'keep-alive',
26
									'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.39 Safari/537.36'
27
							]
28 1
					]);
29
		}
30 6
		$this->client = $client;
31 6
	}
32
33 4
	public function getLinks ($content)
34
	{
35 4
		$this->log('Calling http://dcrypt.it/decrypt/paste', Logger::INFO);
36 4
		$res = $this->client->request('post', 'http://dcrypt.it/decrypt/paste', [
37
				'form_params' => [
38 4
						'content' => $content
39
				]
40
		]);
41
42 4
		$decRes = json_decode($res->getBody()->getContents());
43 4
		$this->log('Response from http://dcrypt.it/decrypt/paste was: ' . print_r($decRes, true));
44
45 4
		if (is_object($decRes) && isset($decRes->success) && is_array($decRes->success->links))
46
		{
47 2
			$links = $decRes->success->links;
48 2
			$links = array_filter($links, function  ($link) {
49 2
				return strpos($link, 'http') === 0;
50 2
			});
51
52 2
			$this->log('Found ' . count($links), Logger::INFO);
53
54 2
			return $links;
55
		}
56
		else
57
		{
58 2
			throw new \RuntimeException('Failed parsing response: ' . var_export($decRes, true));
59
		}
60
	}
61
}