Completed
Push — master ( 3c3a4e...2d48a9 )
by Julius
02:29 queued 11s
created

CapabilitiesService::hasTemplateSource()   A

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