Completed
Push — master ( 0c68cd...766ef1 )
by Maxence
02:02
created

GroupFoldersService   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 199
Duplicated Lines 18.59 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 3
dl 37
loc 199
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
A initGroupShares() 0 8 2
A getFileSource() 0 12 2
B updateDocumentAccess() 0 29 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 OCA\Files_FullTextSearch\Model\GroupSharesMount;
36
use OCA\FullTextSearch\Model\DocumentAccess;
37
use OCP\App;
38
use OCP\Files\IRootFolder;
39
use OCP\Files\Node;
40
use OCP\IUserManager;
41
use OCP\Share\IManager;
42
43
class GroupFoldersService {
44
45
46
	const DOCUMENT_SOURCE = 'group_folders';
47
48
	/** @var IRootFolder */
49
	private $rootFolder;
50
51
	/** @var IUserManager */
52
	private $userManager;
53
54
	/** @var IManager */
55
	private $shareManager;
56
57
	/** @var ConfigService */
58
	private $configService;
59
60
	/** @var MiscService */
61
	private $miscService;
62
63
64
	/** @var GroupFolderMount[] */
65
	private $groupFolders = [];
66
67
68
	/**
69
	 * ExternalFilesService constructor.
70
	 *
71
	 * @param IRootFolder $rootFolder
72
	 * @param IUserManager $userManager
73
	 * @param IManager $shareManager
74
	 * @param ConfigService $configService
75
	 * @param MiscService $miscService
76
	 */
77 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...
78
		IRootFolder $rootFolder, IUserManager $userManager, IManager $shareManager,
79
		ConfigService $configService, MiscService $miscService
80
	) {
81
		$this->rootFolder = $rootFolder;
82
		$this->userManager = $userManager;
83
		$this->shareManager = $shareManager;
84
85
		$this->configService = $configService;
86
		$this->miscService = $miscService;
87
	}
88
89
90
	/**
91
	 *
92
	 */
93
	public function initGroupShares() {
94
		$this->groupFolders = [];
95
		if (!App::isEnabled('groupfolders')) {
96
			return;
97
		}
98
99
		$this->groupFolders = $this->getGroupFoldersMounts();
100
	}
101
102
103
	/**
104
	 * @param Node $file
105
	 *
106
	 * @param string $source
107
	 *
108
	 * @throws KnownFileSourceException
109
	 */
110
	public function getFileSource(Node $file, &$source) {
111
112
113
		try {
114
			$this->getGroupFolderMount($file);
115
		} catch (FileIsNotIndexableException $e) {
116
			return;
117
		}
118
119
		$source = self::DOCUMENT_SOURCE;
120
		throw new KnownFileSourceException();
121
	}
122
123
//
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...
124
//	/**
125
//	 * @param DocumentAccess $access
126
//	 *
127
//	 * @return array
128
//	 */
129
//	public function getAllSharesFromExternalFile(DocumentAccess $access) {
130
//		$result = $access->getUsers();
131
//
132
//		if ($access->getOwnerId() !== '') {
133
//			array_push($result, $access->getOwnerId());
134
//		}
135
//
136
//		// TODO: get users from groups & circles.
137
//		return $result;
138
//	}
139
140
141
	/**
142
	 * @param FilesDocument $document
143
	 * @param Node $file
144
	 */
145
	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...
146
147
		if ($document->getSource() !== self::DOCUMENT_SOURCE) {
148
			return;
149
		}
150
151
//		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...
152
//			$mount = $this->getGroupFolderMount($file);
153
//		} catch (FileIsNotIndexableException $e) {
154
//			return;
155
//		}
156
157
		$access = $document->getAccess();
158
echo json_encode($access);
159
//		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...
160
//			$access->addUsers(['__all']);
161
//		} else {
162
//			$access->addUsers($mount->getUsers());
163
//			$access->addGroups($mount->getGroups());
164
////		 	$access->addCircles($mount->getCircles());
165
//		}
166
//
167
//		// twist 'n tweak.
168
//		if (!$mount->isGlobal()) {
169
//			$access->setOwnerId($mount->getUsers()[0]);
170
//		}
171
172
		$document->setAccess($access);
173
	}
174
175
176
	/**
177
	 * @param GroupFolderMount $mount
178
	 *
179
	 * @return bool
180
	 */
181 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...
182
		if (sizeof($mount->getGroups()) > 0) {
183
			return false;
184
		}
185
186
		if (sizeof($mount->getUsers()) !== 1) {
187
			return false;
188
		}
189
190
		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...
191
			return true;
192
		}
193
194
		return false;
195
	}
196
197
198
	/**
199
	 * @param Node $file
200
	 *
201
	 * @return GroupFolderMount
202
	 * @throws FileIsNotIndexableException
203
	 */
204 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...
205
206
		foreach ($this->groupFolders as $mount) {
207
			if (strpos($file->getPath(), $mount->getPath()) === 0) {
208
				return $mount;
209
			}
210
		}
211
212
		throw new FileIsNotIndexableException();
213
214
	}
215
216
217
	/**
218
	 * @return GroupFolderMount[]
219
	 */
220
	private function getGroupFoldersMounts() {
221
222
		$groupFolders = [];
223
224
		// 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...
225
		$mounts = [];
226
		foreach ($mounts as $mountPoint => $mount) {
227
			$groupFolder = new GroupFolderMount();
228
//			$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...
229
//						  ->setPath($mountPoint)
230
//						  ->setGroups($mount['applicable']['groups'])
231
//						  ->setUsers($mount['applicable']['users'])
232
//						  ->setGlobal((!$mount['personal']));
233
			$groupFolders[] = $groupFolder;
234
		}
235
236
		return $groupFolders;
237
	}
238
239
240
241
}