Completed
Push — master ( 172550...380074 )
by Sander
10s
created

NotebookService   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 110
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A find() 0 3 1
A findByName() 0 3 1
A create() 0 15 3
A update() 0 16 3
A delete() 0 13 3
A checkPermissions() 0 3 1
1
<?php
2
/**
3
 * Nextcloud - namespace OCA\Nextnote
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
6
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
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\NextNote\Service;
25
26
use OCA\NextNote\Db\Notebook;
27
use OCA\NextNote\Db\NotebookMapper;
28
use OCA\NextNote\ShareBackend\NextNoteShareBackend;
29
use OCA\NextNote\Utility\Utils;
30
use OCP\AppFramework\Db\Entity;
31
32
33
class NotebookService {
34
35
	private $groupMapper;
36
	private $utils;
37
	private $sharing;
38
39
	public function __construct(NotebookMapper $groupMapper, Utils $utils, NextNoteShareBackend $shareBackend) {
40
		$this->groupMapper = $groupMapper;
41
		$this->utils = $utils;
42
		$this->sharing = $shareBackend;
43
	}
44
45
	/**
46
	 * Find a group by id
47
	 *
48
	 * @param $notebook_id
49
	 * @param null $user_id
50
	 * @param int|bool $deleted
51
	 * @return Notebook[]|Notebook
52
	 */
53
	public function find($notebook_id=null, $user_id = null, $deleted = false) {
54
		return $this->groupMapper->find($notebook_id, $user_id, $deleted);
55
	}
56
	/**
57
	 * Find a group by name
58
	 *
59
	 * @param $group_name string
60
	 * @param null $user_id
61
	 * @param bool $deleted
62
	 * @return Notebook[]|Notebook
63
	 */
64
	public function findByName($group_name=null, $user_id = null, $deleted = false) {
65
		return $this->groupMapper->findByName($group_name, $user_id, $deleted);
66
	}
67
68
	/**
69
	 * Creates a group
70
	 *
71
	 * @param array|Notebook $group
72
	 * @param $userId
73
	 * @return Notebook|Entity
74
	 * @throws \Exception
75
	 */
76
	public function create($group, $userId) {
77
		if (is_array($group)) {
78
			$entity = new Notebook();
79
			$entity->setName($group['name']);
80
			$entity->setParentId($group['parent_id']);
81
			$entity->setUid($userId);
82
			$entity->setGuid($group['guid']);
83
			$entity->setColor($group['color']);
84
			$group = $entity;
85
		}
86
		if (!$group instanceof Notebook) {
87
			throw new \Exception("Expected Note object!");
88
		}
89
		return $this->groupMapper->insert($group);
90
	}
91
92
	/**
93
	 * Update a group
94
	 *
95
	 * @param $group array|Notebook
96
	 * @return Notebook|Entity|bool
97
	 * @throws \Exception
98
	 * @internal param $userId
99
	 * @internal param $vault
100
	 */
101
	public function update($group) {
102
103
		if (is_array($group)) {
104
			$entity = $this->find($group['id']);
105
			$entity->setName($group['title']);
106
			$entity->setParentId($group['parent_id']);
107
			$entity->setColor($group['color']);
108
			$group = $entity;
109
		}
110
111
		if (!$group instanceof Notebook) {
112
			throw new \Exception("Expected Note object!");
113
		}
114
115
		return $this->groupMapper->update($group);
116
	}
117
118
	/**
119
	 * Delete a group
120
	 *
121
	 * @param $group_id
122
	 * @param string $user_id
123
	 * @return bool
124
	 */
125
	public function delete($group_id, $user_id = null) {
126
		if (!$this->checkPermissions()) {
127
			return false;
128
		}
129
130
		$group = $this->groupMapper->find($group_id, $user_id);
131
		if ($group instanceof Notebook) {
132
			$this->groupMapper->delete($group);
133
			return true;
134
		} else {
135
			return false;
136
		}
137
	}
138
139
	private function checkPermissions() {
140
		return true;
141
	}
142
}
143