Passed
Pull Request — master (#557)
by Julius
02:24
created

CardService::assignUser()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 4
nc 5
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\Db\AssignedUsers;
27
use OCA\Deck\Db\AssignedUsersMapper;
28
use OCA\Deck\Db\Card;
29
use OCA\Deck\Db\CardMapper;
30
use OCA\Deck\Db\Acl;
31
use OCA\Deck\Db\StackMapper;
32
use OCA\Deck\Notification\NotificationHelper;
33
use OCA\Deck\Db\BoardMapper;
34
use OCA\Deck\Db\LabelMapper;
35
use OCA\Deck\NotFoundException;
36
use OCA\Deck\StatusException;
37
38
39
class CardService {
40
41
	private $cardMapper;
42
	private $stackMapper;
43
	private $boardMapper;
44
	private $labelMapper;
45
	private $permissionService;
46
	private $boardService;
47
	private $notificationHelper;
48
	private $assignedUsersMapper;
49
	private $attachmentService;
50
	private $currentUser;
51
52 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...
53
		CardMapper $cardMapper,
54
		StackMapper $stackMapper,
55
		BoardMapper $boardMapper,
56
		LabelMapper $labelMapper,
57
		PermissionService $permissionService, 
58
		BoardService $boardService,
59
		NotificationHelper $notificationHelper,
60
		AssignedUsersMapper $assignedUsersMapper, 
61
		AttachmentService $attachmentService,
62
		$userId
63
	) {
64
		$this->cardMapper = $cardMapper;
65
		$this->stackMapper = $stackMapper;
66
		$this->boardMapper = $boardMapper;
67
		$this->labelMapper = $labelMapper;
68
		$this->permissionService = $permissionService;
69
		$this->boardService = $boardService;
70
		$this->notificationHelper = $notificationHelper;
71
		$this->assignedUsersMapper = $assignedUsersMapper;
72
		$this->attachmentService = $attachmentService;
73
		$this->currentUser = $userId;
74
	}
75
76
	public function enrich($card) {
77
		$cardId = $card->getId();
78
		$card->setAssignedUsers($this->assignedUsersMapper->find($cardId));
79
		$card->setLabels($this->labelMapper->findAssignedLabelsForCard($cardId));
80
		$card->setAttachmentCount($this->attachmentService->count($cardId));
81
	}
82
83 View Code Duplication
	public function fetchDeleted($boardId) {
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...
84
		$this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_READ);
85
		$cards = $this->cardMapper->findDeleted($boardId);
86
		foreach ($cards as $card) {
87
			$this->enrich($card);
88
		}
89
		return $cards;
90
	}
91
92
	public function find($cardId) {
93
		$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_READ);
94
		$card = $this->cardMapper->find($cardId);
95
		$assignedUsers = $this->assignedUsersMapper->find($card->getId());
96
		$attachments = $this->attachmentService->findAll($cardId, true);
97
		$card->setAssignedUsers($assignedUsers);
98
		$card->setAttachments($attachments);
99
		return $card;
100
	}
101
102
	/**
103
	 * @param integer $order
104
	 */
105 View Code Duplication
	public function create($title, $stackId, $type, $order, $owner) {
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...
106
		$this->permissionService->checkPermission($this->stackMapper, $stackId, Acl::PERMISSION_EDIT);
107
		if ($this->boardService->isArchived($this->stackMapper, $stackId)) {
108
			throw new StatusException('Operation not allowed. This board is archived.');
109
		}
110
		$card = new Card();
111
		$card->setTitle($title);
112
		$card->setStackId($stackId);
113
		$card->setType($type);
114
		$card->setOrder($order);
115
		$card->setOwner($owner);
116
		return $this->cardMapper->insert($card);
117
118
	}
119
120
	public function delete($id) {
121
		$this->permissionService->checkPermission($this->cardMapper, $id, Acl::PERMISSION_EDIT);
122
		if ($this->boardService->isArchived($this->cardMapper, $id)) {
123
			throw new StatusException('Operation not allowed. This board is archived.');
124
		}
125
		$card = $this->cardMapper->find($id);
126
		$card->setDeletedAt(time());
127
		$this->cardMapper->update($card);
128
		return $card;
129
	}
130
131
	public function update($id, $title, $stackId, $type, $order, $description, $owner, $duedate, $deletedAt) {
132
		$this->permissionService->checkPermission($this->cardMapper, $id, Acl::PERMISSION_EDIT);
133
		if ($this->boardService->isArchived($this->cardMapper, $id)) {
134
			throw new StatusException('Operation not allowed. This board is archived.');
135
		}
136
		$card = $this->cardMapper->find($id);
137
		if ($card->getArchived()) {
138
			throw new StatusException('Operation not allowed. This card is archived.');
139
		}
140
		$card->setTitle($title);
141
		$card->setStackId($stackId);
142
		$card->setType($type);
143
		$card->setOrder($order);
144
		$card->setOwner($owner);
145
		$card->setDescription($description);
146
		$card->setDuedate($duedate);
147
		$card->setDeletedAt($deletedAt);
148
		return $this->cardMapper->update($card);
149
	}
