Completed
Push — master ( 719d3a...c8652a )
by C
04:58
created

Rapidgatornet   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 82
ccs 40
cts 40
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C fetchDownloadInfo() 0 39 7
A getUrlToDownload() 0 14 4
B login() 0 23 4
1
<?php
2
namespace Tartana\Host;
3
use GuzzleHttp\Psr7\Request;
4
use Tartana\Entity\Download;
5
use Tartana\Host\Common\Http;
6
7
class Rapidgatornet extends Http
8
{
9
10 4
	public function fetchDownloadInfo (array $downloads)
11
	{
12 4
		if (! $this->login())
13
		{
14 1
			foreach ($downloads as $download)
15
			{
16 1
				$download->setState(Download::STATE_DOWNLOADING_ERROR);
17 1
				$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_INVALID_LOGIN');
18
			}
19 1
			return;
20
		}
21
22 3
		$sid = $this->getCookie('PHPSESSID')->getValue();
23 3
		foreach ($downloads as $download)
24
		{
25
			try
26
			{
27
				// Getting the link information
28 3
				$res = $this->getClient()->request('get', 'https://rapidgator.net/api/file/info?sid=' . $sid . '&url=' . $download->getLink());
29 2
				$info = json_decode($res->getBody()->getContents());
30 2
				if (isset($info->response_status) && $info->response_status == 200)
31
				{
32 1
					$download->setFileName($info->response->filename);
33 1
					$download->setSize($info->response->size);
34
				}
35
				else
36
				{
37 1
					$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_INVALID_URL');
38 2
					$download->setState(Download::STATE_DOWNLOADING_ERROR);
39
				}
40
			}
41 1
			catch (\Exception $e)
42
			{
43 1
				$this->log('Exception fetching file info for connection test: ' . $e->getMessage());
44 1
				$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_INVALID_URL');
45 3
				$download->setState(Download::STATE_DOWNLOADING_ERROR);
46
			}
47
		}
48 3
	}
49
50 2
	protected function getUrlToDownload (Download $download)
51
	{
52 2
		$sid = $this->getCookie('PHPSESSID') ? $this->getCookie('PHPSESSID')->getValue() : '';
53
54 2
		$res = $this->getClient()->request('get', 'https://rapidgator.net/api/file/download?sid=' . $sid . '&url=' . $download->getLink());
55 2
		$info = json_decode($res->getBody()->getContents());
56 2
		if (isset($info->response_status) && $info->response_status == 200)
57
		{
58 1
			$this->log('Rapidgator net real url: ' . $info->response->url);
59 1
			return $info->response->url;
60
		}
61
62 1
		return null;
63
	}
64
65 6
	protected function login ()
66
	{
67 6
		if ($this->getCookie('PHPSESSID'))
68
		{
69 4
			return true;
70
		}
71
72
		$args = [
73 2
				'username' => trim($this->getConfiguration()->get('rapidgatornet.username')),
74 2
				'password' => trim($this->getConfiguration()->get('rapidgatornet.password'))
75
		];
76
77 2
		if (! $args['username'])
78
		{
79 1
			return false;
80
		}
81
82 1
		$res = $this->getClient()->request('get',
83 1
				'https://rapidgator.net/api/user/login?username=' . $args['username'] . '&password=' . $args['password']);
84 1
		$html = $res->getBody()->getContents();
85 1
		$response = json_decode($html);
86 1
		return isset($response->response_status) && $response->response_status == 200;
87
	}
88
}
89