Passed
Push — master ( 6b97f6...a6ae80 )
by Blizzz
13:11 queued 11s
created

DirectEditingService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 26
c 1
b 0
f 0
dl 0
loc 49
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDirectEditingETag() 0 2 1
A __construct() 0 3 1
A getDirectEditingCapabilitites() 0 33 3
1
<?php
2
/**
3
 * @copyright Copyright (c) 2019 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[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\Files\Service;
25
26
27
use OCP\DirectEditing\ACreateEmpty;
28
use OCP\DirectEditing\ACreateFromTemplate;
29
use OCP\DirectEditing\IEditor;
30
use OCP\DirectEditing\IManager;
31
use OCP\DirectEditing\RegisterDirectEditorEvent;
32
use OCP\EventDispatcher\IEventDispatcher;
33
34
class DirectEditingService {
35
36
	/** @var IManager */
37
	private $directEditingManager;
38
	/** @var IEventDispatcher */
39
	private $eventDispatcher;
40
41
	public function __construct(IEventDispatcher $eventDispatcher, IManager $directEditingManager) {
42
		$this->directEditingManager = $directEditingManager;
43
		$this->eventDispatcher = $eventDispatcher;
44
	}
45
46
	public function getDirectEditingETag(): string {
47
		return \md5(\json_encode($this->getDirectEditingCapabilitites()));
48
	}
49
50
	public function getDirectEditingCapabilitites(): array {
51
		$this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
52
53
		$capabilities = [
54
			'editors' => [],
55
			'creators' => []
56
		];
57
58
		/**
59
		 * @var string $id
60
		 * @var IEditor $editor
61
		 */
62
		foreach ($this->directEditingManager->getEditors() as $id => $editor) {
0 ignored issues
show
Bug introduced by
The method getEditors() does not exist on OCP\DirectEditing\IManager. Since it exists in all sub-types, consider adding an abstract or default implementation to OCP\DirectEditing\IManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
		foreach ($this->directEditingManager->/** @scrutinizer ignore-call */ getEditors() as $id => $editor) {
Loading history...
63
			$capabilities['editors'][$id] = [
64
				'name' => $editor->getName(),
65
				'mimetypes' => $editor->getMimetypes(),
66
				'optionalMimetypes' => $editor->getMimetypesOptional(),
67
				'secure' => $editor->isSecure(),
68
			];
69
			/** @var ACreateEmpty|ACreateFromTemplate $creator */
70
			foreach ($editor->getCreators() as $creator) {
71
				$id = $creator->getId();
72
				$capabilities['creators'][$id] = [
73
					'id' => $id,
74
					'editor' => $editor->getId(),
75
					'name' => $creator->getName(),
76
					'extension' => $creator->getExtension(),
77
					'templates' => $creator instanceof ACreateFromTemplate,
78
					'mimetype' => $creator->getMimetype()
79
				];
80
			}
81
		}
82
		return $capabilities;
83
	}
84
85
}
86