NotebookService   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 134
Duplicated Lines 11.19 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findNotebooksFromUser() 15 15 2
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

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Nextcloud - namespace OCA\Nextnote
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([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\NextNote\Service;
25
26
use OCA\NextNote\Db\Notebook;
27
use OCA\NextNote\Db\NotebookMapper;
28
use OCA\NextNote\Utility\Utils;
29
use OCP\AppFramework\Db\Entity;
30
31
32
class NotebookService {
33
34
	private $notebookMapper;
35
	private $utils;
36
37
	public function __construct(NotebookMapper $notebookMapper, Utils $utils) {
38
		$this->notebookMapper = $notebookMapper;
39
		$this->utils = $utils;
40
	}
41
42
43
44
	/**
45
	 * Get notebooks from a user.
46
	 *
47
	 * @param $userId
48
	 * @param int|bool $deleted
49
	 * @return Notebook[]
50
	 */
51 View Code Duplication
	public function findNotebooksFromUser($userId, $deleted = false) {
52
		// Get shares
53
		$dbNotebooks = $this->notebookMapper->find(null, $userId, $deleted);
54
		$n = $dbNotebooks;
55
		if($dbNotebooks instanceof Notebook){
56
			$dbNotebooks = [];
57
			/**
58
			 * @var $result Notebook
59
			 */
60
			$dbNotebooks[$n->getId()] = $n;
61
		}
62
		$sharedNotebooks = [];
63
		$notebooks = array_merge($dbNotebooks, $sharedNotebooks);
64
		return $notebooks;
65
	}
66
67
68
	/**
69
	 * Find a notebook by id
70
	 *
71
	 * @param null|int $notebook_id
72
	 * @param null $user_id
73
	 * @param int|bool $deleted
74
	 * @return Notebook[]|Notebook
75
	 */
76
	public function find($notebook_id=null, $user_id = null, $deleted = false) {
77
		return $this->notebookMapper->find($notebook_id, $user_id, $deleted);
78
	}
79
	/**
80
	 * Find a notebook by name
81
	 *
82
	 * @param $notebook_name string
83
	 * @param null $user_id
84
	 * @param bool $deleted
85
	 * @return Notebook[]|Notebook
86
	 */
87
	public function findByName($notebook_name=null, $user_id = null, $deleted = false) {
88
		return $this->notebookMapper->findByName($notebook_name, $user_id, $deleted);
89
	}
90
91
	/**
92
	 * Creates a notebook
93
	 *
94
	 * @param array|Notebook $notebook
95
	 * @param $userId
96
	 * @return Notebook|Entity
97
	 * @throws \Exception
98
	 */
99
	public function create($notebook, $userId) {
100
		if (is_array($notebook)) {
101
			$entity = new Notebook();
102
			$entity->setName($notebook['name']);
103
			$entity->setParentId($notebook['parent_id']);
104
			$entity->setUid($userId);
105
			$entity->setGuid($notebook['guid']);
106
			$entity->setColor($notebook['color']);
107
			$notebook = $entity;
108
		}
109
		if (!$notebook instanceof Notebook) {
110
			throw new \Exception("Expected Notebook object!");
111
		}
112
		return $this->notebookMapper->insert($notebook);
113
	}
114
115
	/**
116
	 * Update a notebook
117
	 *
118
	 * @param $notebook array|Notebook
119
	 * @return Notebook|Entity|bool
120
	 * @throws \Exception
121
	 * @internal param $userId
122
	 * @internal param $vault
123
	 */
124
	public function update($notebook) {
125
126
		if (is_array($notebook)) {
127
			$entity = $this->find($notebook['id']);
128
			$entity->setName($notebook['title']);
129
			$entity->setParentId($notebook['parent_id']);
130
			$entity->setColor($notebook['color']);
131
			$notebook = $entity;
132
		}
133
134
		if (!$notebook instanceof Notebook) {
135
			throw new \Exception("Expected Notebook object!");
136
		}
137
138
		return $this->notebookMapper->update($notebook);
139
	}
140
141
	/**
142
	 * Delete a notebook
143
	 *
144
	 * @param $notebook_id
145
	 * @param string $user_id
146
	 * @return bool
147
	 */
148
	public function delete($notebook_id, $user_id = null) {
149
		if (!$this->checkPermissions()) {
150
			return false;
151
		}
152
153
		$notebook = $this->notebookMapper->find($notebook_id, $user_id);
154
		if ($notebook instanceof Notebook) {
155
			$this->notebookMapper->delete($notebook);
156
			return true;
157
		} else {
158
			return false;
159
		}
160
	}
161
162
	private function checkPermissions() {
163
		return true;
164
	}
165
}
166