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\ChangeHelper; |
27
|
|
|
use OCA\Deck\Db\Label; |
28
|
|
|
use OCA\Deck\Db\Acl; |
29
|
|
|
use OCA\Deck\Db\LabelMapper; |
30
|
|
|
use OCA\Deck\StatusException; |
31
|
|
|
use OCA\Deck\BadRequestException; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
class LabelService { |
35
|
|
|
|
36
|
|
|
/** @var LabelMapper */ |
37
|
|
|
private $labelMapper; |
38
|
|
|
/** @var PermissionService */ |
39
|
|
|
private $permissionService; |
40
|
|
|
/** @var BoardService */ |
41
|
|
|
private $boardService; |
42
|
|
|
/** @var ChangeHelper */ |
43
|
|
|
private $changeHelper; |
44
|
|
|
|
45
|
|
|
public function __construct(LabelMapper $labelMapper, PermissionService $permissionService, BoardService $boardService, ChangeHelper $changeHelper) { |
46
|
|
|
$this->labelMapper = $labelMapper; |
47
|
|
|
$this->permissionService = $permissionService; |
48
|
|
|
$this->boardService = $boardService; |
49
|
|
|
$this->changeHelper = $changeHelper; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param $labelId |
54
|
|
|
* @return \OCP\AppFramework\Db\Entity |
55
|
|
|
* @throws \OCA\Deck\NoPermissionException |
56
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException |
57
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
58
|
|
|
* @throws BadRequestException |
59
|
|
|
*/ |
60
|
|
|
public function find($labelId) { |
61
|
|
|
if (is_numeric($labelId) === false) { |
62
|
|
|
throw new BadRequestException('label id must be a number'); |
63
|
|
|
} |
64
|
|
|
$this->permissionService->checkPermission($this->labelMapper, $labelId, Acl::PERMISSION_READ); |
65
|
|
|
return $this->labelMapper->find($labelId); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $title |
70
|
|
|
* @param $color |
71
|
|
|
* @param $boardId |
72
|
|
|
* @return \OCP\AppFramework\Db\Entity |
73
|
|
|
* @throws StatusException |
74
|
|
|
* @throws \OCA\Deck\NoPermissionException |
75
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException |
76
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
77
|
|
|
* @throws BadRequestException |
78
|
|
|
*/ |
79
|
|
|
public function create($title, $color, $boardId) { |
80
|
|
|
|
81
|
|
|
if ($title === false || $title === null) { |
82
|
|
|
throw new BadRequestException('title must be provided'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($color === false || $color === null) { |
86
|
|
|
throw new BadRequestException('color must be provided'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (is_numeric($boardId) === false) { |
90
|
|
|
throw new BadRequestException('board id must be a number'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->permissionService->checkPermission(null, $boardId, Acl::PERMISSION_MANAGE); |
94
|
|
|
|
95
|
|
|
$boardLabels = $this->labelMapper->findAll($boardId); |
96
|
|
|
if (\is_array($boardLabels)) { |
97
|
|
|
foreach($boardLabels as $boardLabel) { |
98
|
|
|
if ($boardLabel->getTitle() === $title) { |
99
|
|
|
throw new BadRequestException('title must be unique'); |
100
|
|
|
break; |
|
|
|
|
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($this->boardService->isArchived(null, $boardId)) { |
106
|
|
|
throw new StatusException('Operation not allowed. This board is archived.'); |
107
|
|
|
} |
108
|
|
|
$label = new Label(); |
109
|
|
|
$label->setTitle($title); |
110
|
|
|
$label->setColor($color); |
111
|
|
|
$label->setBoardId($boardId); |
112
|
|
|
$this->changeHelper->boardChanged($boardId); |
113
|
|
|
return $this->labelMapper->insert($label); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param $id |
118
|
|
|
* @return \OCP\AppFramework\Db\Entity |
119
|
|
|
* @throws StatusException |
120
|
|
|
* @throws \OCA\Deck\NoPermissionException |
121
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException |
122
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
123
|
|
|
* @throws BadRequestException |
124
|
|
|
*/ |
125
|
|
|
public function delete($id) { |
126
|
|
|
|
127
|
|
|
if (is_numeric($id) === false) { |
128
|
|
|
throw new BadRequestException('label id must be a number'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->permissionService->checkPermission($this->labelMapper, $id, Acl::PERMISSION_MANAGE); |
132
|
|
|
if ($this->boardService->isArchived($this->labelMapper, $id)) { |
133
|
|
|
throw new StatusException('Operation not allowed. This board is archived.'); |
134
|
|
|
} |
135
|
|
|
$label = $this->labelMapper->delete($this->find($id)); |
136
|
|
|
$this->changeHelper->boardChanged($label->getBoardId()); |
137
|
|
|
return $label; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param $id |
142
|
|
|
* @param $title |
143
|
|
|
* @param $color |
144
|
|
|
* @return \OCP\AppFramework\Db\Entity |
145
|
|
|
* @throws StatusException |
146
|
|
|
* @throws \OCA\Deck\NoPermissionException |
147
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException |
148
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
149
|
|
|
* @throws BadRequestException |
150
|
|
|
*/ |
151
|
|
|
public function update($id, $title, $color) { |
152
|
|
|
|
153
|
|
|
if (is_numeric($id) === false) { |
154
|
|
|
throw new BadRequestException('label id must be a number'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if ($title === false || $title === null || $title === "") { |
158
|
|
|
throw new BadRequestException('title must be provided'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if ($color === false || $color === null) { |
162
|
|
|
throw new BadRequestException('color must be provided'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$this->permissionService->checkPermission($this->labelMapper, $id, Acl::PERMISSION_MANAGE); |
166
|
|
|
|
167
|
|
|
$label = $this->find($id); |
168
|
|
|
|
169
|
|
|
$boardLabels = $this->labelMapper->findAll($label->getBoardId()); |
170
|
|
|
if (\is_array($boardLabels)) { |
171
|
|
|
foreach($boardLabels as $boardLabel) { |
172
|
|
|
if ($boardLabel->getId() === $label->getId()) { |
173
|
|
|
continue; |
174
|
|
|
} |
175
|
|
|
if ($boardLabel->getTitle() === $title) { |
176
|
|
|
throw new BadRequestException('title must be unique'); |
177
|
|
|
break; |
|
|
|
|
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if ($this->boardService->isArchived($this->labelMapper, $id)) { |
183
|
|
|
throw new StatusException('Operation not allowed. This board is archived.'); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$label->setTitle($title); |
187
|
|
|
$label->setColor($color); |
188
|
|
|
$this->changeHelper->boardChanged($label->getBoardId()); |
189
|
|
|
return $this->labelMapper->update($label); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.