Completed
Push — master ( 3e4ded...a3a70f )
by Julius
11s
created

StackService::enrichStacksWithCards()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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\Acl;
27
use OCA\Deck\Db\CardMapper;
28
use OCA\Deck\Db\BoardMapper;
29
use OCA\Deck\Db\LabelMapper;
30
use OCA\Deck\Db\AssignedUsersMapper;
31
use OCA\Deck\Db\Stack;
32
use OCA\Deck\Db\StackMapper;
33
use OCA\Deck\StatusException;
34
use OCP\ICache;
35
use OCP\ICacheFactory;
36
37
38
class StackService {
39
40
	private $stackMapper;
41
	private $cardMapper;
42
	private $boardMapper;
43
	private $labelMapper;
44
	private $permissionService;
45
	private $boardService;
46
	private $cardService;
47
	private $assignedUsersMapper;
48
	private $attachmentService;
49
50 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...
51
		StackMapper $stackMapper,
52
		BoardMapper $boardMapper,
53
		CardMapper $cardMapper,
54
		LabelMapper $labelMapper,
55
		PermissionService $permissionService,
56
		BoardService $boardService,
57
		CardService $cardService,
58
		AssignedUsersMapper $assignedUsersMapper,
59
		AttachmentService $attachmentService
60
	) {
61
		$this->stackMapper = $stackMapper;
62
		$this->boardMapper = $boardMapper;
63
		$this->cardMapper = $cardMapper;
64
		$this->labelMapper = $labelMapper;
65
		$this->permissionService = $permissionService;
66
		$this->boardService = $boardService;
67
		$this->cardService = $cardService;
68
		$this->assignedUsersMapper = $assignedUsersMapper;
69
		$this->attachmentService = $attachmentService;
70
	}
71
72
	private function enrichStackWithCards($stack) {
73
		$cards = $this->cardMapper->findAll($stack->id);
74
75
		if(is_null($cards)) {
76
			return;
77
		}
78
79
		foreach ($cards as $card) {
80
			$this->cardService->enrich($card);
81
		}
82
83
		$stack->setCards($cards);
84
	}
85
86
	private function enrichStacksWithCards($stacks) {
87
		foreach ($stacks as $stack) {
88
			$this->enrichStackWithCards($stack);
89
		}
90
	}
91
92
	public function findAll($boardId) {
93
		$this->permissionService->checkPermission(null, $boardId, Acl::PERMISSION_READ);
94
		$stacks = $this->stackMapper->findAll($boardId);
95
		$this->enrichStacksWithCards($stacks);
96
		return $stacks;
97
	}
98
99
	public function fetchDeleted($boardId) {
100
		$this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_READ);
101
		$stacks = $this->stackMapper->findDeleted($boardId);
102
		$this->enrichStacksWithCards($stacks);
103
		return $stacks;
104
	}
105
106
	public function findAllArchived($boardId) {
107
		$this->permissionService->checkPermission(null, $boardId, Acl::PERMISSION_READ);
108
		$stacks = $this->stackMapper->findAll($boardId);
109
		$labels = $this->labelMapper->getAssignedLabelsForBoard($boardId);
110
		foreach ($stacks as $stackIndex => $stack) {
111
			$cards = $this->cardMapper->findAllArchived($stack->id);
112
			foreach ($cards as $cardIndex => $card) {
113
				if (array_key_exists($card->id, $labels)) {
114
					$cards[$cardIndex]->setLabels($labels[$card->id]);
115
				}
116
			}
117
			$stacks[$stackIndex]->setCards($cards);
118
		}
119
		return $stacks;
120
	}
121
122
	/**
123
	 * @param integer $order
124
	 */
125 View Code Duplication
	public function create($title, $boardId, $order) {
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...
126
		$this->permissionService->checkPermission(null, $boardId, Acl::PERMISSION_MANAGE);
127
		if ($this->boardService->isArchived(null, $boardId)) {
128
			throw new StatusException('Operation not allowed. This board is archived.');
129
		}
130
		$stack = new Stack();
131
		$stack->setTitle($title);
132
		$stack->setBoardId($boardId);
133
		$stack->setOrder($order);
134
		return $this->stackMapper->insert($stack);
135
136
	}
137
138
	public function delete($id) {
139
		$this->permissionService->checkPermission($this->stackMapper, $id, Acl::PERMISSION_MANAGE);
140
141
		$stack = $this->stackMapper->find($id);
142
		$stack->setDeletedAt(time());
143
		$this->stackMapper->update($stack);
144
145
		$this->enrichStackWithCards($stack);
146
147
		return $stack;
148
	}
149
150
151 View Code Duplication
	public function update($id, $title, $boardId, $order, $deletedAt) {
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...
152
		$this->permissionService->checkPermission($this->stackMapper, $id, Acl::PERMISSION_MANAGE);
153
		if ($this->boardService->isArchived($this->stackMapper, $id)) {
154
			throw new StatusException('Operation not allowed. This board is archived.');
155
		}
156
		$stack = $this->stackMapper->find($id);
157
		$stack->setTitle($title);
158
		$stack->setBoardId($boardId);
159
		$stack->setOrder($order);
160
		$stack->setDeletedAt($deletedAt);
161
		return $this->stackMapper->update($stack);
162
	}
163
164
	public function reorder($id, $order) {
165
		$this->permissionService->checkPermission($this->stackMapper, $id, Acl::PERMISSION_EDIT);
166
		$stackToSort = $this->stackMapper->find($id);
167
		$stacks = $this->stackMapper->findAll($stackToSort->getBoardId());
168
		$result = [];
169
		$i = 0;
170
		foreach ($stacks as $stack) {
171
			if ($stack->id === $id) {
172
				$stack->setOrder($order);
173
			}
174
175
			if ($i === $order) {
176
				$i++;
177
			}
178
179
			if ($stack->id !== $id) {
180
				$stack->setOrder($i++);
181
			}
182
			$this->stackMapper->update($stack);
183
			$result[$stack->getOrder()] = $stack;
184
		}
185
186
		return $result;
187
	}
188
}
189