NoteService   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 118
Duplicated Lines 16.1 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 19
loc 118
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createExampleNote() 0 9 1
A __construct() 0 5 1
A findNotesFromUser() 19 19 2
A find() 0 4 1
A create() 0 7 2
A update() 0 7 2
A delete() 0 9 2

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\Note;
28
use OCA\NextNote\Fixtures\ExampleNote;
29
use OCA\NextNote\Utility\Utils;
30
use OCA\NextNote\Db\NoteMapper;
31
32
33
class NoteService {
34
35
	private $noteMapper;
36
	private $utils;
37
	private $groupService;
38
39
	public function __construct(NoteMapper $noteMapper, Utils $utils,NotebookService $groupService) {
40
		$this->noteMapper = $noteMapper;
41
		$this->utils = $utils;
42
		$this->groupService = $groupService;
43
	}
44
45
	/**
46
	 * Get notes from a user.
47
	 *
48
	 * @param $userId
49
	 * @param int|bool $deleted
50
	 * @param string|bool $grouping
51
	 * @return Note[]
52
	 */
53 View Code Duplication
	public function findNotesFromUser($userId, $deleted = false, $grouping = false) {
54
		// Get shares
55
56
		$dbNotes = $this->noteMapper->findNotesFromUser($userId, $deleted, $grouping);
57
58
		$n = $dbNotes;
59
		if($dbNotes instanceof Note){
60
			$dbNotes = [];
61
			/**
62
			 * @var $n Note
63
			 */
64
			$dbNotes[$n->getId()] = $n;
65
		}
66
67
		//$sharedNotes = $this->sharing->getSharedNotes();
68
		$sharedNotes = [];
69
		$notes = array_merge($dbNotes, $sharedNotes);
70
		return $notes;
71
	}
72
73
	/**
74
	 * Get a single note
75
	 *
76
	 * @param $note_id
77
	 * @param $user_id
78
	 * @param bool|int $deleted
79
	 * @return Note
80
	 * @internal param $vault_id
81
	 */
82
	public function find($note_id, $user_id = null, $deleted = false) {
83
		$note = $this->noteMapper->find($note_id, $user_id, $deleted);
84
		return $note;
85
	}
86
87
	/**
88
	 * Creates a note
89
	 *
90
	 * @param Note $note
91
	 * @return Note
92
	 * @throws \Exception
93
	 */
94
	public function create(Note $note) {
95
		if (!$note instanceof Note) {
96
			throw new \Exception("Expected Note object!");
97
		}
98
99
		return $this->noteMapper->insert($note);
100
	}
101
102
	/**
103
	 * Update note
104
	 *
105
	 * @param $note Note
106
	 * @return Note|bool
107
	 * @throws \Exception
108
	 * @internal param $userId
109
	 * @internal param $vault
110
	 */
111
	public function update(Note $note) {
112
		if (!$note instanceof Note) {
113
			throw new \Exception("Expected Note object!");
114
		}
115
116
		return $this->noteMapper->updateNote($note);
117
	}
118
119
	/**
120
	 * Delete a note from user
121
	 *
122
	 * @param $note_id
123
	 * @param string $user_id
124
	 * @return bool
125
	 * @internal param string $vault_guid
126
	 */
127
	public function delete($note_id, $user_id = null) {
128
		$note = $this->noteMapper->find($note_id, $user_id);
129
		if ($note instanceof Note) {
130
			$this->noteMapper->deleteNote($note);
131
			return true;
132
		} else {
133
			return false;
134
		}
135
	}
136
137
	/**
138
	 * Creates an example note for a user.
139
	 * @param $userId
140
	 */
141
	public function createExampleNote($userId) {
142
		$note = new Note();
143
		$note->setName(ExampleNote::TITLE);
144
		$note->setNote(ExampleNote::NOTE_CONTENT);
145
		$note->setUid($userId);
146
		$note->setMtime(time());
147
		$note->setGuid(Utils::GUID());
148
		$this->create($note);
149
	}
150
}
151