Failed Conditions
Pull Request — master (#95)
by Sander
02:06
created

lib/Service/NoteService.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Fixtures\ShareFix;
30
use OCA\NextNote\ShareBackend\NextNoteShareBackend;
31
use OCA\NextNote\Utility\Utils;
32
use OCA\NextNote\Db\NoteMapper;
33
34
35
class NoteService {
36
37
	private $noteMapper;
38
	private $utils;
39
	private $sharing;
40
	private $groupService;
41
42
	public function __construct(NoteMapper $noteMapper, Utils $utils, NextNoteShareBackend $shareBackend, NotebookService $groupService) {
43
		$this->noteMapper = $noteMapper;
44
		$this->utils = $utils;
45
		$this->sharing = $shareBackend;
46
		$this->groupService = $groupService;
47
	}
48
49
	/**
50
	 * Get notes from a user.
51
	 *
52
	 * @param $userId
53
	 * @param int|bool $deleted
54
	 * @param string|bool $grouping
55
	 * @return Note[]
56
	 */
57
	public function findNotesFromUser($userId, $deleted = false, $grouping = false) {
58
		// Get shares
59
60
		$dbNotes = $this->noteMapper->findNotesFromUser($userId, $deleted, $grouping);
61
62
		$n = $dbNotes;
63
		if($dbNotes instanceof Note){
64
			$dbNotes = [];
65
			/**
66
			 * @var $result Note
67
			 */
68
			$dbNotes[$n->getId()] = $n;
0 ignored issues
show
The method getId cannot be called on $n (of type array<integer,object<OCA\NextNote\Db\Note>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
69
		}
70
71
		$sharedNotes = $this->sharing->getSharedNotes();
72
		$notes = array_merge($dbNotes, $sharedNotes);
73
		return $notes;
74
	}
75
76
	/**
77
	 * Get a single note
78
	 *
79
	 * @param $note_id
80
	 * @param $user_id
81
	 * @param bool|int $deleted
82
	 * @return Note
83
	 * @internal param $vault_id
84
	 */
85
	public function find($note_id, $user_id = null, $deleted = false) {
86
		$note = $this->noteMapper->find($note_id, $user_id, $deleted);
87
		return $note;
88
	}
89
90
	/**
91
	 * Creates a note
92
	 *
93
	 * @param Note $note
94
	 * @return Note
95
	 * @throws \Exception
96
	 */
97
	public function create(Note $note) {
98
		if (!$note instanceof Note) {
99
			throw new \Exception("Expected Note object!");
100
		}
101
102
		return $this->noteMapper->insert($note);
103
	}
104
105
	/**
106
	 * Update note
107
	 *
108
	 * @param $note Note
109
	 * @return Note|bool
110
	 * @throws \Exception
111
	 * @internal param $userId
112
	 * @internal param $vault
113
	 */
114
	public function update(Note $note) {
115
		if (!$note instanceof Note) {
116
			throw new \Exception("Expected Note object!");
117
		}
118
119
		return $this->noteMapper->updateNote($note);
120
	}
121
122
	/**
123
	 * Delete a note from user
124
	 *
125
	 * @param $note_id
126
	 * @param string $user_id
127
	 * @return bool
128
	 * @internal param string $vault_guid
129
	 */
130
	public function delete($note_id, $user_id = null) {
131
		$note = $this->noteMapper->find($note_id, $user_id);
132
		if ($note instanceof Note) {
133
			$this->noteMapper->deleteNote($note);
134
			return true;
135
		} else {
136
			return false;
137
		}
138
	}
139
140
	/**
141
	 * Creates an example note for a user.
142
	 * @param $userId
143
	 */
144
	public function createExampleNote($userId) {
145
		$note = new Note();
146
		$note->setName(ExampleNote::TITLE);
147
		$note->setNote(ExampleNote::NOTE_CONTENT);
148
		$note->setUid($userId);
149
		$note->setMtime(time());
150
		$note->setGuid(Utils::GUID());
151
		$this->create($note);
152
	}
153
}
154