CapabilitiesService::clear()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2019, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Richdocuments\Service;
25
26
use OCP\App\IAppManager;
27
use OCP\Http\Client\IClientService;
28
use OCP\ICache;
29
use OCP\ICacheFactory;
30
use OCP\IConfig;
31
32
class CapabilitiesService {
33
34
	/** @var IConfig */
35
	private $config;
36
	/** @var IClientService */
37
	private $clientService;
38
	/** @var ICache */
39
	private $cache;
40
	/** @var IAppManager */
41
	private $appManager;
42
43
	/** @var array */
44
	private $capabilities;
45
46
47 View Code Duplication
	public function __construct(IConfig $config, IClientService $clientService, ICacheFactory $cacheFactory, IAppManager $appManager) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
		$this->config = $config;
49
		$this->clientService = $clientService;
50
		$this->cache = $cacheFactory->createDistributed('richdocuments');
51
		$this->appManager = $appManager;
52
	}
53
54
	public function getCapabilities() {
55
		if (!$this->capabilities) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->capabilities of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
56
			$this->capabilities = $this->cache->get('capabilities');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->cache->get('capabilities') of type * is incompatible with the declared type array of property $capabilities.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
		}
58
59
		$isARM64 = php_uname('m') === 'aarch64';
60
		$CODEAppID = $isARM64 ? 'richdocumentscode_arm64' : 'richdocumentscode';
61
		$isCODEInstalled = $this->appManager->isEnabledForUser($CODEAppID);
62
		$isCODEEnabled = strpos($this->config->getAppValue('richdocuments', 'wopi_url'), 'proxy.php?req=') !== false;
63
		$shouldRecheckCODECapabilities = $isCODEInstalled && $isCODEEnabled && ($this->capabilities === null || count($this->capabilities) === 0);
64
		if($this->capabilities === null || $shouldRecheckCODECapabilities) {
65
			$this->refetch();
66
		}
67
68
		if (!is_array($this->capabilities)) {
69
			return [];
70
		}
71
72
		return $this->capabilities;
73
	}
74
75
	public function hasTemplateSaveAs() {
76
		return $this->getCapabilities()['hasTemplateSaveAs'] ?? false;
77
	}
78
79
	public function hasTemplateSource() {
80
		return $this->getCapabilities()['hasTemplateSource'] ?? false;
81
	}
82
83
	public function clear() {
84
		$this->cache->remove('capabilities');
85
	}
86
87
	public function refetch() {
88
		$remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url');
89
		if ($remoteHost === '') {
90
			return [];
91
		}
92
		$capabilitiesEndpoint = $remoteHost . '/hosting/capabilities';
93
94
		$client = $this->clientService->newClient();
95
		$options = ['timeout' => 45, 'nextcloud' => ['allow_local_address' => true]];
96
97
		if ($this->config->getAppValue('richdocuments', 'disable_certificate_verification') === 'yes') {
98
			$options['verify'] = false;
99
		}
100
101
		try {
102
			$response = $client->get($capabilitiesEndpoint, $options);
103
			$responseBody = $response->getBody();
104
			$capabilities = \json_decode($responseBody, true);
105
106
			if (!is_array($capabilities)) {
107
				$capabilities = [];
108
			}
109
110
111
		} catch (\Exception $e) {
112
			$capabilities = [];
113
		}
114
115
		$this->capabilities = $capabilities;
116
		$this->cache->set('capabilities', $capabilities, 3600);
117
	}
118
}
119