Completed
Push — master ( 3071ad...3723f6 )
by Maxence
03:35 queued 01:12
created

MembersRequest   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 394
Duplicated Lines 8.88 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 38
c 1
b 0
f 0
lcom 1
cbo 4
dl 35
loc 394
rs 8.3999

16 Methods

Rating   Name   Duplication   Size   Complexity  
A forceGetMember() 18 18 2
A forceGetMembers() 0 22 3
A getMembers() 0 18 3
A forceGetGroup() 17 17 2
A includeGroupMembers() 0 12 4
A indexOfMember() 0 10 3
A forceGetGroupMembers() 0 16 2
A forceGetHigherLevelGroupFromUser() 0 20 4
A createMember() 0 17 2
B getGroups() 0 22 4
A insertGroup() 0 15 2
A updateMember() 0 7 1
A removeAllFromCircle() 0 8 2
A removeAllFromUser() 0 8 2
A updateGroup() 0 8 1
A unlinkAllFromGroup() 0 4 1

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
 * Circles - Bring cloud-users closer together.
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 2017
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\Circles\Db;
29
30
31
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
32
use OCA\Circles\Exceptions\MemberAlreadyExistsException;
33
use OCA\Circles\Exceptions\MemberDoesNotExistException;
34
use OCA\Circles\Model\Member;
35
36
class MembersRequest extends MembersRequestBuilder {
37
38
39
	/**
40
	 * Returns information about a member.
41
	 *
42
	 * WARNING: This function does not filters data regarding the current user/viewer.
43
	 *          In case of interaction with users, Please use MembersService->getMember() instead.
44
	 *
45
	 * @param $circleId
46
	 * @param $userId
47
	 *
48
	 * @return Member
49
	 * @throws MemberDoesNotExistException
50
	 */
51 View Code Duplication
	public function forceGetMember($circleId, $userId) {
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...
52
		$qb = $this->getMembersSelectSql();
53
54
		$this->limitToUserId($qb, $userId);
55
		$this->limitToCircleId($qb, $circleId);
56
57
		$cursor = $qb->execute();
58
		$data = $cursor->fetch();
59
		$cursor->closeCursor();
60
61
		if ($data === false) {
62
			throw new MemberDoesNotExistException($this->l10n->t('This member does not exist'));
63
		}
64
65
		$member = $this->parseMembersSelectSql($data);
66
67
		return $member;
68
	}
69
70
71
	/**
72
	 * Returns members list of a circle, based on their level.
73
	 *
74
	 * WARNING: This function does not filters data regarding the current user/viewer.
75
	 *          In case of interaction with users, Please use getMembers() instead.
76
	 *
77
	 * @param int $circleId
78
	 * @param int $level
79
	 * @param bool $includeGroupMembers
80
	 *
81
	 * @return Member[]
82
	 */
83
	public function forceGetMembers(
84
		$circleId, $level = Member::LEVEL_MEMBER, $includeGroupMembers = false
85
	) {
86
87
		$qb = $this->getMembersSelectSql();
88
89
		$this->limitToLevel($qb, $level);
90
		$this->limitToCircleId($qb, $circleId);
91
92
		$members = [];
93
		$cursor = $qb->execute();
94
		while ($data = $cursor->fetch()) {
95
			$members[] = $this->parseMembersSelectSql($data);
96
		}
97
		$cursor->closeCursor();
98
99
		if ($includeGroupMembers === true) {
100
			$this->includeGroupMembers($members, $circleId, $level);
101
		}
102
103
		return $members;
104
	}
105
106
107
	/**
108
	 * @param int $circleId
109
	 * @param Member $viewer
110
	 *
111
	 * @return Member[]
112
	 * @throws \Exception
113
	 */
114
	public function getMembers($circleId, Member $viewer) {
115
		try {
116
			$viewer->hasToBeMember();
117
118
			$members = $this->forceGetMembers($circleId, Member::LEVEL_MEMBER);
119
			if (!$viewer->isLevel(Member::LEVEL_MODERATOR)) {
120
				array_map(
121
					function(Member $m) {
122
						$m->setNote('');
123
					}, $members
124
				);
125
			}
126
127
			return $members;
128
		} catch (\Exception $e) {
129
			return [];
130
		}
131
	}
132
133
134
	/**
135
	 * forceGetGroup();
136
	 *
137
	 * returns group information as a member within a Circle.
138
	 *
139
	 * WARNING: This function does not filters data regarding the current user/viewer.
140
	 *          In case of interaction with users, Please use getGroup() instead.
141
	 *
142
	 * @param int $circleId
143
	 * @param string $groupId
144
	 *
145
	 * @return Member
146
	 * @throws MemberDoesNotExistException
147
	 */
148 View Code Duplication
	public function forceGetGroup($circleId, $groupId) {
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...
149
		$qb = $this->getGroupsSelectSql();
150
151
		$this->limitToGroupId($qb, $groupId);
152
		$this->limitToCircleId($qb, $circleId);
153
154
		$cursor = $qb->execute();
155
		$data = $cursor->fetch();
156
		if ($data === false) {
157
			throw new MemberDoesNotExistException($this->l10n->t('This member does not exist'));
158
		}
159
160
		$group = $this->parseGroupsSelectSql($data);
161
		$cursor->closeCursor();
162
163
		return $group;
164
	}
165
166
167
	/**
168
	 * includeGroupMembers();
169
	 *
170
	 * This function will get members of a circle throw NCGroups and fill the result an existing
171
	 * Members List. In case of duplicate, higher level will be kept.
172
	 *
173
	 * @param Member[] $members
174
	 * @param int $circleId
175
	 * @param int $level
176
	 */
177
	private function includeGroupMembers(array &$members, $circleId, $level) {
178
179
		$groupMembers = $this->forceGetGroupMembers($circleId, $level);
180
		foreach ($groupMembers as $member) {
181
			$index = $this->indexOfMember($members, $member->getUserId());
182
			if ($index === -1) {
183
				array_push($members, $member);
184
			} else if ($members[$index]->getLevel() < $member->getLevel()) {
185
				$members[$index] = $member;
186
			}
187
		}
188
	}
189
190
191
	/**
192
	 * returns the index of a specific UserID in a Members List
193
	 *
194
	 * @param array $members
195
	 * @param $userId
196
	 *
197
	 * @return int
198
	 */
199
	private function indexOfMember(array $members, $userId) {
200
201
		foreach ($members as $k => $member) {
202
			if ($member->getUserId() === $userId) {
203
				return intval($k);
204
			}
205
		}
206
207
		return -1;
208
	}
209
210
211
	/**
212
	 * Returns members list of a Group Members of a Circle. The Level of the linked group will be
213
	 * assigned to each entry
214
	 *
215
	 * NOTE: Can contains duplicate.
216
	 *
217
	 * WARNING: This function does not filters data regarding the current user/viewer.
218
	 *          Do not use in case of direct interaction with users.
219
	 *
220
	 * @param int $circleId
221
	 * @param int $level
222
	 *
223
	 * @return Member[]
224
	 */
225
	public function forceGetGroupMembers($circleId, $level = Member::LEVEL_MEMBER) {
226
		$qb = $this->getGroupsSelectSql();
227
228
		$this->limitToLevel($qb, $level);
229
		$this->limitToCircleId($qb, $circleId);
230
		$this->limitToNCGroupUser($qb);
231
232
		$members = [];
233
		$cursor = $qb->execute();
234
		while ($data = $cursor->fetch()) {
235
			$members[] = $this->parseGroupsSelectSql($data);
236
		}
237
		$cursor->closeCursor();
238
239
		return $members;
240
	}
241
242
243
	/**
244
	 * return the higher level group linked to a circle, that include the userId.
245
	 *
246
	 * WARNING: This function does not filters data regarding the current user/viewer.
247
	 *          In case of direct interaction with users, Please don't use this.
248
	 *
249
	 * @param int $circleId
250
	 * @param string $userId
251
	 *
252
	 * @return Member
253
	 */
254
	public function forceGetHigherLevelGroupFromUser($circleId, $userId) {
255
		$qb = $this->getGroupsSelectSql();
256
257
		$this->limitToCircleId($qb, $circleId);
258
		$this->limitToNCGroupUser($qb, $userId);
259
260
		/** @var Member $group */
261
		$group = null;
262
263
		$cursor = $qb->execute();
264
		while ($data = $cursor->fetch()) {
265
			$entry = $this->parseGroupsSelectSql($data);
266
			if ($group === null || $entry->getLevel() > $group->getLevel()) {
267
				$group = $entry;
268
			}
269
		}
270
		$cursor->closeCursor();
271
272
		return $group;
273
	}
274
275
276
	/**
277
	 * Insert Member into database.
278
	 *
279
	 * @param Member $member
280
	 *
281
	 * @throws MemberAlreadyExistsException
282
	 */
283
	public function createMember(Member $member) {
284
285
		try {
286
			$qb = $this->getMembersInsertSql();
287
			$qb->setValue('circle_id', $qb->createNamedParameter($member->getCircleId()))
288
			   ->setValue('user_id', $qb->createNamedParameter($member->getUserId()))
289
			   ->setValue('level', $qb->createNamedParameter($member->getLevel()))
290
			   ->setValue('status', $qb->createNamedParameter($member->getStatus()))
291
			   ->setValue('note', $qb->createNamedParameter($member->getNote()));
292
293
			$qb->execute();
294
		} catch (UniqueConstraintViolationException $e) {
0 ignored issues
show
Bug introduced by
The class Doctrine\DBAL\Exception\...raintViolationException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
295
			throw new MemberAlreadyExistsException(
296
				$this->l10n->t('This user is already a member of the circle')
297
			);
298
		}
299
	}
300
301
302
	/**
303
	 * @param int $circleId
304
	 * @param Member $viewer
305
	 *
306
	 * @return Member[]
307
	 * @throws MemberDoesNotExistException
308
	 */
309
	public function getGroups($circleId, Member $viewer) {
310
311
		if ($viewer->getLevel() < Member::LEVEL_MEMBER) {
312
			return [];
313
		}
314
315
		$qb = $this->getGroupsSelectSql();
316
		$this->limitToCircleId($qb, $circleId);
317
		$this->limitToLevel($qb, Member::LEVEL_MEMBER);
318
319
		$cursor = $qb->execute();
320
		$groups = [];
321
		while ($data = $cursor->fetch()) {
322
			if ($viewer->getLevel() < Member::LEVEL_MODERATOR) {
323
				$data['note'] = '';
324
			}
325
			$groups[] = $this->parseGroupsSelectSql($data);
326
		}
327
		$cursor->closeCursor();
328
329
		return $groups;
330
	}
331
332
333
	/**
334
	 * Insert Member into database.
335
	 *
336
	 * @param Member $member
337
	 *
338
	 * @throws MemberAlreadyExistsException
339
	 */
340
	public function insertGroup(Member $member) {
341
		try {
342
			$qb = $this->getGroupsInsertSql();
343
			$qb->setValue('circle_id', $qb->createNamedParameter($member->getCircleId()))
344
			   ->setValue('group_id', $qb->createNamedParameter($member->getGroupId()))
345
			   ->setValue('level', $qb->createNamedParameter($member->getLevel()))
346
			   ->setValue('note', $qb->createNamedParameter($member->getNote()));
347
348
			$qb->execute();
349
		} catch (UniqueConstraintViolationException $e) {
350
			throw new MemberAlreadyExistsException(
351
				$this->l10n->t('This user is already a member of the circle')
352
			);
353
		}
354
	}
355
356
357
	/**
358
	 * update database entry for a specific Member.
359
	 *
360
	 * @param Member $member
361
	 *
362
	 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
363
	 */
364
	public function updateMember(Member $member) {
365
		$qb = $this->getMembersUpdateSql($member->getCircleId(), $member->getUserId());
366
		$qb->set('level', $qb->createNamedParameter($member->getLevel()))
367
		   ->set('status', $qb->createNamedParameter($member->getStatus()));
368
369
		$qb->execute();
370
	}
371
372
373
	/**
374
	 * removeAllFromCircle();
375
	 *
376
	 * Remove All members from a Circle. Used when deleting a Circle.
377
	 *
378
	 * @param $circleId
379
	 */
380
	public function removeAllFromCircle($circleId) {
381
		if ($circleId === 0) {
382
			return;
383
		}
384
385
		$qb = $this->getMembersDeleteSql($circleId, '');
386
		$qb->execute();
387
	}
388
389
390
	/**
391
	 * removeAllFromUser();
392
	 *
393
	 * remove All membership from a User. Used when removing a User from the Cloud.
394
	 *
395
	 * @param $userId
396
	 */
397
	public function removeAllFromUser($userId) {
398
		if ($userId === '') {
399
			return;
400
		}
401
402
		$qb = $this->getMembersDeleteSql(0, $userId);
403
		$qb->execute();
404
	}
405
406
407
	/**
408
	 * update database entry for a specific Group.
409
	 *
410
	 * @param Member $member
411
	 *
412
	 * @return bool
413
	 */
414
	public function updateGroup(Member $member) {
415
416
		$qb = $this->getGroupsUpdateSql($member->getCircleId(), $member->getGroupId());
417
		$qb->set('level', $qb->createNamedParameter($member->getLevel()));
418
		$qb->execute();
419
420
		return true;
421
	}
422
423
424
	public function unlinkAllFromGroup($groupId) {
425
		$qb = $this->getGroupsDeleteSql($groupId);
426
		$qb->execute();
427
	}
428
429
}