Passed
Push — master ( 29f6ae...d5212e )
by Julius
03:55 queued 01:45
created

BoardService::enrichWithLabels()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Deck\Service;
25
26
use OCA\Deck\Activity\ActivityManager;
27
use OCA\Deck\Activity\ChangeSet;
28
use OCA\Deck\Collaboration\Resources\ResourceProvider;
29
use OCA\Deck\Db\Acl;
30
use OCA\Deck\Db\AclMapper;
31
use OCA\Deck\Db\AssignedUsersMapper;
32
use OCA\Deck\Db\ChangeHelper;
33
use OCA\Deck\Db\IPermissionMapper;
34
use OCA\Deck\Db\Label;
35
use OCA\Deck\Db\StackMapper;
36
use OCA\Deck\NoPermissionException;
37
use OCA\Deck\Notification\NotificationHelper;
38
use OCP\AppFramework\Db\DoesNotExistException;
39
use OCP\IGroupManager;
40
use OCP\IL10N;
41
use OCA\Deck\Db\Board;
42
use OCA\Deck\Db\BoardMapper;
43
use OCA\Deck\Db\LabelMapper;
44
use OCP\IUserManager;
45
use OCA\Deck\BadRequestException;
46
47
48
class BoardService {
49
50
	private $boardMapper;
51
	private $stackMapper;
52
	private $labelMapper;
53
	private $aclMapper;
54
	private $l10n;
55
	private $permissionService;
56
	private $notificationHelper;
57
	private $assignedUsersMapper;
58
	private $userManager;
59
	private $groupManager;
60
	private $userId;
61
	private $activityManager;
62
	private $changeHelper;
63
64 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...
65
		BoardMapper $boardMapper,
66
		StackMapper $stackMapper,
67
		IL10N $l10n,
68
		LabelMapper $labelMapper,
69
		AclMapper $aclMapper,
70
		PermissionService $permissionService,
71
		NotificationHelper $notificationHelper,
72
		AssignedUsersMapper $assignedUsersMapper,
73
		IUserManager $userManager,
74
		IGroupManager $groupManager,
75
		ActivityManager $activityManager,
76
		ChangeHelper $changeHelper,
77
		$userId
78
	) {
79
		$this->boardMapper = $boardMapper;
80
		$this->stackMapper = $stackMapper;
81
		$this->labelMapper = $labelMapper;
82
		$this->aclMapper = $aclMapper;
83
		$this->l10n = $l10n;
84
		$this->permissionService = $permissionService;
85
		$this->notificationHelper = $notificationHelper;
86
		$this->assignedUsersMapper = $assignedUsersMapper;
87
		$this->userManager = $userManager;
88
		$this->groupManager = $groupManager;
89
		$this->activityManager = $activityManager;
90
		$this->changeHelper = $changeHelper;
91
		$this->userId = $userId;
92
	}
93
94
	/**
95
	 * @return array
96
	 */
97
	public function findAll($since = 0, $details = null) {
98
		$userInfo = $this->getBoardPrerequisites();
99
		$userBoards = $this->boardMapper->findAllByUser($userInfo['user'], null, null, $since);
100
		$groupBoards = $this->boardMapper->findAllByGroups($userInfo['user'], $userInfo['groups'],null, null,  $since);
0 ignored issues
show
Unused Code introduced by
The call to BoardMapper::findAllByGroups() has too many arguments starting with $since.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
101
		$circleBoards = $this->boardMapper->findAllByCircles($userInfo['user'], null, null,  $since);
0 ignored issues
show
Unused Code introduced by
The call to BoardMapper::findAllByCircles() has too many arguments starting with $since.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
102
		$complete = array_merge($userBoards, $groupBoards, $circleBoards);
103
		$result = [];
104
		/** @var Board $item */
105
		foreach ($complete as &$item) {
106
			if (!array_key_exists($item->getId(), $result)) {
107
				$this->boardMapper->mapOwner($item);
108
				if ($item->getAcl() !== null) {
109
					foreach ($item->getAcl() as &$acl) {
0 ignored issues
show
Bug introduced by
The expression $item->getAcl() cannot be used as a reference.

Let?s assume that you have the following foreach statement:

foreach ($array as &$itemValue) { }

$itemValue is assigned by reference. This is possible because the expression (in the example $array) can be used as a reference target.

However, if we were to replace $array with something different like the result of a function call as in

foreach (getArray() as &$itemValue) { }

then assigning by reference is not possible anymore as there is no target that could be modified.

Available Fixes

1. Do not assign by reference
foreach (getArray() as $itemValue) { }
2. Assign to a local variable first
$array = getArray();
foreach ($array as &$itemValue) {}
3. Return a reference
function &getArray() { $array = array(); return $array; }

foreach (getArray() as &$itemValue) { }
Loading history...
110
						$this->boardMapper->mapAcl($acl);
111
					}
112
				}
113
				if ($details !== null) {
114
					$this->enrichWithStacks($item);
115
					$this->enrichWithLabels($item);
116
					$this->enrichWithUsers($item);
117
				}
118
				$permissions = $this->permissionService->matchPermissions($item);
119
				$item->setPermissions([
120
					'PERMISSION_READ' => $permissions[Acl::PERMISSION_READ],
121
					'PERMISSION_EDIT' => $permissions[Acl::PERMISSION_EDIT],
122
					'PERMISSION_MANAGE' => $permissions[Acl::PERMISSION_MANAGE],
123
					'PERMISSION_SHARE' => $permissions[Acl::PERMISSION_SHARE]
124
				]);
125
				$result[$item->getId()] = $item;
126
			}
127
		}
128
		return array_values($result);
129
	}
