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

CapabilitiesService   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 12.16 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 9
loc 74
ccs 0
cts 52
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 2
A getFile() 0 10 2
A clear() 0 4 1
A refretch() 0 8 2
A renewCapabilities() 0 29 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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