BoardService::create()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 20
nc 2
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\Acl;
27
use OCA\Deck\Db\AclMapper;
28
use OCA\Deck\Db\Label;
29
30
31
use OCP\IL10N;
32
33
use OCA\Deck\Db\Board;
34
use OCA\Deck\Db\BoardMapper;
35
use OCA\Deck\Db\LabelMapper;
36
37
38
class BoardService {
39
40
	private $boardMapper;
41
	private $labelMapper;
42
	private $aclMapper;
43
	private $l10n;
44
	private $permissionService;
45
46
	public function __construct(BoardMapper $boardMapper, IL10N $l10n, LabelMapper $labelMapper, AclMapper $aclMapper, PermissionService $permissionService) {
47
		$this->boardMapper = $boardMapper;
48
		$this->labelMapper = $labelMapper;
49
		$this->aclMapper = $aclMapper;
50
		$this->l10n = $l10n;
51
		$this->permissionService = $permissionService;
52
	}
53
54
	public function findAll($userInfo) {
55
		$userBoards = $this->boardMapper->findAllByUser($userInfo['user']);
56
		$groupBoards = $this->boardMapper->findAllByGroups($userInfo['user'], $userInfo['groups']);
57
		$complete = array_merge($userBoards, $groupBoards);
58
		return array_map("unserialize", array_unique(array_map("serialize", $complete)));
59
	}
60
61
	public function find($boardId) {
62
		$this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_READ);
63
		return $this->boardMapper->find($boardId, true, true);
64
	}
65
66
	public function create($title, $userId, $color) {
67
		$board = new Board();
68
		$board->setTitle($title);
69
		$board->setOwner($userId);
70
		$board->setColor($color);
71
		$new_board = $this->boardMapper->insert($board);
72
73
		// create new labels
74
		$default_labels = [
75
			'31CC7C' => $this->l10n->t('Finished'),
76
			'317CCC' => $this->l10n->t('To review'),
77
			'FF7A66' => $this->l10n->t('Action needed'),
78
			'F1DB50' => $this->l10n->t('Later')
79
		];
80
		$labels = [];
81
		foreach ($default_labels as $color => $title) {
82
			$label = new Label();
83
			$label->setColor($color);
84
			$label->setTitle($title);
85
			$label->setBoardId($new_board->getId());
86
			$labels[] = $this->labelMapper->insert($label);
87
		}
88
		$new_board->setLabels($labels);
89
		return $new_board;
90
91
	}
92
93
	public function delete($id) {
94
		$this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_READ);
95
		return $this->boardMapper->delete($this->find($id));
96
	}
97
98
	public function update($id, $title, $color) {
99
		$this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_MANAGE);
100
		$board = $this->find($id);
101
		$board->setTitle($title);
102
		$board->setColor($color);
103
		return $this->boardMapper->update($board);
104
	}
105
106
107
	public function addAcl($boardId, $type, $participant, $edit, $share, $manage) {
108
		$this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_SHARE);
109
		$acl = new Acl();
110
		$acl->setBoardId($boardId);
111
		$acl->setType($type);
112
		$acl->setParticipant($participant);
113
		$acl->setPermissionEdit($edit);
114
		$acl->setPermissionShare($share);
115
		$acl->setPermissionManage($manage);
116
		return $this->aclMapper->insert($acl);
117
	}
118
119
	public function updateAcl($id, $edit, $share, $manage) {
120
		$this->permissionService->checkPermission($this->aclMapper, $id, Acl::PERMISSION_SHARE);
121
		$acl = $this->aclMapper->find($id);
122
		$acl->setPermissionEdit($edit);
123
		$acl->setPermissionShare($share);
124
		$acl->setPermissionManage($manage);
125
		return $this->aclMapper->update($acl);
126
	}
127
128
	public function deleteAcl($id) {
129
		$this->permissionService->checkPermission($this->aclMapper, $id, Acl::PERMISSION_SHARE);
130
		$acl = $this->aclMapper->find($id);
131
		return $this->aclMapper->delete($acl);
132
	}
133
134
}