Completed
Push — master ( eebb66...95029a )
by Julius
02:24 queued 12s
created

CapabilitiesService::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 0
cts 9
cp 0
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 6
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\Files\IAppData;
27
use OCP\Files\NotFoundException;
28
use OCP\Files\SimpleFS\ISimpleFile;
29
use OCP\Files\SimpleFS\ISimpleFolder;
30
use OCP\Http\Client\IClientService;
31
use OCP\IConfig;
32
33
class CapabilitiesService {
34
35
	/** @var IConfig */
36
	private $config;
37
	/** @var IClientService */
38
	private $clientService;
39
	/** @var ISimpleFolder */
40
	private $appData;
41
	/** @var array */
42
	private $capabilities;
43
44 View Code Duplication
	public function __construct(IConfig $config, IClientService $clientService, IAppData $appData) {
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...
45
		$this->config = $config;
46
		$this->clientService = $clientService;
47
		try {
48
			$this->appData = $appData->getFolder('richdocuments');
49
		} catch (NotFoundException $e) {
50
			$this->appData = $appData->newFolder('richdocuments');
51
		}
52
	}
53
54
55
	public function getCapabilities() {
56
		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...
57
            $isCODEInstalled = $this->getContainer()->getServer()->getAppManager()->isEnabledForUser('richdocumentscode');
0 ignored issues
show
Bug introduced by
The method getContainer() does not seem to exist on object<OCA\Richdocuments...ce\CapabilitiesService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
            $isCODEEnabled = strpos($this->config->getAppValue('richdocuments', 'wopi_url'), 'proxy.php?req=') !== false;
59
            if($isCODEInstalled && $isCODEEnabled && count($this->capabilities) === 0) {
60
                $this->refretch();
61
            }
62
			return $this->capabilities;
63
		}
64
		try {
65
			$file = $this->appData->getFile('capabilities.json');
66
			$decodedFile = \json_decode($file->getContent(), true);
67
		} catch (NotFoundException $e) {
68
			return [];
69
		}
70
71
		if (!is_array($decodedFile)) {
72
			return [];
73
		}
74
		$this->capabilities = $decodedFile;
75
76
		return $this->capabilities;
77
	}
78
79
	public function hasTemplateSaveAs() {
80
		return $this->getCapabilities()['hasTemplateSaveAs'] ?? false;
81
	}
82
83
	public function hasTemplateSource() {
84
		return $this->getCapabilities()['hasTemplateSource'] ?? false;
85
	}
86
87
	private function getFile() {
88
		try {
89
			$file = $this->appData->getFile('capabilities.json');
90
		} catch (NotFoundException $e) {
91
			$file = $this->appData->newFile('capabilities.json');
92
			$file->putContent(json_encode([]));
93
		}
94
95
		return $file;
96
	}
97
98
	public function clear() {
99
		$file = $this->getFile();
100
		$file->putContent(json_encode([]));
101
	}
102
103
	public function refretch() {
104
		$capabilties = $this->renewCapabilities();
105
106
		if ($capabilties !== []) {
107
			$file = $this->getFile();
108
			$file->putContent(json_encode($capabilties));
109
		}
110
	}
111
112
	private function renewCapabilities() {
113
		$remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url');
114
		if ($remoteHost === '') {
115
			return [];
116
		}
117
		$capabilitiesEndpoint = $remoteHost . '/hosting/capabilities';
118
119
		$client = $this->clientService->newClient();
120
		$options = ['timeout' => 45, 'nextcloud' => ['allow_local_address' => true]];
121
122
		$options['verify'] = $this->config->getAppValue('richdocuments', 'disable_certificate_verification', '') === '';
123
124
		try {
125
			$response = $client->get($capabilitiesEndpoint, $options);
126
		} catch (\Exception $e) {
127
			return [];
128
		}
129
130
		$responseBody = $response->getBody();
131
		$ret = \json_decode($responseBody, true);
132
133
		if (!is_array($ret)) {
134
			return [];
135
		}
136
137
		return $ret;
138
	}
139
140
}
141