StackService   A
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 330
Duplicated Lines 7.27 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 0
Metric Value
wmc 40
lcom 1
cbo 15
dl 24
loc 330
rs 9.2
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A enrichStackWithCards() 0 13 3
A enrichStacksWithCards() 0 5 2
A find() 0 16 3
A findAll() 0 11 2
A fetchDeleted() 0 9 1
A findAllArchived() 0 21 5
B create() 0 35 6
A delete() 24 24 2
B update() 0 41 7
B reorder() 0 34 8
A __construct() 0 27 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like StackService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use StackService, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 * @author Maxence Lange <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 *  This program is free software: you can redistribute it and/or modify
11
 *  it under the terms of the GNU Affero General Public License as
12
 *  published by the Free Software Foundation, either version 3 of the
13
 *  License, or (at your option) any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU Affero General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU Affero General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\Deck\Service;
26
27
use OCA\Deck\Activity\ActivityManager;
28
use OCA\Deck\Activity\ChangeSet;
29
use OCA\Deck\BadRequestException;
30
use OCA\Deck\Db\Acl;
31
use OCA\Deck\Db\AssignedUsersMapper;
32
use OCA\Deck\Db\BoardMapper;
33
use OCA\Deck\Db\CardMapper;
34
use OCA\Deck\Db\ChangeHelper;
35
use OCA\Deck\Db\LabelMapper;
36
use OCA\Deck\Db\Stack;
37
use OCA\Deck\Db\StackMapper;
38
use OCA\Deck\StatusException;
39
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
40
use Symfony\Component\EventDispatcher\GenericEvent;
41
42
43
class StackService {
44
45
	private $stackMapper;
46
	private $cardMapper;
47
	private $boardMapper;
48
	private $labelMapper;
49
	private $permissionService;
50
	private $boardService;
51
	private $cardService;
52
	private $assignedUsersMapper;
53
	private $attachmentService;
54
	private $activityManager;
55
	/** @var EventDispatcherInterface */
56
	private $eventDispatcher;
57
	private $changeHelper;
58
59
	public function __construct(
60
		StackMapper $stackMapper,
61
		BoardMapper $boardMapper,
62
		CardMapper $cardMapper,
63
		LabelMapper $labelMapper,
64
		PermissionService $permissionService,
65
		BoardService $boardService,
66
		CardService $cardService,
67
		AssignedUsersMapper $assignedUsersMapper,
68
		AttachmentService $attachmentService,
69
		ActivityManager $activityManager,
70
		EventDispatcherInterface $eventDispatcher,
71
		ChangeHelper $changeHelper
72
	) {
73
		$this->stackMapper = $stackMapper;
74
		$this->boardMapper = $boardMapper;
75
		$this->cardMapper = $cardMapper;
76
		$this->labelMapper = $labelMapper;
77
		$this->permissionService = $permissionService;
78
		$this->boardService = $boardService;
79
		$this->cardService = $cardService;
80
		$this->assignedUsersMapper = $assignedUsersMapper;
81
		$this->attachmentService = $attachmentService;
82
		$this->activityManager = $activityManager;
83
		$this->eventDispatcher = $eventDispatcher;
84
		$this->changeHelper = $changeHelper;
85
	}
86
87
	private function enrichStackWithCards($stack, $since = -1) {
88
		$cards = $this->cardMapper->findAll($stack->getId(), null, null, $since);
89
90
		if (\count($cards) === 0) {
91
			return;
92
		}
93
94
		foreach ($cards as $card) {
95
			$this->cardService->enrich($card);
96
		}
97
98
		$stack->setCards($cards);
99
	}
100
101
	private function enrichStacksWithCards($stacks, $since = -1) {
102
		foreach ($stacks as $stack) {
103
			$this->enrichStackWithCards($stack, $since);
104
		}
105
	}
106
107
	/**
108
	 * @param $stackId
109
	 *
110
	 * @return \OCP\AppFramework\Db\Entity
111
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
112
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
113
	 * @throws BadRequestException
114
	 */
115
	public function find($stackId) {
116
		if (is_numeric($stackId) === false) {
117
			throw new BadRequestException('stack id must be a number');
118
		}
119
120
		$stack = $this->stackMapper->find($stackId);
121
		$cards = $this->cardMapper->findAll($stackId);
122
		foreach ($cards as $cardIndex => $card) {
123
			$assignedUsers = $this->assignedUsersMapper->find($card->getId());
124
			$card->setAssignedUsers($assignedUsers);
125
			$card->setAttachmentCount($this->attachmentService->count($card->getId()));
126
		}
127
		$stack->setCards($cards);
128
129
		return $stack;
130
	}
131
132
	/**
133
	 * @param $boardId
134
	 *
135
	 * @return array
136
	 * @throws \OCA\Deck\NoPermissionException
137
	 * @throws BadRequestException
138
	 */
139
	public function findAll($boardId, $since = -1) {
140
		if (is_numeric($boardId) === false) {
141
			throw new BadRequestException('boardId must be a number');
142
		}
143
144
		$this->permissionService->checkPermission(null, $boardId, Acl::PERMISSION_READ);
145
		$stacks = $this->stackMapper->findAll($boardId);
146
		$this->enrichStacksWithCards($stacks, $since);
147
148
		return $stacks;
149
	}
150
151
	public function fetchDeleted($boardId) {
152
		$this->permissionService->checkPermission(
153
			$this->boardMapper, $boardId, Acl::PERMISSION_READ
154
		);
155
		$stacks = $this->stackMapper->findDeleted($boardId);
156
		$this->enrichStacksWithCards($stacks);
157
158
		return $stacks;
159
	}
160
161
	/**
162
	 * @param $boardId
163
	 *
164
	 * @return array
165
	 * @throws \OCA\Deck\NoPermissionException
166
	 * @throws BadRequestException
167
	 */
168
	public function findAllArchived($boardId) {
169
170
		if (is_numeric($boardId) === false) {
171
			throw new BadRequestException('board id must be a number');
172
		}
173
174
		$this->permissionService->checkPermission(null, $boardId, Acl::PERMISSION_READ);
175
		$stacks = $this->stackMapper->findAll($boardId);
176
		$labels = $this->labelMapper->getAssignedLabelsForBoard($boardId);
177
		foreach ($stacks as $stackIndex => $stack) {
178
			$cards = $this->cardMapper->findAllArchived($stack->id);
179
			foreach ($cards as $cardIndex => $card) {
180
				if (array_key_exists($card->id, $labels)) {
181
					$cards[$cardIndex]->setLabels($labels[$card->id]);
182
				}
183
			}
184
			$stacks[$stackIndex]->setCards($cards);
185
		}
186
187
		return $stacks;
188
	}
189
190
	/**
191
	 * @param $title
192
	 * @param $boardId
193
	 * @param integer $order
194
	 *
195
	 * @return \OCP\AppFramework\Db\Entity
196
	 * @throws StatusException
197
	 * @throws \OCA\Deck\NoPermissionException
198
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
199
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
200
	 * @throws BadRequestException
201
	 */
202
	public function create($title, $boardId, $order) {
203
204
		if ($title === false || $title === null) {
205
			throw new BadRequestException('title must be provided');
206
		}
207
208
		if (is_numeric($order) === false) {
209
			throw new BadRequestException('order must be a number');
210
		}
211
212
		if (is_numeric($boardId) === false) {
213
			throw new BadRequestException('board id must be a number');
214
		}
215
216
		$this->permissionService->checkPermission(null, $boardId, Acl::PERMISSION_MANAGE);
217
		if ($this->boardService->isArchived(null, $boardId)) {
218
			throw new StatusException('Operation not allowed. This board is archived.');
219
		}
220
		$stack = new Stack();
221
		$stack->setTitle($title);
222
		$stack->setBoardId($boardId);
223
		$stack->setOrder($order);
224
		$stack = $this->stackMapper->insert($stack);
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...
225
		$this->activityManager->triggerEvent(
226
			ActivityManager::DECK_OBJECT_BOARD, $stack, ActivityManager::SUBJECT_STACK_CREATE
227
		);
228
		$this->changeHelper->boardChanged($boardId);
229
230
		$this->eventDispatcher->dispatch(
231
			'\OCA\Deck\Stack::onCreate',
232
			new GenericEvent(null, ['id' => $stack->getId(), 'stack' => $stack])
233
		);
234
235
		return $stack;
236
	}
237
238
	/**
239
	 * @param $id
240
	 *
241
	 * @return \OCP\AppFramework\Db\Entity
242
	 * @throws \OCA\Deck\NoPermissionException
243
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
244
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
245
	 * @throws BadRequestException
246
	 */
247 View Code Duplication
	public function delete($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...
248
249
		if (is_numeric($id) === false) {
250
			throw new BadRequestException('stack id must be a number');
251
		}
252
253
		$this->permissionService->checkPermission($this->stackMapper, $id, Acl::PERMISSION_MANAGE);
254
255
		$stack = $this->stackMapper->find($id);
256
		$stack->setDeletedAt(time());
257
		$stack = $this->stackMapper->update($stack);
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...
258
259
		$this->activityManager->triggerEvent(
260
			ActivityManager::DECK_OBJECT_BOARD, $stack, ActivityManager::SUBJECT_STACK_DELETE
261
		);
262
		$this->changeHelper->boardChanged($stack->getBoardId());
263
		$this->enrichStackWithCards($stack);
264
265
		$this->eventDispatcher->dispatch(
266
			'\OCA\Deck\Stack::onDelete', new GenericEvent(null, ['id' => $id, 'stack' => $stack])
267
		);
268
269
		return $stack;
270
	}
271
272
	/**
273
	 * @param $id
274
	 * @param $title
275
	 * @param $boardId
276
	 * @param $order
277
	 * @param $deletedAt
278
	 *
279
	 * @return \OCP\AppFramework\Db\Entity
280
	 * @throws StatusException
281
	 * @throws \OCA\Deck\NoPermissionException
282
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
283
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
284
	 * @throws BadRequestException
285
	 */
286
	public function update($id, $title, $boardId, $order, $deletedAt) {
287
288
		if (is_numeric($id) === false) {
289
			throw new BadRequestException('stack id must be a number');
290
		}
291
292
		if ($title === false || $title === null) {
293
			throw new BadRequestException('title must be provided');
294
		}
295
296
		if (is_numeric($boardId) === false) {
297
			throw new BadRequestException('board id must be a number');
298
		}
299
300
		if (is_numeric($order) === false) {
301
			throw new BadRequestException('order must be a number');
302
		}
303
304
		$this->permissionService->checkPermission($this->stackMapper, $id, Acl::PERMISSION_MANAGE);
305
		if ($this->boardService->isArchived($this->stackMapper, $id)) {
306
			throw new StatusException('Operation not allowed. This board is archived.');
307
		}
308
		$stack = $this->stackMapper->find($id);
309
		$changes = new ChangeSet($stack);
310
		$stack->setTitle($title);
311
		$stack->setBoardId($boardId);
312
		$stack->setOrder($order);
313
		$stack->setDeletedAt($deletedAt);
314
		$changes->setAfter($stack);
315
		$stack = $this->stackMapper->update($stack);
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...
316
		$this->activityManager->triggerUpdateEvents(
317
			ActivityManager::DECK_OBJECT_BOARD, $changes, ActivityManager::SUBJECT_STACK_UPDATE
318
		);
319
		$this->changeHelper->boardChanged($stack->getBoardId());
320
321
		$this->eventDispatcher->dispatch(
322
			'\OCA\Deck\Stack::onUpdate', new GenericEvent(null, ['id' => $id, 'stack' => $stack])
323
		);
324
325
		return $stack;
326
	}
327
328
	/**
329
	 * @param $id
330
	 * @param $order
331
	 *
332
	 * @return array
333
	 * @throws \OCA\Deck\NoPermissionException
334
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
335
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
336
	 * @throws BadRequestException
337
	 */
338
	public function reorder($id, $order) {
339
340
		if (is_numeric($id) === false) {
341
			throw new BadRquestException('id must be a number');
342
		}
343
344
		if ($order === false || $order === null) {
345
			throw new BadRequestException('order must be provided');
346
		}
347
348
		$this->permissionService->checkPermission($this->stackMapper, $id, Acl::PERMISSION_MANAGE);
349
		$stackToSort = $this->stackMapper->find($id);
350
		$stacks = $this->stackMapper->findAll($stackToSort->getBoardId());
351
		$result = [];
352
		$i = 0;
353
		foreach ($stacks as $stack) {
354
			if ($stack->id === $id) {
355
				$stack->setOrder($order);
356
			}
357
358
			if ($i === $order) {
359
				$i++;
360
			}
361
362
			if ($stack->id !== $id) {
363
				$stack->setOrder($i++);
364
			}
365
			$this->stackMapper->update($stack);
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...
366
			$result[$stack->getOrder()] = $stack;
367
		}
368
		$this->changeHelper->boardChanged($stackToSort->getBoardId());
369
370
		return $result;
371
	}
372
}
373