Completed
Push — master ( 766ef1...3564a7 )
by Maxence
02:20
created

GroupFoldersService   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 218
Duplicated Lines 17.89 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
lcom 2
cbo 4
dl 39
loc 218
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
A initGroupShares() 0 8 2
A getFileSource() 0 12 2
B updateDocumentAccess() 0 29 2
A getShareUsers() 0 8 2
A isMountFullGlobal() 15 15 4
A getGroupFolderMount() 11 11 3
A getGroupFoldersMounts() 0 18 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Files_FullTextSearch - Index the content of your files
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2018
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
28
namespace OCA\Files_FullTextSearch\Service;
29
30
31
use OCA\Files_FullTextSearch\Exceptions\FileIsNotIndexableException;
32
use OCA\Files_FullTextSearch\Exceptions\KnownFileSourceException;
33
use OCA\Files_FullTextSearch\Model\FilesDocument;
34
use OCA\Files_FullTextSearch\Model\GroupFolderMount;
35
use OCP\App;
36
use OCP\Files\IRootFolder;
37
use OCP\Files\Node;
38
use OCP\IUserManager;
39
use OCP\Share\IManager;
40
41
class GroupFoldersService {
42
43
44
	const DOCUMENT_SOURCE = 'group_folders';
45
46
	/** @var IRootFolder */
47
	private $rootFolder;
48
49
	/** @var IUserManager */
50
	private $userManager;
51
52
	/** @var IManager */
53
	private $shareManager;
54
55
	/** @var LocalFilesService */
56
	private $localFilesService;
57
58
	/** @var ConfigService */
59
	private $configService;
60
61
	/** @var MiscService */
62
	private $miscService;
63
64
65
	/** @var GroupFolderMount[] */
66
	private $groupFolders = [];
67
68
69
	/**
70
	 * ExternalFilesService constructor.
71
	 *
72
	 * @param IRootFolder $rootFolder
73
	 * @param IUserManager $userManager
74
	 * @param IManager $shareManager
75
	 * @param LocalFilesService $localFilesService
76
	 * @param ConfigService $configService
77
	 * @param MiscService $miscService
78
	 */
79 View Code Duplication
	public function __construct(
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...
80
		IRootFolder $rootFolder, IUserManager $userManager, IManager $shareManager,
81
		LocalFilesService $localFilesService, ConfigService $configService, MiscService $miscService
82
	) {
83
		$this->rootFolder = $rootFolder;
84
		$this->userManager = $userManager;
85
		$this->shareManager = $shareManager;
86
87
		$this->localFilesService = $localFilesService;
88
89
		$this->configService = $configService;
90
		$this->miscService = $miscService;
91
	}
92
93
94
	/**
95
	 *
96
	 */
97
	public function initGroupShares() {
98
		$this->groupFolders = [];
99
		if (!App::isEnabled('groupfolders')) {
100
			return;
101
		}
102
103
		$this->groupFolders = $this->getGroupFoldersMounts();
104
	}
105
106
107
	/**
108
	 * @param Node $file
109
	 *
110
	 * @param string $source
111
	 *
112
	 * @throws KnownFileSourceException
113
	 */
114
	public function getFileSource(Node $file, &$source) {
115
116
117
		try {
118
			$this->getGroupFolderMount($file);
119
		} catch (FileIsNotIndexableException $e) {
120
			return;
121
		}
122
123
		$source = self::DOCUMENT_SOURCE;
124
		throw new KnownFileSourceException();
125
	}
126
127
//
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
128
//	/**
129
//	 * @param DocumentAccess $access
130
//	 *
131
//	 * @return array
132
//	 */
133
//	public function getAllSharesFromExternalFile(DocumentAccess $access) {
134
//		$result = $access->getUsers();
135
//
136
//		if ($access->getOwnerId() !== '') {
137
//			array_push($result, $access->getOwnerId());
138
//		}
139
//
140
//		// TODO: get users from groups & circles.
141
//		return $result;
142
//	}
143
144
145
	/**
146
	 * @param FilesDocument $document
147
	 * @param Node $file
148
	 */
149
	public function updateDocumentAccess(FilesDocument &$document, Node $file) {
0 ignored issues
show
Unused Code introduced by
The parameter $file is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
150
151
		if ($document->getSource() !== self::DOCUMENT_SOURCE) {
152
			return;
153
		}
154
155
//		try {
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
156
//			$mount = $this->getGroupFolderMount($file);
157
//		} catch (FileIsNotIndexableException $e) {
158
//			return;
159
//		}
160
161
		$access = $document->getAccess();
162
		echo json_encode($access);
163
//		if ($this->isMountFullGlobal($mount)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
164
//			$access->addUsers(['__all']);
165
//		} else {
166
//			$access->addUsers($mount->getUsers());
167
//			$access->addGroups($mount->getGroups());
168
////		 	$access->addCircles($mount->getCircles());
169
//		}
170
//
171
//		// twist 'n tweak.
172
//		if (!$mount->isGlobal()) {
173
//			$access->setOwnerId($mount->getUsers()[0]);
174
//		}
175
176
		$document->setAccess($access);
177
	}
178
179
180
	/**
181
	 * @param FilesDocument $document
182
	 * @param array $users
183
	 */
184
	public function getShareUsers(FilesDocument $document, &$users) {
185
186
		if ($document->getSource() !== self::DOCUMENT_SOURCE) {
187
			return;
188
		}
189
190
		$this->localFilesService->getSharedUsersFromAccess($document->getAccess(), $users);
191
	}
192
193
194
	/**
195
	 * @param GroupFolderMount $mount
196
	 *
197
	 * @return bool
198
	 */
199 View Code Duplication
	public function isMountFullGlobal(GroupFolderMount $mount) {
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...
200
		if (sizeof($mount->getGroups()) > 0) {
201
			return false;
202
		}
203
204
		if (sizeof($mount->getUsers()) !== 1) {
205
			return false;
206
		}
207
208
		if ($mount->getUsers()[0] === 'all') {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $mount->getUsers()[0] === 'all';.
Loading history...
209
			return true;
210
		}
211
212
		return false;
213
	}
214
215
216
	/**
217
	 * @param Node $file
218
	 *
219
	 * @return GroupFolderMount
220
	 * @throws FileIsNotIndexableException
221
	 */
222 View Code Duplication
	private function getGroupFolderMount(Node $file) {
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...
223
224
		foreach ($this->groupFolders as $mount) {
225
			if (strpos($file->getPath(), $mount->getPath()) === 0) {
226
				return $mount;
227
			}
228
		}
229
230
		throw new FileIsNotIndexableException();
231
232
	}
233
234
235
	/**
236
	 * @return GroupFolderMount[]
237
	 */
238
	private function getGroupFoldersMounts() {
239
240
		$groupFolders = [];
241
242
		// TODO: deprecated - use UserGlobalStoragesService::getStorages() and UserStoragesService::getStorages()
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 107 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
243
		$mounts = [];
244
		foreach ($mounts as $mountPoint => $mount) {
245
			$groupFolder = new GroupFolderMount();
246
//			$externalMount->setId($mount['id'])
0 ignored issues
show
Unused Code Comprehensibility introduced by
76% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
247
//						  ->setPath($mountPoint)
248
//						  ->setGroups($mount['applicable']['groups'])
249
//						  ->setUsers($mount['applicable']['users'])
250
//						  ->setGlobal((!$mount['personal']));
251
			$groupFolders[] = $groupFolder;
252
		}
253
254
		return $groupFolders;
255
	}
256
257
258
}