Failed Conditions
Pull Request — master (#21)
by Sander
01:18
created

NextNoteMapper::updateNote()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
dl 21
loc 21
rs 9.0534
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\Utility\Utils;
27
use OCP\AppFramework\Db\Entity;
28
use OCP\IDBConnection;
29
use OCP\AppFramework\Db\Mapper;
30
31
class NextNoteMapper extends Mapper {
32
	private $utils;
33
	private $maxNoteFieldLength = 2621440;
34
35
	public function __construct(IDBConnection $db, Utils $utils) {
36
		parent::__construct($db, 'ownnote');
37
		$this->utils = $utils;
38
	}
39
40
41
    /**
42
     * @param $note_id
43
     * @param null $user_id
44
     * @param int|bool $deleted
45
     * @return NextNote if not found
46
     */
47
	public function find($note_id, $user_id = null, $deleted = false) {
48
		$params = [$note_id];
49
		$uidSql = '';
50
		if ($user_id) {
51
			$params[] = $user_id;
52
			$uidSql = 'and n.uid = ?';
53
		}
54
55
		$deletedSql = '';
56
		if ($deleted !== false) {
57
			$params[] = $deleted;
58
			$deletedSql = 'and n.deleted = ?';
59
		}
60
		$sql = "SELECT id, uid, name, grouping, shared, mtime, deleted, note FROM *PREFIX*ownnote n WHERE n.id= ? $uidSql $deletedSql";
61
		$results = [];
62 View Code Duplication
		foreach ($this->execute($sql, $params)->fetchAll() as $item) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
			/**
64
			 * @var $note NextNote
65
			 */
66
			$note = $this->makeEntityFromDBResult($item);
67
			$results[] = $note;
68
		}
69
		return array_shift($results);
70
	}
71
72
73
	/**
74
	 * @param $userId
75
	 * @param int|bool $deleted
76
	 * @param string|bool $group
77
	 * @return NextNote[] if not found
78
	 */
79
	public function findNotesFromUser($userId, $deleted = 0, $group = false) {
80
		$params = [$userId];
81
		$groupSql = '';
82
		if ($group) {
83
			$groupSql = 'and n.grouping = ?';
84
			$params[] = $group;
85
		}
86
		$deletedSql = '';
87 View Code Duplication
		if ($deleted !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
			$deleted = (int) $deleted;
89
			$deletedSql = 'and n.deleted = ?';
90
			$params[] = $deleted;
91
		}
92
		$sql = "SELECT id, uid, name, grouping, shared, mtime, deleted, note FROM *PREFIX*ownnote n WHERE `uid` = ? $groupSql $deletedSql";
93
		$results = [];
94 View Code Duplication
		foreach ($this->execute($sql, $params)->fetchAll() as $item) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
			/**
96
			 * @var $note NextNote
97
			 */
98
			$note = $this->makeEntityFromDBResult($item);
99
			$results[] = $note;
100
		}
101
		return $results;
102
	}
103
104
	/**
105
	 * @param $group
106
	 * @param $userId
107
	 * @param bool $deleted
108
	 * @return NextNote[] if not found
109
	 */
110
	public function findNotesByGroup($group, $userId, $deleted = false) {
111
		$params = [$group];
112
		$uidSql = '';
113
		if ($userId) {
114
			$params[] = $userId;
115
			$uidSql = 'and n.uid = ?';
116
		}
117
118
		$deletedSql = '';
0 ignored issues
show
Unused Code introduced by
$deletedSql is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
119 View Code Duplication
		if ($deleted !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
			$deleted = (int) $deleted;
121
			$deletedSql = 'and n.deleted = ?';
0 ignored issues
show
Unused Code introduced by
$deletedSql is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
122
			$params[] = $deleted;
123
		}
124
125
		$sql = "SELECT n.uid, n.id, n.name, n.grouping, n.shared, n.mtime, n.deleted, p.pid, GROUP_CONCAT(p.note SEPARATOR '') as note FROM *PREFIX*ownnote n INNER JOIN *PREFIX*ownnote_parts p ON n.id = p.id WHERE n.deleted = 0 $uidSql and n.grouping = ? GROUP BY p.id";
126
		$results = [];
127
		foreach ($this->execute($sql, $params)->fetchAll() as $item) {
128
			$note = new NextNote();
129
			$note->setId($item['id']);
130
			$note->setName($item['name']);
131
			$note->setGrouping($item['grouping']);
132
			$note->setMtime($item['mtime']);
133
			$note->setDeleted($item['deleted']);
134
			$note->setNote($item['note']);
135
			$note->setUid($item['uid']);
136
			$results[] = $note;
137
		}
138
		return $results;
139
	}
