Completed
Pull Request — master (#535)
by Julius
02:47
created

LabelService::create()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 24

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
dl 24
loc 24
rs 8.6026
c 0
b 0
f 0
cc 7
nc 5
nop 3
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\Label;
27
use OCA\Deck\Db\Acl;
28
use OCA\Deck\Db\LabelMapper;
29
use OCA\Deck\StatusException;
30
use OCA\Deck\BadRequestException;
31
32
33
class LabelService {
34
35
	/** @var LabelMapper */
36
	private $labelMapper;
37
	/** @var PermissionService */
38
	private $permissionService;
39
	/** @var BoardService */
40
	private $boardService;
41
42
	public function __construct(LabelMapper $labelMapper, PermissionService $permissionService, BoardService $boardService) {
43
		$this->labelMapper = $labelMapper;
44
		$this->permissionService = $permissionService;
45
		$this->boardService = $boardService;
46
	}
47
48
	/**
49
	 * @param $labelId
50
	 * @return \OCP\AppFramework\Db\Entity
51
	 * @throws \OCA\Deck\NoPermissionException
52
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
53
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
54
	 * @throws BadRequestException
55
	 */
56
	public function find($labelId) {
57
		if (is_numeric($labelId) === false) {
58
			throw new BadRequestException('label id must be a number');
59
		}
60
		$this->permissionService->checkPermission($this->labelMapper, $labelId, Acl::PERMISSION_READ);
61
		return $this->labelMapper->find($labelId);
62
	}
63
64
	/**
65
	 * @param $title
66
	 * @param $color
67
	 * @param $boardId
68
	 * @return \OCP\AppFramework\Db\Entity
69
	 * @throws StatusException
70
	 * @throws \OCA\Deck\NoPermissionException
71
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
72
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
73
	 * @throws BadRequestException
74
	 */
75 View Code Duplication
	public function create($title, $color, $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...
76
77
		if ($title === false || $title === null) {
78
			throw new BadRequestException('title must be provided');
79
		}
80
81
		if ($color === false || $color === null) {
82
			throw new BadRequestException('color must be provided');
83
		}
84
85
		if (is_numeric($boardId) === false) {
86
			throw new BadRequestException('board id must be a number');
87
		}
88
89
		$this->permissionService->checkPermission(null, $boardId, Acl::PERMISSION_MANAGE);
90
		if ($this->boardService->isArchived(null, $boardId)) {
91
			throw new StatusException('Operation not allowed. This board is archived.');
92
		}
93
		$label = new Label();
94
		$label->setTitle($title);
95
		$label->setColor($color);
96
		$label->setBoardId($boardId);
97
		return $this->labelMapper->insert($label);
98
	}
99
100
	/**
101
	 * @param $id
102
	 * @return \OCP\AppFramework\Db\Entity
103
	 * @throws StatusException
104
	 * @throws \OCA\Deck\NoPermissionException
105
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
106
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
107
	 * @throws BadRequestException
108
	 */
109
	public function delete($id) {
110
111
		if (is_numeric($id) === false) {
112
			throw new BadRequestException('label id must be a number');
113
		}
114
115
		$this->permissionService->checkPermission($this->labelMapper, $id, Acl::PERMISSION_MANAGE);
116
		if ($this->boardService->isArchived($this->labelMapper, $id)) {
117
			throw new StatusException('Operation not allowed. This board is archived.');
118
		}
119
		return $this->labelMapper->delete($this->find($id));
120
	}
121
122
	/**
123
	 * @param $id
124
	 * @param $title
125
	 * @param $color
126
	 * @return \OCP\AppFramework\Db\Entity
127
	 * @throws StatusException
128
	 * @throws \OCA\Deck\NoPermissionException
129
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
130
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
131
	 * @throws BadRequestException
132
	 */
133
	public function update($id, $title, $color) {
134
135
		if (is_numeric($id) === false) {
136
			throw new BadRequestException('label id must be a number');
137
		}
138
139
		if ($title === false || $title === null) {
140
			throw new BadRequestException('title must be provided');
141
		}
142
143
		if ($color === false || $color === null) {
144
			throw new BadRequestException('color must be provided');
145
		}
146
147
		$this->permissionService->checkPermission($this->labelMapper, $id, Acl::PERMISSION_MANAGE);
148
		if ($this->boardService->isArchived($this->labelMapper, $id)) {
149
			throw new StatusException('Operation not allowed. This board is archived.');
150
		}
151
		$label = $this->find($id);
152
		$label->setTitle($title);
153
		$label->setColor($color);
154
		return $this->labelMapper->update($label);
155
	}
156
157
}