150
151
	public function rename($id, $title) {
152
		$this->permissionService->checkPermission($this->cardMapper, $id, Acl::PERMISSION_EDIT);
153
		if ($this->boardService->isArchived($this->cardMapper, $id)) {
154
			throw new StatusException('Operation not allowed. This board is archived.');
155
		}
156
		$card = $this->cardMapper->find($id);
157
		if ($card->getArchived()) {
158
			throw new StatusException('Operation not allowed. This card is archived.');
159
		}
160
		$card->setTitle($title);
161
		return $this->cardMapper->update($card);
162
	}
163
164
	public function reorder($id, $stackId, $order) {
165
		$this->permissionService->checkPermission($this->cardMapper, $id, Acl::PERMISSION_EDIT);
166
		if ($this->boardService->isArchived($this->cardMapper, $id)) {
167
			throw new StatusException('Operation not allowed. This board is archived.');
168
		}
169
		$cards = $this->cardMapper->findAll($stackId);
170
		$result = [];
171
		$i = 0;
172
		foreach ($cards as $card) {
173
			if ($card->getArchived()) {
174
				throw new StatusException('Operation not allowed. This card is archived.');
175
			}
176
			if ($card->id === $id) {
177
				$card->setOrder($order);
178
				$card->setLastModified(time());
179
			}
180
181
			if ($i === $order) {
182
				$i++;
183
			}
184
185
			if ($card->id !== $id) {
186
				$card->setOrder($i++);
187
			}
188
			$this->cardMapper->update($card);
189
			$result[$card->getOrder()] = $card;
190
		}
191
192
		return $result;
193
	}
194
195 View Code Duplication
	public function archive($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...
196
		$this->permissionService->checkPermission($this->cardMapper, $id, Acl::PERMISSION_EDIT);
197
		if ($this->boardService->isArchived($this->cardMapper, $id)) {
198
			throw new StatusException('Operation not allowed. This board is archived.');
199
		}
200
		$card = $this->cardMapper->find($id);
201
		$card->setArchived(true);
202
		return $this->cardMapper->update($card);
203
	}
204
205 View Code Duplication
	public function unarchive($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...
206
		$this->permissionService->checkPermission($this->cardMapper, $id, Acl::PERMISSION_EDIT);
207
		if ($this->boardService->isArchived($this->cardMapper, $id)) {
208
			throw new StatusException('Operation not allowed. This board is archived.');
209
		}
210
		$card = $this->cardMapper->find($id);
211
		$card->setArchived(false);
212
		return $this->cardMapper->update($card);
213
	}
214
215 View Code Duplication
	public function assignLabel($cardId, $labelId) {
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...
216
		$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);
217
		if ($this->boardService->isArchived($this->cardMapper, $cardId)) {
218
			throw new StatusException('Operation not allowed. This board is archived.');
219
		}
220
		$card = $this->cardMapper->find($cardId);
221
		if ($card->getArchived()) {
222
			throw new StatusException('Operation not allowed. This card is archived.');
223
		}
224
		$this->cardMapper->assignLabel($cardId, $labelId);
225
	}
226
227 View Code Duplication
	public function removeLabel($cardId, $labelId) {
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...
228
		$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);
229
		if ($this->boardService->isArchived($this->cardMapper, $cardId)) {
230
			throw new StatusException('Operation not allowed. This board is archived.');
231
		}
232
		$card = $this->cardMapper->find($cardId);
233
		if ($card->getArchived()) {
234
			throw new StatusException('Operation not allowed. This card is archived.');
235
		}
236
		$this->cardMapper->removeLabel($cardId, $labelId);
237
	}
238
239
	public function assignUser($cardId, $userId) {
240
		$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);
241
		$assignments = $this->assignedUsersMapper->find($cardId);
242
		foreach ($assignments as $assignment) {
243
			if ($assignment->getParticipant() === $userId) {
244
				return false;
245
			}
246
		}
247
248
		if ($userId !== $this->currentUser) {
249
			/* Notifyuser about the card assignment */
250
			$card = $this->cardMapper->find($cardId);
251
			$this->notificationHelper->sendCardAssigned($card, $userId);
252
		}
253
254
		$assignment = new AssignedUsers();
255
		$assignment->setCardId($cardId);
256
		$assignment->setParticipant($userId);
257
		return $this->assignedUsersMapper->insert($assignment);
258
	}
259
260
	public function unassignUser($cardId, $userId) {
261
		$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);
262
		$assignments = $this->assignedUsersMapper->find($cardId);
263
		foreach ($assignments as $assignment) {
264
			if ($assignment->getParticipant() === $userId) {
265
				return $this->assignedUsersMapper->delete($assignment);
266
			}
267
		}
268
		throw new NotFoundException('No assignment for ' . $userId . 'found.');
269
	}
270
}
271