Completed
Pull Request — master (#449)
by Roeland
03:38
created

CapabilitiesService::getFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
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
42 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...
43
		$this->config = $config;
44
		$this->clientService = $clientService;
45
		try {
46
			$this->appData = $appData->getFolder('richdocuments');
47
		} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
48
			$this->appData = $appData->newFolder('richdocuments');
49
		}
50
	}
51
52
	private function getFile() {
53
		try {
54
			$file = $this->appData->getFile('capabilities.json');
55
		} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
56
			$file = $this->appData->newFile('capabilities.json');
57
			$file->putContent(json_encode([]));
58
		}
59
60
		return $file;
61
	}
62
63
	public function clear() {
64
		$file = $this->getFile();
65
		$file->putContent(json_encode([]));
66
	}
67
68
	public function refretch() {
69
		$capabilties = $this->renewCapabilities();
70
71
		if ($capabilties !== []) {
72
			$file = $this->getFile();
73
			$file->putContent(json_encode($capabilties));
74
		}
75
	}
76
77
	private function renewCapabilities() {
78
		$remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url');
79
		if ($remoteHost === '') {
80
			return [];
81
		}
82
		$capabilitiesEndpoint = $remoteHost . '/hosting/capabilities';
83
84
		$client = $this->clientService->newClient();
85
		$options = ['timeout' => 10];
86
87
		if ($this->config->getAppValue('richdocuments', 'disable_certificate_verification') === 'yes') {
88
			$options['verify'] = false;
89
		}
90
91
		try {
92
			$response = $client->get($capabilitiesEndpoint, $options);
93
		} catch (\Exception $e) {
94
			return [];
95
		}
96
97
		$responseBody = $response->getBody();
98
		$ret = \json_decode($responseBody, true);
99
100
		if (!is_array($ret)) {
101
			return [];
102
		}
103
104
		return $ret;
105
	}
106
}
107