130
131
	/**
132
	 * @param $boardId
133
	 * @return Board
134
	 * @throws DoesNotExistException
135
	 * @throws \OCA\Deck\NoPermissionException
136
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
137
	 * @throws BadRequestException
138
	 */
139
	public function find($boardId) {
140
141
		if ( is_numeric($boardId) === false ) {
142
			throw new BadRequestException('board id must be a number');
143
		}
144
145
		$this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_READ);
146
		/** @var Board $board */
147
		$board = $this->boardMapper->find($boardId, true, true);
148
		$this->boardMapper->mapOwner($board);
149
		foreach ($board->getAcl() as &$acl) {
0 ignored issues
show
Bug introduced by
The expression $board->getAcl() cannot be used as a reference.

Let?s assume that you have the following foreach statement:

foreach ($array as &$itemValue) { }

$itemValue is assigned by reference. This is possible because the expression (in the example $array) can be used as a reference target.

However, if we were to replace $array with something different like the result of a function call as in

foreach (getArray() as &$itemValue) { }

then assigning by reference is not possible anymore as there is no target that could be modified.

Available Fixes

1. Do not assign by reference
foreach (getArray() as $itemValue) { }
2. Assign to a local variable first
$array = getArray();
foreach ($array as &$itemValue) {}
3. Return a reference
function &getArray() { $array = array(); return $array; }

foreach (getArray() as &$itemValue) { }
Loading history...
150
			if ($acl !== null) {
151
				$this->boardMapper->mapAcl($acl);
152
			}
153
		}
154
		$permissions = $this->permissionService->matchPermissions($board);
155
		$board->setPermissions([
156
			'PERMISSION_READ' => $permissions[Acl::PERMISSION_READ],
157
			'PERMISSION_EDIT' => $permissions[Acl::PERMISSION_EDIT],
158
			'PERMISSION_MANAGE' => $permissions[Acl::PERMISSION_MANAGE],
159
			'PERMISSION_SHARE' => $permissions[Acl::PERMISSION_SHARE]
160
		]);
161
		$this->enrichWithUsers($board);
162
		return $board;
163
	}
164
165
	/**
166
	 * @return array
167
	 */
168
	private function getBoardPrerequisites() {
169
		$groups = $this->groupManager->getUserGroupIds(
170
			$this->userManager->get($this->userId)
0 ignored issues
show
Bug introduced by
It seems like $this->userManager->get($this->userId) can be null; however, getUserGroupIds() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
171
		);
172
		return [
173
			'user' => $this->userId,
174
			'groups' => $groups
175
		];
176
	}
177
178
	/**
179
	 * @param $mapper
180
	 * @param $id
181
	 * @return bool
182
	 * @throws DoesNotExistException
183
	 * @throws \OCA\Deck\NoPermissionException
184
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
185
	 * @throws BadRequestException
186
	 */
187
	public function isArchived($mapper, $id) {
188
189
		if (is_numeric($id) === false)  {
190
			throw new BadRequestException('id must be a number');
191
		}
192
193
		try {
194
			$boardId = $id;
195
			if ($mapper instanceof IPermissionMapper) {
196
				$boardId = $mapper->findBoardId($id);
197
			}
198
			if ($boardId === null) {
199
				return false;
200
			}
201
		} catch (DoesNotExistException $exception) {
202
			return false;
203
		}
204
		$board = $this->find($boardId);
205
		return $board->getArchived();
206
	}
