Completed
Push — master ( 86d952...cbfcfb )
by Morris
24:23
created

DeletedShareAPIController::formatShare()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 54

Duplication

Lines 9
Ratio 16.67 %

Importance

Changes 0
Metric Value
cc 7
nc 17
nop 1
dl 9
loc 54
rs 8.0703
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
/**
4
 * @Copyright 2018, Roeland Jago Douma <[email protected]>
5
 * @Copyright 2018, John Molakvoæ (skjnldsv) <[email protected]>
6
 *
7
 * @author John Molakvoæ (skjnldsv) <[email protected]>
8
 * @author Roeland Jago Douma <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Files_Sharing\Controller;
28
29
use OCP\AppFramework\Http\DataResponse;
30
use OCP\AppFramework\OCS\OCSException;
31
use OCP\AppFramework\OCS\OCSNotFoundException;
32
use OCP\AppFramework\OCSController;
33
use OCP\Files\IRootFolder;
34
use OCP\Files\NotFoundException;
35
use OCP\IGroupManager;
36
use OCP\IRequest;
37
use OCP\IUserManager;
38
use OCP\Share\Exceptions\GenericShareException;
39
use OCP\Share\Exceptions\ShareNotFound;
40
use OCP\Share\IManager as ShareManager;
41
use OCP\Share\IShare;
42
43
class DeletedShareAPIController extends OCSController {
44
45
	/** @var ShareManager */
46
	private $shareManager;
47
48
	/** @var string */
49
	private $userId;
50
51
	/** @var IUserManager */
52
	private $userManager;
53
54
	/** @var IGroupManager */
55
	private $groupManager;
56
57
	/** @var IRootFolder */
58
	private $rootFolder;
59
60
	public function __construct(string $appName,
61
								IRequest $request,
62
								ShareManager $shareManager,
63
								string $UserId,
64
								IUserManager $userManager,
65
								IGroupManager $groupManager,
66
								IRootFolder $rootFolder) {
67
		parent::__construct($appName, $request);
68
69
		$this->shareManager = $shareManager;
70
		$this->userId = $UserId;
71
		$this->userManager = $userManager;
72
		$this->groupManager = $groupManager;
73
		$this->rootFolder = $rootFolder;
74
	}
75
76
	private function formatShare(IShare $share): array {
77
78
		$result = [
79
			'id' => $share->getFullId(),
80
			'share_type' => $share->getShareType(),
81
			'uid_owner' => $share->getSharedBy(),
82
			'displayname_owner' => $this->userManager->get($share->getSharedBy())->getDisplayName(),
83
			'permissions' => 0,
84
			'stime' => $share->getShareTime()->getTimestamp(),
85
			'parent' => null,
86
			'expiration' => null,
87
			'token' => null,
88
			'uid_file_owner' => $share->getShareOwner(),
89
			'displayname_file_owner' => $this->userManager->get($share->getShareOwner())->getDisplayName(),
90
			'path' => $share->getTarget(),
91
		];
92
		$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
93
		$nodes = $userFolder->getById($share->getNodeId());
94 View Code Duplication
		if (empty($nodes)) {
95
			// fallback to guessing the path
96
			$node = $userFolder->get($share->getTarget());
97
			if ($node === null || $share->getTarget() === '') {
98
				throw new NotFoundException();
99
			}
100
		} else {
101
			$node = $nodes[0];
102
		}
103
104
		$result['path'] = $userFolder->getRelativePath($node->getPath());
105
		if ($node instanceOf \OCP\Files\Folder) {
106
			$result['item_type'] = 'folder';
107
		} else {
108
			$result['item_type'] = 'file';
109
		}
110
		$result['mimetype'] = $node->getMimetype();
111
		$result['storage_id'] = $node->getStorage()->getId();
112
		$result['storage'] = $node->getStorage()->getCache()->getNumericStorageId();
113
		$result['item_source'] = $node->getId();
114
		$result['file_source'] = $node->getId();
115
		$result['file_parent'] = $node->getParent()->getId();
116
		$result['file_target'] = $share->getTarget();
117
118
		$expiration = $share->getExpirationDate();
119
		if ($expiration !== null) {
120
			$result['expiration'] = $expiration->format('Y-m-d 00:00:00');
121
		}
122
123
		$group = $this->groupManager->get($share->getSharedWith());
124
		$result['share_with'] = $share->getSharedWith();
125
		$result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith();
126
127
		return $result;
128
129
	}
130
131
	/**
132
	 * @NoAdminRequired
133
	 */
134
	public function index(): DataResponse {
135
		$shares = $this->shareManager->getDeletedSharedWith($this->userId, \OCP\Share::SHARE_TYPE_GROUP, null, -1, 0);
136
137
		$shares = array_map(function (IShare $share) {
138
			return $this->formatShare($share);
139
		}, $shares);
140
141
		return new DataResponse($shares);
142
	}
143
144
	/**
145
	 * @NoAdminRequired
146
	 *
147
	 * @throws OCSException
148
	 */
149
	public function undelete(string $id): DataResponse {
150
		try {
151
			$share = $this->shareManager->getShareById($id, $this->userId);
152
		} catch (ShareNotFound $e) {
153
			throw new OCSNotFoundException('Share not found');
154
		}
155
156
		if ($share->getPermissions() !== 0) {
157
			throw new OCSNotFoundException('No deleted share found');
158
		}
159
160
		try {
161
			$this->shareManager->restoreShare($share, $this->userId);
162
		} catch (GenericShareException $e) {
163
			throw new OCSException('Something went wrong');
164
		}
165
166
		return new DataResponse([]);
167
	}
168
}
169