Passed
Push — master ( 80e12c...127af0 )
by Julius
15:15 queued 16s
created

DeletedShareAPIController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 9
dl 0
loc 18
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2016 Roeland Jago Douma <[email protected]>
7
 *
8
 * @author Christoph Wurst <[email protected]>
9
 * @author Daniel Calviño Sánchez <[email protected]>
10
 * @author Joas Schilling <[email protected]>
11
 * @author John Molakvoæ <[email protected]>
12
 * @author Julius Härtl <[email protected]>
13
 * @author Roeland Jago Douma <[email protected]>
14
 *
15
 * @license GNU AGPL version 3 or any later version
16
 *
17
 * This program is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License as
19
 * published by the Free Software Foundation, either version 3 of the
20
 * License, or (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
 * GNU Affero General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Affero General Public License
28
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29
 *
30
 */
31
namespace OCA\Files_Sharing\Controller;
32
33
use OCP\App\IAppManager;
34
use OCP\AppFramework\Http\DataResponse;
35
use OCP\AppFramework\OCS\OCSException;
36
use OCP\AppFramework\OCS\OCSNotFoundException;
37
use OCP\AppFramework\OCSController;
38
use OCP\AppFramework\QueryException;
39
use OCP\Files\IRootFolder;
40
use OCP\Files\NotFoundException;
41
use OCP\IGroupManager;
42
use OCP\IRequest;
43
use OCP\IServerContainer;
44
use OCP\IUserManager;
45
use OCP\Share\Exceptions\GenericShareException;
46
use OCP\Share\Exceptions\ShareNotFound;
47
use OCP\Share\IManager as ShareManager;
48
use OCP\Share\IShare;
49
50
class DeletedShareAPIController extends OCSController {
51
52
	/** @var ShareManager */
53
	private $shareManager;
54
55
	/** @var string */
56
	private $userId;
57
58
	/** @var IUserManager */
59
	private $userManager;
60
61
	/** @var IGroupManager */
62
	private $groupManager;
63
64
	/** @var IRootFolder */
65
	private $rootFolder;
66
67
	/** @var IAppManager */
68
	private $appManager;
69
70
	/** @var IServerContainer */
71
	private $serverContainer;
72
73
	public function __construct(string $appName,
74
								IRequest $request,
75
								ShareManager $shareManager,
76
								string $UserId,
77
								IUserManager $userManager,
78
								IGroupManager $groupManager,
79
								IRootFolder $rootFolder,
80
								IAppManager $appManager,
81
								IServerContainer $serverContainer) {
82
		parent::__construct($appName, $request);
83
84
		$this->shareManager = $shareManager;
85
		$this->userId = $UserId;
86
		$this->userManager = $userManager;
87
		$this->groupManager = $groupManager;
88
		$this->rootFolder = $rootFolder;
89
		$this->appManager = $appManager;
90
		$this->serverContainer = $serverContainer;
91
	}
92
93
	/**
94
	 * @suppress PhanUndeclaredClassMethod
95
	 */
96
	private function formatShare(IShare $share): array {
97
		$result = [
98
			'id' => $share->getFullId(),
99
			'share_type' => $share->getShareType(),
100
			'uid_owner' => $share->getSharedBy(),
101
			'displayname_owner' => $this->userManager->get($share->getSharedBy())->getDisplayName(),
102
			'permissions' => 0,
103
			'stime' => $share->getShareTime()->getTimestamp(),
104
			'parent' => null,
105
			'expiration' => null,
106
			'token' => null,
107
			'uid_file_owner' => $share->getShareOwner(),
108
			'displayname_file_owner' => $this->userManager->get($share->getShareOwner())->getDisplayName(),
109
			'path' => $share->getTarget(),
110
		];
111
		$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
112
		$nodes = $userFolder->getById($share->getNodeId());
113
		if (empty($nodes)) {
114
			// fallback to guessing the path
115
			$node = $userFolder->get($share->getTarget());
116
			if ($node === null || $share->getTarget() === '') {
117
				throw new NotFoundException();
118
			}
119
		} else {
120
			$node = $nodes[0];
121
		}
122
123
		$result['path'] = $userFolder->getRelativePath($node->getPath());
124
		if ($node instanceof \OCP\Files\Folder) {
125
			$result['item_type'] = 'folder';
126
		} else {
127
			$result['item_type'] = 'file';
128
		}
129
		$result['mimetype'] = $node->getMimetype();
130
		$result['storage_id'] = $node->getStorage()->getId();
131
		$result['storage'] = $node->getStorage()->getCache()->getNumericStorageId();
132
		$result['item_source'] = $node->getId();
133
		$result['file_source'] = $node->getId();
134
		$result['file_parent'] = $node->getParent()->getId();
135
		$result['file_target'] = $share->getTarget();
136
137
		$expiration = $share->getExpirationDate();
138
		if ($expiration !== null) {
139
			$result['expiration'] = $expiration->format('Y-m-d 00:00:00');
140
		}
141
142
		if ($share->getShareType() === IShare::TYPE_GROUP) {
143
			$group = $this->groupManager->get($share->getSharedWith());
144
			$result['share_with'] = $share->getSharedWith();
145
			$result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith();
146
		} elseif ($share->getShareType() === IShare::TYPE_ROOM) {
147
			$result['share_with'] = $share->getSharedWith();
148
			$result['share_with_displayname'] = '';
149
150
			try {
151
				$result = array_merge($result, $this->getRoomShareHelper()->formatShare($share));
152
			} catch (QueryException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
153
			}
154
		} elseif ($share->getShareType() === IShare::TYPE_DECK) {
155
			$result['share_with'] = $share->getSharedWith();
156
			$result['share_with_displayname'] = '';
157
158
			try {
159
				$result = array_merge($result, $this->getDeckShareHelper()->formatShare($share));
160
			} catch (QueryException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
161
			}
162
		} elseif ($share->getShareType() === IShare::TYPE_SCIENCEMESH) {
163
			$result['share_with'] = $share->getSharedWith();
164
			$result['share_with_displayname'] = '';
165
166
			try {
167
				$result = array_merge($result, $this->getSciencemeshShareHelper()->formatShare($share));
168
			} catch (QueryException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
169
			}
170
		}
171
172
		return $result;
173
	}
174
175
	/**
176
	 * @NoAdminRequired
177
	 */
178
	public function index(): DataResponse {
179
		$groupShares = $this->shareManager->getDeletedSharedWith($this->userId, IShare::TYPE_GROUP, null, -1, 0);
180
		$roomShares = $this->shareManager->getDeletedSharedWith($this->userId, IShare::TYPE_ROOM, null, -1, 0);
181
		$deckShares = $this->shareManager->getDeletedSharedWith($this->userId, IShare::TYPE_DECK, null, -1, 0);
182
		$sciencemeshShares = $this->shareManager->getDeletedSharedWith($this->userId, IShare::TYPE_SCIENCEMESH, null, -1, 0);
183
184
		$shares = array_merge($groupShares, $roomShares, $deckShares, $sciencemeshShares);
185
186
		$shares = array_map(function (IShare $share) {
187
			return $this->formatShare($share);
188
		}, $shares);
189
190
		return new DataResponse($shares);
191
	}
192
193
	/**
194
	 * @NoAdminRequired
195
	 *
196
	 * @throws OCSException
197
	 */
198
	public function undelete(string $id): DataResponse {
199
		try {
200
			$share = $this->shareManager->getShareById($id, $this->userId);
201
		} catch (ShareNotFound $e) {
202
			throw new OCSNotFoundException('Share not found');
203
		}
204
205
		if ($share->getPermissions() !== 0) {
206
			throw new OCSNotFoundException('No deleted share found');
207
		}
208
209
		try {
210
			$this->shareManager->restoreShare($share, $this->userId);
211
		} catch (GenericShareException $e) {
212
			throw new OCSException('Something went wrong');
213
		}
214
215
		return new DataResponse([]);
216
	}
217
218
	/**
219
	 * Returns the helper of DeletedShareAPIController for room shares.
220
	 *
221
	 * If the Talk application is not enabled or the helper is not available
222
	 * a QueryException is thrown instead.
223
	 *
224
	 * @return \OCA\Talk\Share\Helper\DeletedShareAPIController
0 ignored issues
show
Bug introduced by
The type OCA\Talk\Share\Helper\DeletedShareAPIController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
225
	 * @throws QueryException
226
	 */
227
	private function getRoomShareHelper() {
228
		if (!$this->appManager->isEnabledForUser('spreed')) {
229
			throw new QueryException();
0 ignored issues
show
Deprecated Code introduced by
The class OCP\AppFramework\QueryException has been deprecated: 20.0.0 catch \Psr\Container\ContainerExceptionInterface ( Ignorable by Annotation )

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

229
			throw /** @scrutinizer ignore-deprecated */ new QueryException();
Loading history...
230
		}
231
232
		return $this->serverContainer->get('\OCA\Talk\Share\Helper\DeletedShareAPIController');
233
	}
234
235
	/**
236
	 * Returns the helper of DeletedShareAPIHelper for deck shares.
237
	 *
238
	 * If the Deck application is not enabled or the helper is not available
239
	 * a QueryException is thrown instead.
240
	 *
241
	 * @return \OCA\Deck\Sharing\ShareAPIHelper
0 ignored issues
show
Bug introduced by
The type OCA\Deck\Sharing\ShareAPIHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
242
	 * @throws QueryException
243
	 */
244
	private function getDeckShareHelper() {
245
		if (!$this->appManager->isEnabledForUser('deck')) {
246
			throw new QueryException();
0 ignored issues
show
Deprecated Code introduced by
The class OCP\AppFramework\QueryException has been deprecated: 20.0.0 catch \Psr\Container\ContainerExceptionInterface ( Ignorable by Annotation )

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

246
			throw /** @scrutinizer ignore-deprecated */ new QueryException();
Loading history...
247
		}
248
249
		return $this->serverContainer->get('\OCA\Deck\Sharing\ShareAPIHelper');
250
	}
251
252
	/**
253
	 * Returns the helper of DeletedShareAPIHelper for sciencemesh shares.
254
	 *
255
	 * If the sciencemesh application is not enabled or the helper is not available
256
	 * a QueryException is thrown instead.
257
	 *
258
	 * @return \OCA\Deck\Sharing\ShareAPIHelper
259
	 * @throws QueryException
260
	 */
261
	private function getSciencemeshShareHelper() {
262
		if (!$this->appManager->isEnabledForUser('sciencemesh')) {
263
			throw new QueryException();
0 ignored issues
show
Deprecated Code introduced by
The class OCP\AppFramework\QueryException has been deprecated: 20.0.0 catch \Psr\Container\ContainerExceptionInterface ( Ignorable by Annotation )

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

263
			throw /** @scrutinizer ignore-deprecated */ new QueryException();
Loading history...
264
		}
265
266
		return $this->serverContainer->get('\OCA\ScienceMesh\Sharing\ShareAPIHelper');
267
	}
268
}
269