207
208
	/**
209
	 * @param $mapper
210
	 * @param $id
211
	 * @return bool
212
	 * @throws DoesNotExistException
213
	 * @throws \OCA\Deck\NoPermissionException
214
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
215
	 * @throws BadRequestException
216
	 */
217
	public function isDeleted($mapper, $id) {
218
219
		if ($mapper === false || $mapper === null) {
220
			throw new BadRequestException('mapper must be provided');
221
		}
222
223
		if (is_numeric($id) === false)  {
224
			throw new BadRequestException('id must be a number');
225
		}
226
227
		try {
228
			$boardId = $id;
229
			if ($mapper instanceof IPermissionMapper) {
230
				$boardId = $mapper->findBoardId($id);
231
			}
232
			if ($boardId === null) {
233
				return false;
234
			}
235
		} catch (DoesNotExistException $exception) {
236
			return false;
237
		}
238
		$board = $this->find($boardId);
239
		return $board->getDeletedAt() > 0;
240
	}
241
242
243
	/**
244
	 * @param $title
245
	 * @param $userId
246
	 * @param $color
247
	 * @return \OCP\AppFramework\Db\Entity
248
	 * @throws BadRequestException
249
	 */
250
	public function create($title, $userId, $color) {
251
252
		if ($title === false || $title === null) {
253
			throw new BadRequestException('title must be provided');
254
		}
255
256
		if ($userId === false || $userId === null) {
257
			throw new BadRequestException('userId must be provided');
258
		}
259
260
		if ($color === false || $color === null) {
261
			throw new BadRequestException('color must be provided');
262
		}
263
264
		if (!$this->permissionService->canCreate()) {
265
			throw new NoPermissionException('Creating boards has been disabled for your account.');
266
		}
267
268
		$board = new Board();
269
		$board->setTitle($title);
270
		$board->setOwner($userId);
271
		$board->setColor($color);
272
		$new_board = $this->boardMapper->insert($board);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::insert() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
273
274
		// create new labels
275
		$default_labels = [
276
			'31CC7C' => $this->l10n->t('Finished'),
277
			'317CCC' => $this->l10n->t('To review'),
278
			'FF7A66' => $this->l10n->t('Action needed'),
279
			'F1DB50' => $this->l10n->t('Later')
280
		];
281
		$labels = [];
282
		foreach ($default_labels as $labelColor => $labelTitle) {
283
			$label = new Label();
284
			$label->setColor($labelColor);
285
			$label->setTitle($labelTitle);
286
			$label->setBoardId($new_board->getId());
287
			$labels[] = $this->labelMapper->insert($label);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::insert() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
288
		}
289
		$new_board->setLabels($labels);
290
		$this->boardMapper->mapOwner($new_board);
291
		$permissions = $this->permissionService->matchPermissions($new_board);
292
		$new_board->setPermissions([
293
			'PERMISSION_READ' => $permissions[Acl::PERMISSION_READ],
294
			'PERMISSION_EDIT' => $permissions[Acl::PERMISSION_EDIT],
295
			'PERMISSION_MANAGE' => $permissions[Acl::PERMISSION_MANAGE],
296
			'PERMISSION_SHARE' => $permissions[Acl::PERMISSION_SHARE]
297
		]);
298
		$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $new_board, ActivityManager::SUBJECT_BOARD_CREATE);
299
		$this->changeHelper->boardChanged($new_board->getId());
300
		return $new_board;
301
302
	}
303
304
	/**
305
	 * @param $id
306
	 * @return Board
307
	 * @throws DoesNotExistException
308
	 * @throws \OCA\Deck\NoPermissionException
309
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
310
	 * @throws BadRequestException
311
	 */
312
	public function delete($id) {
313
314
		if (is_numeric($id) === false) {
315
			throw new BadRequestException('board id must be a number');
316
		}
317
318
		$this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_READ);
319
		$board = $this->find($id);
320
		if ($board->getDeletedAt() > 0) {
321
			throw new BadRequestException('This board has already been deleted');
322
		}
323
		$board->setDeletedAt(time());
324
		$board = $this->boardMapper->update($board);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::update() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
325
		$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $board, ActivityManager::SUBJECT_BOARD_DELETE);
326
		$this->changeHelper->boardChanged($board->getId());
327
		return $board;
328
	}
329
330
	/**
331
	 * @param $id
332
	 * @return \OCP\AppFramework\Db\Entity
333
	 * @throws DoesNotExistException
334
	 * @throws \OCA\Deck\NoPermissionException
335
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
336
	 */
