Completed
Push — master ( addcdd...ca9298 )
by Lukas
02:21
created

DiscoveryManager::refretch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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
use OCP\Notification\IApp;
32
33
class DiscoveryManager {
34
	/** @var IClientService */
35
	private $clientService;
36
	/** @var ISimpleFolder */
37
	private $appData;
38
	/** @var IConfig */
39
	private $config;
40
	/** @var IL10N */
41
	private $l10n;
42
	/** @var ITimeFactory */
43
	private $timeFactory;
44
45
	/**
46
	 * @param IClientService $clientService
47
	 * @param IAppData $appData
48
	 * @param IConfig $config
49
	 * @param IL10N $l10n
50
	 * @param ITimeFactory $timeFactory
51
	 */
52
	public function __construct(IClientService $clientService,
53
								IAppData $appData,
54
								IConfig $config,
55
								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...
56
								ITimeFactory $timeFactory) {
57
		$this->clientService = $clientService;
58
		try {
59
			$this->appData = $appData->getFolder('richdocuments');
60
		} 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...
61
			$this->appData = $appData->newFolder('richdocuments');
62
		}
63
		$this->config = $config;
64
		$this->timeFactory = $timeFactory;
65
	}
66
67
	public function get() {
68
		// First check if there is a local valid discovery file
69
		try {
70
			$file = $this->appData->getFile('discovery.xml');
71
			$decodedFile = json_decode($file->getContent(), true);
72
			if($decodedFile['timestamp'] + 3600 > $this->timeFactory->getTime()) {
73
				return $decodedFile['data'];
74
			}
75
		} 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...
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
76
		}
77
78
		$remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url');
79
		$wopiDiscovery = $remoteHost . '/hosting/discovery';
80
81
		$client = $this->clientService->newClient();
82
		try {
83
			$response = $client->get($wopiDiscovery);
84
		} catch (\Exception $e) {
85
			throw $e;
86
		}
87
88
		$responseBody = $response->getBody();
89
		$file->putContent(
90
			json_encode([
91
				'data' => $responseBody,
92
				'timestamp' => $this->timeFactory->getTime(),
93
			])
94
		);
95
		return $responseBody;
96
	}
97
98
	public function refretch() {
99
		$this->appData->getFile('discovery.xml')->delete();
100
	}
101
}
102