140
141
	/**
142
	 * Creates a note
143
	 *
144
	 * @param NextNote $note
145
	 * @return NextNote|Entity
146
	 * @internal param $userId
147
	 */
148 View Code Duplication
	public function create($note) {
149
		$len = mb_strlen($note->getNote());
150
		$parts = false;
151
		if ($len > $this->maxNoteFieldLength) {
152
			$parts = $this->utils->splitContent($note->getNote());
153
			$note->setNote('');
154
		}
155
156
		$note->setShared(false);
157
		/**
158
		 * @var $note NextNote
159
		 */
160
		$note = parent::insert($note);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (insert() instead of create()). Are you sure this is correct? If so, you might want to change this to $this->insert().

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...
161
162
		if ($parts) {
163
			foreach ($parts as $part) {
164
				$this->createNotePart($note, $part);
165
			}
166
			$note->setNote(implode('', $parts));
167
		}
168
169
170
		return $note;
171
	}
172
173
	/**
174
	 * Update note
175
	 *
176
	 * @param NextNote $note
177
	 * @return NextNote|Entity
178
	 */
179 View Code Duplication
	public function updateNote($note) {
180
		$len = mb_strlen($note->getNote());
181
		$parts = false;
182
		$this->deleteNoteParts($note);
183
184
		if ($len > $this->maxNoteFieldLength) {
185
			$parts = $this->utils->splitContent($note->getNote());
186
			$note->setNote('');
187
		}
188
		/**
189
		 * @var $note NextNote
190
		 */
191
		$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...
192
		if ($parts) {
193
			foreach ($parts as $part) {
194
				$this->createNotePart($note, $part);
195
			}
196
			$note->setNote(implode('', $parts));
197
		}
198
		return $note;
199
	}
200
201
	/**
202
	 * @param NextNote $note
203
	 * @param $content
204
	 */
205
	public function createNotePart(NextNote $note, $content) {
206
		$sql = "INSERT INTO *PREFIX*ownnote_parts VALUES (NULL, ?, ?);";
207
		$this->execute($sql, array($note->getId(), $content));
208
	}
209
210
	/**
211
	 * Delete the note parts
212
	 *
213
	 * @param NextNote $note
214
	 */
215
	public function deleteNoteParts(NextNote $note) {
216
		$sql = 'DELETE FROM *PREFIX*ownnote_parts where id = ?';
217
		$this->execute($sql, array($note->getId()));
218
	}
219
220
	/**
221
	 * Get the note parts
222
	 *
223
	 * @param NextNote $note
224
	 * @return array
225
	 */
226
	public function getNoteParts(NextNote $note) {
227
		$sql = 'SELECT * from *PREFIX*ownnote_parts where id = ?';
228
		return $this->execute($sql, array($note->getId()))->fetchAll();
229
	}
230
231
	/**
232
	 * @param NextNote $note
233
	 * @return bool
234
	 */
235
	public function deleteNote(NextNote $note) {
236
		$this->deleteNoteParts($note);
237
		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...
238
		return true;
239
	}
240
241
	/**
242
	 * @param $arr
243
	 * @return NextNote
244
	 */
245
	public function makeEntityFromDBResult($arr) {
246
		$note = new NextNote();
247
		$note->setId($arr['id']);
248
		$note->setName($arr['name']);
249
		$note->setGrouping($arr['grouping']);
250
		$note->setMtime($arr['mtime']);
251
		$note->setDeleted($arr['deleted']);
252
		$note->setUid($arr['uid']);
253
		$noteParts = $this->getNoteParts($note);
254
		$partsTxt = implode('', array_map(function ($part) {
255
			return $part['note'];
256
		}, $noteParts));
257
		$note->setNote($arr['note'] . $partsTxt);
258
		return $note;
259
	}
260
}
261