337 View Code Duplication
	public function deleteUndo($id) {
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...
338
339
		if (is_numeric($id) === false) {
340
			throw new BadRequestException('board id must be a number');
341
		}
342
343
		$this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_READ);
344
		$board = $this->find($id);
345
		$board->setDeletedAt(0);
346
		$board = $this->boardMapper->update($board);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::update() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
347
		$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $board, ActivityManager::SUBJECT_BOARD_RESTORE);
348
		$this->changeHelper->boardChanged($board->getId());
349
		return $board;
350
	}
351
352
	/**
353
	 * @param $id
354
	 * @return \OCP\AppFramework\Db\Entity
355
	 * @throws DoesNotExistException
356
	 * @throws \OCA\Deck\NoPermissionException
357
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
358
	 * @throws BadRequestException
359
	 */
360 View Code Duplication
	public function deleteForce($id) {
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...
361
		if (is_numeric($id) === false)  {
362
			throw new BadRequestException('id must be a number');
363
		}
364
365
		$this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_READ);
366
		$board = $this->find($id);
367
		return $this->boardMapper->delete($board);
368
	}
369
370
	/**
371
	 * @param $id
372
	 * @param $title
373
	 * @param $color
374
	 * @param $archived
375
	 * @return \OCP\AppFramework\Db\Entity
376
	 * @throws DoesNotExistException
377
	 * @throws \OCA\Deck\NoPermissionException
378
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
379
	 * @throws BadRequestException
380
	 */
381
	public function update($id, $title, $color, $archived) {
382
383
		if (is_numeric($id) === false) {
384
			throw new BadRequestException('board id must be a number');
385
		}
386
387
		if ($title === false || $title === null) {
388
			throw new BadRequestException('color must be provided');
389
		}
390
391
		if ($color === false || $color === null) {
392
			throw new BadRequestException('color must be provided');
393
		}
394
395
		if ( is_bool($archived) === false ) {
396
			throw new BadRequestException('archived must be a boolean');
397
		}
398
399
		$this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_MANAGE);
400
		$board = $this->find($id);
401
		$changes = new ChangeSet($board);
402
		$board->setTitle($title);
403
		$board->setColor($color);
404
		$board->setArchived($archived);
405
		$changes->setAfter($board);
406
		$this->boardMapper->update($board); // operate on clone so we can check for updated fields
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::update() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
407
		$this->boardMapper->mapOwner($board);
408
		$this->activityManager->triggerUpdateEvents(ActivityManager::DECK_OBJECT_BOARD, $changes, ActivityManager::SUBJECT_BOARD_UPDATE);
409
		$this->changeHelper->boardChanged($board->getId());
410
		return $board;
411
	}
412
413
414
	/**
415
	 * @param $boardId
416
	 * @param $type
417
	 * @param $participant
418
	 * @param $edit
419
	 * @param $share
420
	 * @param $manage
421
	 * @return \OCP\AppFramework\Db\Entity
422
	 * @throws BadRequestException
423
	 * @throws \OCA\Deck\NoPermissionException
424
	 */
425
	public function addAcl($boardId, $type, $participant, $edit, $share, $manage) {
426
427
		if (is_numeric($boardId) === false) {
428
			throw new BadRequestException('board id must be a number');
429
		}
430
431
		if ($type === false || $type === null) {
432
			throw new BadRequestException('type must be provided');
433
		}
434
435
		if ($participant === false || $participant === null) {
436
			throw new BadRequestException('participant must be provided');
437
		}
438
439
		if ($edit === null) {
440
			throw new BadRequestException('edit must be provided');
441
		}
442
443
		if ($share === null) {
444
			throw new BadRequestException('share must be provided');
445
		}
446
447
		if ($manage === null) {
448
			throw new BadRequestException('manage must be provided');
449
		}
450
451
		$this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_SHARE);
452
		$acl = new Acl();
453
		$acl->setBoardId($boardId);
454
		$acl->setType($type);
455
		$acl->setParticipant($participant);
456
		$acl->setPermissionEdit($edit);
457
		$acl->setPermissionShare($share);
458
		$acl->setPermissionManage($manage);
459
460
		/* Notify users about the shared board */
461
		$this->notificationHelper->sendBoardShared($boardId, $acl);
462
463
		$newAcl = $this->aclMapper->insert($acl);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::insert() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
464
		$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $newAcl, ActivityManager::SUBJECT_BOARD_SHARE);
465
		$this->boardMapper->mapAcl($newAcl);
466
		$this->changeHelper->boardChanged($boardId);
467
		$version = \OC_Util::getVersion()[0];
