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

DirectEditingController::info()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
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\Controller;
25
26
27
use Exception;
28
use OCA\Files\Service\DirectEditingService;
29
use OCP\AppFramework\Http;
30
use OCP\AppFramework\Http\DataResponse;
31
use OCP\AppFramework\OCSController;
32
use OCP\DirectEditing\ACreateEmpty;
33
use OCP\DirectEditing\ACreateFromTemplate;
34
use OCP\DirectEditing\IEditor;
35
use OCP\DirectEditing\IManager;
36
use OCP\DirectEditing\RegisterDirectEditorEvent;
37
use OCP\EventDispatcher\IEventDispatcher;
38
use OCP\ILogger;
39
use OCP\IRequest;
40
use OCP\IURLGenerator;
41
42
class DirectEditingController extends OCSController {
43
44
	/** @var IEventDispatcher */
45
	private $eventDispatcher;
46
47
	/** @var IManager */
48
	private $directEditingManager;
49
50
	/** @var IURLGenerator */
51
	private $urlGenerator;
52
53
	/** @var ILogger */
54
	private $logger;
55
56
	/** @var DirectEditingService */
57
	private $directEditingService;
58
59
	public function __construct($appName, IRequest $request, $corsMethods, $corsAllowedHeaders, $corsMaxAge,
60
								IEventDispatcher $eventDispatcher, IURLGenerator $urlGenerator, IManager $manager, DirectEditingService $directEditingService, ILogger $logger) {
61
		parent::__construct($appName, $request, $corsMethods, $corsAllowedHeaders, $corsMaxAge);
62
63
		$this->eventDispatcher = $eventDispatcher;
64
		$this->directEditingManager = $manager;
65
		$this->directEditingService = $directEditingService;
66
		$this->logger = $logger;
67
		$this->urlGenerator = $urlGenerator;
68
	}
69
70
	/**
71
	 * @NoAdminRequired
72
	 */
73
	public function info(): DataResponse {
74
		$response = new DataResponse($this->directEditingService->getDirectEditingCapabilitites());
75
		$response->setETag($this->directEditingService->getDirectEditingETag());
76
		return $response;
77
	}
78
79
	/**
80
	 * @NoAdminRequired
81
	 */
82
	public function create(string $path, string $editorId, string $creatorId, string $templateId = null): DataResponse {
83
		$this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
84
85
		try {
86
			$token = $this->directEditingManager->create($path, $editorId, $creatorId, $templateId);
87
			return new DataResponse([
88
				'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token])
89
			]);
90
		} catch (Exception $e) {
91
			$this->logger->logException($e, ['message' => 'Exception when creating a new file through direct editing']);
92
			return new DataResponse('Failed to create file', Http::STATUS_FORBIDDEN);
0 ignored issues
show
Bug introduced by
'Failed to create file' of type string is incompatible with the type array|object expected by parameter $data of OCP\AppFramework\Http\DataResponse::__construct(). ( Ignorable by Annotation )

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

92
			return new DataResponse(/** @scrutinizer ignore-type */ 'Failed to create file', Http::STATUS_FORBIDDEN);
Loading history...
93
		}
94
	}
95
96
	/**
97
	 * @NoAdminRequired
98
	 */
99
	public function open(int $fileId, string $editorId = null): DataResponse {
100
		$this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
101
102
		try {
103
			$token = $this->directEditingManager->open($fileId, $editorId);
0 ignored issues
show
Bug introduced by
The method open() 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

103
			/** @scrutinizer ignore-call */ 
104
   $token = $this->directEditingManager->open($fileId, $editorId);
Loading history...
104
			return new DataResponse([
105
				'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token])
106
			]);
107
		} catch (Exception $e) {
108
			$this->logger->logException($e, ['message' => 'Exception when opening a file through direct editing']);
109
			return new DataResponse('Failed to open file', Http::STATUS_FORBIDDEN);
0 ignored issues
show
Bug introduced by
'Failed to open file' of type string is incompatible with the type array|object expected by parameter $data of OCP\AppFramework\Http\DataResponse::__construct(). ( Ignorable by Annotation )

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

109
			return new DataResponse(/** @scrutinizer ignore-type */ 'Failed to open file', Http::STATUS_FORBIDDEN);
Loading history...
110
		}
111
	}
112
113
114
115
	/**
116
	 * @NoAdminRequired
117
	 */
118
	public function templates(string $editorId, string $creatorId): DataResponse {
119
		$this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
120
121
		try {
122
			return new DataResponse($this->directEditingManager->getTemplates($editorId, $creatorId));
0 ignored issues
show
Bug introduced by
The method getTemplates() 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

122
			return new DataResponse($this->directEditingManager->/** @scrutinizer ignore-call */ getTemplates($editorId, $creatorId));
Loading history...
123
		} catch (Exception $e) {
124
			$this->logger->logException($e);
125
			return new DataResponse('Failed to open file', Http::STATUS_INTERNAL_SERVER_ERROR);
0 ignored issues
show
Bug introduced by
'Failed to open file' of type string is incompatible with the type array|object expected by parameter $data of OCP\AppFramework\Http\DataResponse::__construct(). ( Ignorable by Annotation )

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

125
			return new DataResponse(/** @scrutinizer ignore-type */ 'Failed to open file', Http::STATUS_INTERNAL_SERVER_ERROR);
Loading history...
126
		}
127
	}
128
}
129