Completed
Push — master ( f27c48...bea597 )
by Julius
09:45 queued 10s
created

DiscoveryManager::isProxyStarting()   B

Complexity

Conditions 9
Paths 30

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 16
cp 0
rs 7.7564
c 0
b 0
f 0
cc 9
nc 30
nop 1
crap 90
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Lukas Reschke <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Richdocuments\WOPI;
23
24
use OCP\Http\Client\IClientService;
25
use OCP\ICache;
26
use OCP\ICacheFactory;
27
use OCP\IConfig;
28
29
class DiscoveryManager {
30
31
	/** @var IClientService */
32
	private $clientService;
33
	/** @var ICache */
34
	private $cache;
35
	/** @var IConfig */
36
	private $config;
37
38
	/** @var string */
39
	private $discovery;
40
41
	public function __construct(IClientService $clientService,
42
								ICacheFactory $cacheFactory,
43
								IConfig $config) {
44
		$this->clientService = $clientService;
45
		$this->cache = $cacheFactory->createDistributed('richdocuments');
46
		$this->config = $config;
47
	}
48
49
	public function get() {
50
		if ($this->discovery) {
51
			return $this->discovery;
52
		}
53
54
		$this->discovery = $this->cache->get('discovery');
55
		if (!$this->discovery) {
56
			$response = $this->fetchFromRemote();
57
			$responseBody = $response->getBody();
58
			$this->discovery = $responseBody;
0 ignored issues
show
Documentation Bug introduced by
It seems like $responseBody can also be of type resource. However, the property $discovery is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
59
			$this->cache->set('discovery', $this->discovery, 3600);
60
		}
61
62
		return $this->discovery;
63
	}
64
65
	/**
66
	 * @return \OCP\Http\Client\IResponse
67
	 * @throws \Exception
68
	 */
69
	public function fetchFromRemote() {
70
		$remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url');
71
		$wopiDiscovery = $remoteHost . '/hosting/discovery';
72
73
		$client = $this->clientService->newClient();
74
		$options = ['timeout' => 45, 'nextcloud' => ['allow_local_address' => true]];
75
76
		if ($this->config->getAppValue('richdocuments', 'disable_certificate_verification') === 'yes') {
77
			$options['verify'] = false;
78
		}
79
80
		if ($this->isProxyStarting($wopiDiscovery))
81
			$options['timeout'] = 180;
82
83
		try {
84
			return $client->get($wopiDiscovery, $options);
85
		} catch (\Exception $e) {
86
			throw $e;
87
		}
88
	}
89
90
	public function refetch() {
91
		$this->cache->remove('discovery');
92
		$this->discovery = null;
93
	}
94
95
	/**
96
	 * @return boolean indicating if proxy.php is in initialize or false otherwise
97
	 */
98
	private function isProxyStarting($url) {
99
		$usesProxy = false;
0 ignored issues
show
Unused Code introduced by
$usesProxy is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
100
		$proxyPos = strrpos($url, 'proxy.php');
101
		if ($proxyPos === false)
102
			$usesProxy = false;
103
		else
104
			$usesProxy = true;
105
106
		if ($usesProxy === true) {
107
			$statusUrl = substr($url, 0, $proxyPos);
108
			$statusUrl = $statusUrl . 'proxy.php?status';
109
110
			$client = $this->clientService->newClient();
111
			$options = ['timeout' => 5, 'nextcloud' => ['allow_local_address' => true]];
112
113
			if ($this->config->getAppValue('richdocuments', 'disable_certificate_verification') === 'yes') {
114
				$options['verify'] = false;
115
			}
116
117
			try {
118
				$response = $client->get($statusUrl, $options);
119
120
				if ($response->getStatusCode() === 200) {
121
					$body = json_decode($response->getBody(), true);
122
123
					if ($body['status'] === 'starting'
124
						|| $body['status'] === 'stopped'
125
						|| $body['status'] === 'restarting') {
126
						return true;
127
					}
128
				}
129
			} catch (\Exception $e) {
130
				// ignore
131
			}
132
		}
133
134
		return false;
135
	}
136
}
137