468 View Code Duplication
		if ($version >= 16) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
469
			try {
470
				$resourceProvider = \OC::$server->query(\OCA\Deck\Collaboration\Resources\ResourceProvider::class);
471
				$resourceProvider->invalidateAccessCache($boardId);
472
			} catch (\Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
473
		}
474
		return $newAcl;
475
	}
476
477
	/**
478
	 * @param $id
479
	 * @param $edit
480
	 * @param $share
481
	 * @param $manage
482
	 * @return \OCP\AppFramework\Db\Entity
483
	 * @throws DoesNotExistException
484
	 * @throws \OCA\Deck\NoPermissionException
485
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
486
	 * @throws BadRequestException
487
	 */
488
	public function updateAcl($id, $edit, $share, $manage) {
489
490
		if (is_numeric($id) === false) {
491
			throw new BadRequestException('id must be a number');
492
		}
493
494
		if ($edit === null) {
495
			throw new BadRequestException('edit must be provided');
496
		}
497
498
		if ($share === null) {
499
			throw new BadRequestException('share must be provided');
500
		}
501
502
		if ($manage === null) {
503
			throw new BadRequestException('manage must be provided');
504
		}
505
506
		$this->permissionService->checkPermission($this->aclMapper, $id, Acl::PERMISSION_SHARE);
507
		/** @var Acl $acl */
508
		$acl = $this->aclMapper->find($id);
509
		$acl->setPermissionEdit($edit);
510
		$acl->setPermissionShare($share);
511
		$acl->setPermissionManage($manage);
512
		$this->boardMapper->mapAcl($acl);
513
		$board = $this->aclMapper->update($acl);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::update() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
514
		$this->changeHelper->boardChanged($acl->getBoardId());
515
		return $board;
516
	}
517
518
	/**
519
	 * @param $id
520
	 * @return \OCP\AppFramework\Db\Entity
521
	 * @throws DoesNotExistException
522
	 * @throws \OCA\Deck\NoPermissionException
523
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
524
	 * @throws BadRequestException
525
	 */
526
	public function deleteAcl($id) {
527
528
		if (is_numeric($id) === false) {
529
			throw new BadRequestException('id must be a number');
530
		}
531
532
		$this->permissionService->checkPermission($this->aclMapper, $id, Acl::PERMISSION_SHARE);
533
		/** @var Acl $acl */
534
		$acl = $this->aclMapper->find($id);
535
		$this->boardMapper->mapAcl($acl);
536
		if ($acl->getType() === Acl::PERMISSION_TYPE_USER) {
537
			$assignements = $this->assignedUsersMapper->findByUserId($acl->getParticipant());
538
			foreach ($assignements as $assignement) {
539
				$this->assignedUsersMapper->delete($assignement);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::delete() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
540
			}
541
		}
542
		$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $acl, ActivityManager::SUBJECT_BOARD_UNSHARE);
543
		$this->changeHelper->boardChanged($acl->getBoardId());
544
		$version = \OC_Util::getVersion()[0];
545 View Code Duplication
		if ($version >= 16) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
546
			try {
547
				$resourceProvider = \OC::$server->query(\OCA\Deck\Collaboration\Resources\ResourceProvider::class);
548
				$resourceProvider->invalidateAccessCache($acl->getBoardId());
549
			} catch (\Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
550
		}
551
		return $this->aclMapper->delete($acl);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::delete() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
552
	}
553
554
	private function enrichWithStacks($board, $since = -1) {
555
		$stacks = $this->stackMapper->findAll($board->getId(), null, null, $since);
0 ignored issues
show
Unused Code introduced by
The call to StackMapper::findAll() has too many arguments starting with $since.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
556
557
		if(\count($stacks) === 0) {
558
			return;
559
		}
560
561
		$board->setStacks($stacks);
562
	}
563
564
	private function enrichWithLabels($board, $since = -1) {
565
		$labels = $this->labelMapper->findAll($board->getId(), null, null, $since);
0 ignored issues
show
Unused Code introduced by
The call to LabelMapper::findAll() has too many arguments starting with $since.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
566
567
		if(\count($labels) === 0) {
568
			return;
569
		}
570
571
		$board->setLabels($labels);
572
	}
573
574
	private function enrichWithUsers($board, $since = -1) {
0 ignored issues
show
Unused Code introduced by
The parameter $since 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...
575
		$boardUsers = $this->permissionService->findUsers($board->getId());
576
		if(\count($boardUsers) === 0) {
577
			return;
578
		}
579
		$board->setUsers(array_values($boardUsers));
580
	}
581
582
}
583