Completed
Push — master ( 3dcbe0...f8e908 )
by Julius
14s
created

DiscoveryManager::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 0
cts 14
cp 0
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 5
crap 6
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Lukas Reschke <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Richdocuments\WOPI;
23
24
use OCP\AppFramework\Utility\ITimeFactory;
25
use OCP\Files\IAppData;
26
use OCP\Files\NotFoundException;
27
use OCP\Files\SimpleFS\ISimpleFolder;
28
use OCP\Http\Client\IClientService;
29
use OCP\IConfig;
30
use OCP\IL10N;
31
32
class DiscoveryManager {
33
	/** @var IClientService */
34
	private $clientService;
35
	/** @var ISimpleFolder */
36
	private $appData;
37
	/** @var IConfig */
38
	private $config;
39
	/** @var IL10N */
40
	private $l10n;
41
	/** @var ITimeFactory */
42
	private $timeFactory;
43
44
	/**
45
	 * @param IClientService $clientService
46
	 * @param IAppData $appData
47
	 * @param IConfig $config
48
	 * @param IL10N $l10n
49
	 * @param ITimeFactory $timeFactory
50
	 */
51 View Code Duplication
	public function __construct(IClientService $clientService,
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...
52
								IAppData $appData,
53
								IConfig $config,
54
								IL10N $l10n,
0 ignored issues
show
Unused Code introduced by
The parameter $l10n is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
								ITimeFactory $timeFactory) {
56
		$this->clientService = $clientService;
57
		try {
58
			$this->appData = $appData->getFolder('richdocuments');
59
		} 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...
60
			$this->appData = $appData->newFolder('richdocuments');
61
		}
62
		$this->config = $config;
63
		$this->timeFactory = $timeFactory;
64
	}
65
66
	public function get() {
67
		// First check if there is a local valid discovery file
68
		try {
69
			$file = $this->appData->getFile('discovery.xml');
70
			$decodedFile = json_decode($file->getContent(), true);
71
			if($decodedFile['timestamp'] + 3600 > $this->timeFactory->getTime()) {
72
				return $decodedFile['data'];
73
			}
74
		} 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...
75
			$file = $this->appData->newFile('discovery.xml');
76
		}
77
78
		$response = $this->fetchFromRemote();
79
80
		$responseBody = $response->getBody();
81
		$file->putContent(
82
			json_encode([
83
				'data' => $responseBody,
84
				'timestamp' => $this->timeFactory->getTime(),
85
			])
86
		);
87
		return $responseBody;
88
	}
89
90
	/**
91
	 * @return \OCP\Http\Client\IResponse
92
	 * @throws \Exception
93
	 */
94
	public function fetchFromRemote() {
95
		$remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url');
96
		$wopiDiscovery = $remoteHost . '/hosting/discovery';
97
98
		$client = $this->clientService->newClient();
99
		$options = ['timeout' => 5];
100
101
		if ($this->config->getAppValue('richdocuments', 'disable_certificate_verification') === 'yes') {
102
			$options['verify'] = false;
103
		}
104
105
		try {
106
			return $client->get($wopiDiscovery, $options);
107
		} catch (\Exception $e) {
108
			throw $e;
109
		}
110
	}
111
112
	public function refretch() {
113
		try {
114
			$this->appData->getFile('discovery.xml')->delete();
115
		} catch(\Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
116
	}
117
}
118