Completed
Push — master ( 25f07a...626d4c )
by Sander
01:18
created

NextNoteMapper::getNoteParts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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
		if ($deleted !== false) {
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
106
	/**
107
	 * Creates a note
108
	 *
109
	 * @param NextNote $note
110
	 * @return NextNote|Entity
111
	 * @internal param $userId
112
	 */
113 View Code Duplication
	public function create($note) {
114
		$len = mb_strlen($note->getNote());
115
		$parts = false;
116
		if ($len > $this->maxNoteFieldLength) {
117
			$parts = $this->utils->splitContent($note->getNote());
118
			$note->setNote('');
119
		}
120
121
		$note->setShared(false);
122
		/**
123
		 * @var $note NextNote
124
		 */
125
		$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...
126
127
		if ($parts) {
128
			foreach ($parts as $part) {
129
				$this->createNotePart($note, $part);
130
			}
131
			$note->setNote(implode('', $parts));
132
		}
133
134
135
		return $note;
136
	}
137
138
	/**
139
	 * Update note
140
	 *
141
	 * @param NextNote $note
142
	 * @return NextNote|Entity
143
	 */
144 View Code Duplication
	public function updateNote($note) {
145
		$len = mb_strlen($note->getNote());
146
		$parts = false;
147
		$this->deleteNoteParts($note);
148
149
		if ($len > $this->maxNoteFieldLength) {
150
			$parts = $this->utils->splitContent($note->getNote());
151
			$note->setNote('');
152
		}
153
		/**
154
		 * @var $note NextNote
155
		 */
156
		$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...
157
		if ($parts) {
158
			foreach ($parts as $part) {
159
				$this->createNotePart($note, $part);
160
			}
161
			$note->setNote(implode('', $parts));
162
		}
163
		return $note;
164
	}
165
166
	/**
167
	 * @param NextNote $note
168
	 * @param $content
169
	 */
170
	public function createNotePart(NextNote $note, $content) {
171
		$sql = "INSERT INTO *PREFIX*ownnote_parts VALUES (NULL, ?, ?);";
172
		$this->execute($sql, array($note->getId(), $content));
173
	}
174
175
	/**
176
	 * Delete the note parts
177
	 *
178
	 * @param NextNote $note
179
	 */
180
	public function deleteNoteParts(NextNote $note) {
181
		$sql = 'DELETE FROM *PREFIX*ownnote_parts where id = ?';
182
		$this->execute($sql, array($note->getId()));
183
	}
184
185
	/**
186
	 * Get the note parts
187
	 *
188
	 * @param NextNote $note
189
	 * @return array
190
	 */
191
	public function getNoteParts(NextNote $note) {
192
		$sql = 'SELECT * from *PREFIX*ownnote_parts where id = ?';
193
		return $this->execute($sql, array($note->getId()))->fetchAll();
194
	}
195
196
	/**
197
	 * @param NextNote $note
198
	 * @return bool
199
	 */
200
	public function deleteNote(NextNote $note) {
201
		$this->deleteNoteParts($note);
202
		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...
203
		return true;
204
	}
205
206
	/**
207
	 * @param $arr
208
	 * @return NextNote
209
	 */
210
	public function makeEntityFromDBResult($arr) {
211
		$note = new NextNote();
212
		$note->setId($arr['id']);
213
		$note->setName($arr['name']);
214
		$note->setGrouping($arr['grouping']);
215
		$note->setMtime($arr['mtime']);
216
		$note->setDeleted($arr['deleted']);
217
		$note->setUid($arr['uid']);
218
		$noteParts = $this->getNoteParts($note);
219
		$partsTxt = implode('', array_map(function ($part) {
220
			return $part['note'];
221
		}, $noteParts));
222
		$note->setNote($arr['note'] . $partsTxt);
223
		return $note;
224
	}
225
}
226