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

NoteMapper::insert()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 13
nc 4
nop 1
1
<?php
2
/**
3
 * Nextcloud - NextNote
4
 *
5
 * @copyright Copyright (c) 2015, Ben Curtis <[email protected]>
6
 * @copyright Copyright (c) 2017, Sander Brand ([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\Db;
25
26
use OCA\NextNote\Service\NotebookService;
27
use \OCA\NextNote\Utility\Utils;
28
use OCP\AppFramework\Db\Entity;
29
use OCP\IDBConnection;
30
use OCP\AppFramework\Db\Mapper;
31
32
class NoteMapper extends Mapper {
33
	private $utils;
34
	private $notebookService;
35
36
	public function __construct(IDBConnection $db, Utils $utils, NotebookService $notebookService) {
37
		parent::__construct($db, 'nextnote_notes');
38
		$this->utils = $utils;
39
		$this->notebookService = $notebookService;
40
	}
41
42
43
    /**
44
     * @param $note_id
45
     * @param null $user_id
46
     * @param int|bool $deleted
47
     * @return Note if not found
48
     */
49
	public function find($note_id, $user_id = null, $deleted = false) {
50
		$params = [$note_id];
51
		$uidSql = '';
52
		if ($user_id) {
53
			$params[] = $user_id;
54
			$uidSql = 'and n.uid = ?';
55
		}
56
57
		$deletedSql = '';
58
		if ($deleted !== false) {
59
			$params[] = $deleted;
60
			$deletedSql = 'and n.deleted = ?';
61
		}
62
		$sql = "SELECT n.* FROM *PREFIX*nextnote_notes n WHERE n.id= ? $uidSql $deletedSql";
63
		$results = [];
64
		foreach ($this->execute($sql, $params)->fetchAll() as $item) {
65
			/**
66
			 * @var $note Note
67
			 */
68
			$note = $this->makeEntityFromDBResult($item);
69
			/* fetch note parts */
70
			$noteParts = $this->getNoteParts($note);
71
			$partsTxt = implode('', array_map(function ($part) {
72
				return $part['note'];
73
			}, $noteParts));
74
			$note->setNote($item['note'] . $partsTxt);
75
76
			$results[] = $note;
77
		}
78
		return array_shift($results);
79
	}
80
81
82
	/**
83
	 * @param $userId
84
	 * @param int|bool $deleted
85
	 * @param string|bool $group
86
	 * @return Note[] if not found
87
	 */
88
	public function findNotesFromUser($userId, $deleted = 0, $group = false) {
89
		$params = [$userId];
90
		$groupSql = '';
91
		if ($group) {
92
			$groupSql = 'and n.notebook = ?';
93
			$params[] = $group;
94
		}
95
		$deletedSql = '';
96
		if ($deleted !== false) {
97
			$deleted = (int) $deleted;
98
			$deletedSql = 'and n.deleted = ?';
99
			$params[] = $deleted;
100
		}
101
		$sql = "SELECT n.* FROM *PREFIX*nextnote_notes n WHERE n.uid = ? $groupSql $deletedSql";
102
		$results = [];
103
		foreach ($this->execute($sql, $params)->fetchAll() as $item) {
104
			/**
105
			 * @var $note Note
106
			 */
107
			$note = $this->makeEntityFromDBResult($item);
108
			$note->setNote($item['note']);
109
110
			$results[] = $note;
111
		}
112
		return $results;
113
	}
114
115
	
116
117
	/**
118
	 * Creates a note
119
	 *
120
	 * @param Note $note
121
	 * @return Note|Entity
122
	 * @internal param $userId
123
	 */
124
	public function insert($note) {
125
		$len = mb_strlen($note->getNote());
126
		$parts = false;
127
		if ($len > Utils::$maxPartSize) {
128
			$parts = $this->utils->splitContent($note->getNote());
129
			$note->setNote('');
130
		}
131
		$note->setShared(false);
132
133
		$note = parent::insert($note);
134
		/**
135
		 * @var $note Note
136
		 */
137
		if ($parts) {
138
			foreach ($parts as $part) {
139
				$this->createNotePart($note, $part);
140
			}
141
			$note->setNote(implode('', $parts));
142
		}
143
144
145
		return $note;
146
	}
147
148
	/**
149
	 * Update note
150
	 *
151
	 * @param Note $note
152
	 * @return Note|Entity
153
	 */
154
	public function updateNote($note) {
155
		$len = mb_strlen($note->getNote());
156
		$parts = false;
157
		$this->deleteNoteParts($note);
158
159
		if ($len > Utils::$maxPartSize) {
160
			$parts = $this->utils->splitContent($note->getNote());
161
			$note->setNote('');
162
		}
163
		/**
164
		 * @var $note Note
165
		 */
166
167
		$note = parent::update($note);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (update() instead of updateNote()). Are you sure this is correct? If so, you might want to change this to $this->update().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
168
		if ($parts) {
169
			foreach ($parts as $part) {
170
				$this->createNotePart($note, $part);
171
			}
172
			$note->setNote(implode('', $parts));
173
		}
174
		return $this->find($note->getId());
175
	}
176
177
	/**
178
	 * @param Note $note
179
	 * @param $content
180
	 */
181
	public function createNotePart(Note $note, $content) {
182
		$sql = "INSERT INTO *PREFIX*nextnote_parts VALUES (NULL, ?, ?);";
183
		$this->execute($sql, array($note->getId(), $content));
184
	}
185
186
	/**
187
	 * Delete the note parts
188
	 *
189
	 * @param Note $note
190
	 */
191
	public function deleteNoteParts(Note $note) {
192
		$sql = 'DELETE FROM *PREFIX*nextnote_parts where id = ?';
193
		$this->execute($sql, array($note->getId()));
194
	}
195
196
	/**
197
	 * Get the note parts
198
	 *
199
	 * @param Note $note
200
	 * @return array
201
	 */
202
	public function getNoteParts(Note $note) {
203
		$sql = 'SELECT * from *PREFIX*nextnote_parts where id = ?';
204
		return $this->execute($sql, array($note->getId()))->fetchAll();
205
	}
206
207
	/**
208
	 * @param Note $note
209
	 * @return bool
210
	 */
211
	public function deleteNote(Note $note) {
212
		$this->deleteNoteParts($note);
213
		parent::delete($note);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (delete() instead of deleteNote()). Are you sure this is correct? If so, you might want to change this to $this->delete().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
214
		return true;
215
	}
216
217
	/**
218
	 * @param $arr
219
	 * @return Note
220
	 */
221
	public function makeEntityFromDBResult($arr) {
222
223
		$note = new Note();
224
		$note->setId($arr['id']);
225
		$note->setName($arr['name']);
226
		$note->setGuid($arr['guid']);
227
		$note->setGrouping($arr['grouping']);
228
		if($arr['notebook']){
229
			$notebook = $this->notebookService->find($arr['notebook']);
230
			$note->setNotebook($notebook);
231
		}
232
		$note->setMtime($arr['mtime']);
233
		$note->setDeleted($arr['deleted']);
234
		$note->setUid($arr['uid']);
235
		return $note;
236
	}
237
}
238