Completed
Push — master ( ad704e...2109ec )
by Roeland
18s
created

Capabilities::getCollaboraCapabilities()   A

Complexity

Conditions 4
Paths 11

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 27
cp 0
rs 9.456
c 0
b 0
f 0
cc 4
nc 11
nop 0
crap 20
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018, 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;
25
26
use OCP\AppFramework\Utility\ITimeFactory;
27
use OCP\Capabilities\ICapability;
28
use OCP\Files\IAppData;
29
use OCP\Files\NotFoundException;
30
use OCP\Files\SimpleFS\ISimpleFolder;
31
use OCP\Http\Client\IClientService;
32
use OCP\IConfig;
33
use OCP\IURLGenerator;
34
35
class Capabilities implements ICapability {
36
37
	/** @var IConfig */
38
	private $config;
39
	/** @var IClientService */
40
	private $clientService;
41
	/** @var ITimeFactory */
42
	private $timeFactory;
43
	/** @var ISimpleFolder */
44
	private $appData;
45
46
	/**
47
	 * Capabilities constructor.
48
	 *
49
	 * @param IConfig $config
50
	 * @param IClientService $clientService
51
	 * @param IAppData $appData
52
	 * @param ITimeFactory $timeFactory
53
	 * @throws \OCP\Files\NotPermittedException
54
	 */
55 View Code Duplication
	public function __construct(IConfig $config, IClientService $clientService, IAppData $appData, ITimeFactory $timeFactory) {
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...
56
		$this->config = $config;
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->timeFactory = $timeFactory;
64
	}
65
66
	public function getCapabilities() {
67
		$collaboraCapabilities = $this->getCollaboraCapabilities();
68
		return [
69
			'richdocuments' => [
70
				'mimetypes' => [
71
					'application/vnd.oasis.opendocument.text',
72
					'application/vnd.oasis.opendocument.spreadsheet',
73
					'application/vnd.oasis.opendocument.presentation',
74
					'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
75
					'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
76
					'application/vnd.openxmlformats-officedocument.presentationml.presentation',
77
				],
78
				'collabora' => $collaboraCapabilities,
79
				'direct_editing' => false, //TODO: fix once proper capability is available
80
				'templates' => false, //TODO: fix once proper capability is available
81
			],
82
		];
83
	}
84
85
	/**
86
	 * @return array
87
	 * @throws \OCP\Files\NotPermittedException
88
	 */
89
	private function getCollaboraCapabilities() {
90
		try {
91
			$file = $this->appData->getFile('capabilities.json');
92
			$decodedFile = \json_decode($file->getContent(), true);
93
			if($decodedFile['timestamp'] + 3600 > $this->timeFactory->getTime()) {
94
				return \json_decode($decodedFile['data'], true);
95
			}
96
		} 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...
97
			$file = $this->appData->newFile('capabilities.json');
98
		}
99
		$remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url');
100
		$capabilitiesEndpoint = $remoteHost . '/hosting/capabilities';
101
102
		$client = $this->clientService->newClient();
103
		try {
104
			$response = $client->get($capabilitiesEndpoint);
105
		} catch (\Exception $e) {
106
			return [];
107
		}
108
109
		$responseBody = $response->getBody();
110
		$file->putContent(
111
			\json_encode([
112
				'data' => $responseBody,
113
				'timestamp' => $this->timeFactory->getTime(),
114
			])
115
		);
116
		return \json_decode($responseBody, true);
117
	}
118
}
119