Completed
Pull Request — master (#105)
by Maxence
02:19
created

MembersRequest::updateMember()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
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 string $circleUniqueId
46
	 * @param string $userId
47
	 *
48
	 * @return Member
49
	 * @throws MemberDoesNotExistException
50
	 */
51 View Code Duplication
	public function forceGetMember($circleUniqueId, $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, $circleUniqueId);
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 string $circleUniqueId
78
	 * @param int $level
79
	 * @param bool $includeGroupMembers
80
	 *
81
	 * @return Member[]
82
	 */
83
	public function forceGetMembers(
84
		$circleUniqueId, $level = Member::LEVEL_MEMBER, $includeGroupMembers = false
85
	) {
86
87
		$qb = $this->getMembersSelectSql();
88
89
		$this->limitToLevel($qb, $level);
90
		$this->limitToCircleId($qb, $circleUniqueId);
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, $circleUniqueId, $level);
101
		}
102
103
		return $members;
104
	}
105
106
107
	/**
108
	 * @param string $circleUniqueId
109
	 * @param Member $viewer
110
	 *
111
	 * @return Member[]
112
	 * @throws \Exception
113
	 */
114
	public function getMembers($circleUniqueId, Member $viewer) {
115
		try {
116
			$viewer->hasToBeMember();
117
118
			$members = $this->forceGetMembers($circleUniqueId, 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 string $circleUniqueId
143
	 * @param string $groupId
144
	 *
145
	 * @return Member
146
	 * @throws MemberDoesNotExistException
147
	 */
148 View Code Duplication
	public function forceGetGroup($circleUniqueId, $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, $circleUniqueId);
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 string $circleUniqueId
175
	 * @param int $level
176
	 */
177
	private function includeGroupMembers(array &$members, $circleUniqueId, $level) {
178
179
		$groupMembers = $this->forceGetGroupMembers($circleUniqueId, $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 string $circleUniqueId
221
	 * @param int $level
222
	 *
223
	 * @return Member[]
224
	 */
225
	public function forceGetGroupMembers($circleUniqueId, $level = Member::LEVEL_MEMBER) {
226
		$qb = $this->getGroupsSelectSql();
227
228
		$this->limitToLevel($qb, $level);
229
		$this->limitToCircleId($qb, $circleUniqueId);
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 string $circleUniqueId
250
	 * @param string $userId
251
	 *
252
	 * @return Member
253
	 */
254
	public function forceGetHigherLevelGroupFromUser($circleUniqueId, $userId) {
255
		$qb = $this->getGroupsSelectSql();
256
257
		$this->limitToCircleId($qb, $circleUniqueId);
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 string $circleUniqueId
304
	 * @param Member $viewer
305
	 *
306
	 * @return Member[]
307
	 * @throws MemberDoesNotExistException
308
	 */
309
	public function getGroupsFromCircle($circleUniqueId, Member $viewer) {
310
311
		if ($viewer->getLevel() < Member::LEVEL_MEMBER) {
312
			return [];
313
		}
314
315
		$qb = $this->getGroupsSelectSql();
316
		$this->limitToCircleId($qb, $circleUniqueId);
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
	public function updateMember(Member $member) {
363
		$qb = $this->getMembersUpdateSql($member->getCircleId(), $member->getUserId());
364
		$qb->set('level', $qb->createNamedParameter($member->getLevel()))
365
		   ->set('status', $qb->createNamedParameter($member->getStatus()));
366
367
		$qb->execute();
368
	}
369
370
371
	/**
372
	 * removeAllFromCircle();
373
	 *
374
	 * Remove All members from a Circle. Used when deleting a Circle.
375
	 *
376
	 * @param string $uniqueCircleId
377
	 */
378
	public function removeAllFromCircle($uniqueCircleId) {
379
		$qb = $this->getMembersDeleteSql($uniqueCircleId, '');
380
		$qb->execute();
381
	}
382
383
384
	/**
385
	 * removeAllFromUser();
386
	 *
387
	 * remove All membership from a User. Used when removing a User from the Cloud.
388
	 *
389
	 * @param $userId
390
	 */
391
	public function removeAllFromUser($userId) {
392
		if ($userId === '') {
393
			return;
394
		}
395
396
		$qb = $this->getMembersDeleteSql(0, $userId);
397
		$qb->execute();
398
	}
399
400
401
	/**
402
	 * update database entry for a specific Group.
403
	 *
404
	 * @param Member $member
405
	 *
406
	 * @return bool
407
	 */
408
	public function updateGroup(Member $member) {
409
410
		$qb = $this->getGroupsUpdateSql($member->getCircleId(), $member->getGroupId());
411
		$qb->set('level', $qb->createNamedParameter($member->getLevel()));
412
		$qb->execute();
413
414
		return true;
415
	}
416
417
418
	public function unlinkAllFromGroup($groupId) {
419
		$qb = $this->getGroupsDeleteSql($groupId);
420
		$qb->execute();
421
	}
422
423
}