Completed
Pull Request — master (#21)
by Joas
05:43
created

Handler::deleteById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 9
cts 9
cp 1
rs 9.6666
cc 1
eloc 8
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Notifications;
23
24
25
use OCP\DB\QueryBuilder\IQueryBuilder;
26
use OCP\IDBConnection;
27
use OCP\Notification\IAction;
28
use OCP\Notification\IManager;
29
use OCP\Notification\INotification;
30
31
class Handler {
32
	/** @var IDBConnection */
33
	protected $connection;
34
35
	/** @var IManager */
36
	protected $manager;
37
38
	/**
39
	 * @param IDBConnection $connection
40
	 * @param IManager $manager
41
	 */
42 4
	public function __construct(IDBConnection $connection, IManager $manager) {
43 4
		$this->connection = $connection;
44 4
		$this->manager = $manager;
45 4
	}
46
47
	/**
48
	 * Add a new notification to the database
49
	 *
50
	 * @param INotification $notification
51
	 * @return int
52
	 */
53 2
	public function add(INotification $notification) {
54 2
		$sql = $this->connection->getQueryBuilder();
55 2
		$sql->insert('notifications');
56 2
		$this->sqlInsert($sql, $notification);
57 2
		$sql->execute();
58
59 2
		return $sql->getLastInsertId();
60
	}
61
62
	/**
63
	 * Count the notifications matching the given Notification
64
	 *
65
	 * @param INotification $notification
66
	 * @return int
67
	 */
68 2
	public function count(INotification $notification) {
69 2
		$sql = $this->connection->getQueryBuilder();
70 2
		$sql->select($sql->createFunction('COUNT(*)'))
71 2
			->from('notifications');
72
73 2
		$this->sqlWhere($sql, $notification);
74
75 2
		$statement = $sql->execute();
76 2
		$count = (int) $statement->fetchColumn();
77 2
		$statement->closeCursor();
78
79 2
		return $count;
80
	}
81
82
	/**
83
	 * Delete the notifications matching the given Notification
84
	 *
85
	 * @param INotification $notification
86
	 */
87 2
	public function delete(INotification $notification) {
88 2
		$sql = $this->connection->getQueryBuilder();
89 2
		$sql->delete('notifications');
90 2
		$this->sqlWhere($sql, $notification);
91 2
		$sql->execute();
92 2
	}
93
94
	/**
95
	 * Delete the notification matching the given id
96
	 *
97
	 * @param int $id
98
	 * @param string $user
99
	 */
100 1
	public function deleteById($id, $user) {
101 1
		$sql = $this->connection->getQueryBuilder();
102 1
		$sql->delete('notifications')
103 1
			->where($sql->expr()->eq('notification_id', $sql->createParameter('id')))
104 1
			->setParameter('id', $id)
105 1
			->andWhere($sql->expr()->eq('user', $sql->createParameter('user')))
106 1
			->setParameter('user', $user);
107 1
		$sql->execute();
108 1
	}
109
110
	/**
111
	 * Get the notification matching the given id
112
	 *
113
	 * @param int $id
114
	 * @param string $user
115
	 * @return null|INotification
116
	 */
117 1
	public function getById($id, $user) {
118 1
		$sql = $this->connection->getQueryBuilder();
119 1
		$sql->select('*')
120 1
			->from('notifications')
121 1
			->where($sql->expr()->eq('notification_id', $sql->createParameter('id')))
122 1
			->setParameter('id', $id)
123 1
			->andWhere($sql->expr()->eq('user', $sql->createParameter('user')))
124 1
			->setParameter('user', $user);
125 1
		$statement = $sql->execute();
126
127 1
		$notification = null;
128 1
		if ($row = $statement->fetch()) {
129 1
			$notification = $this->notificationFromRow($row);
130 1
		}
131 1
		$statement->closeCursor();
132
133 1
		return $notification;
134
	}
135
136
	/**
137
	 * Return the notifications matching the given Notification
138
	 *
139
	 * @param INotification $notification
140
	 * @param int $limit
141
	 * @return array [notification_id => INotification]
142
	 */
143 2
	public function get(INotification $notification, $limit = 25) {
144 2
		$sql = $this->connection->getQueryBuilder();
145 2
		$sql->select('*')
146 2
			->from('notifications')
147 2
			->orderBy('notification_id', 'DESC')
148 2
			->setMaxResults($limit);
149
150 2
		$this->sqlWhere($sql, $notification);
151 2
		$statement = $sql->execute();
152
153 2
		$notifications = [];
154 2
		while ($row = $statement->fetch()) {
155 1
			$notifications[(int) $row['notification_id']] = $this->notificationFromRow($row);
156 1
		}
157 2
		$statement->closeCursor();
158
159 2
		return $notifications;
160
	}
161
162
	/**
163
	 * Add where statements to a query builder matching the given notification
164
	 *
165
	 * @param IQueryBuilder $sql
166
	 * @param INotification $notification
167
	 */
168 2
	protected function sqlWhere(IQueryBuilder $sql, INotification $notification) {
169 2
		if ($notification->getApp() !== '') {
170 2
			$sql->andWhere($sql->expr()->eq('app', $sql->createParameter('app')));
171 2
			$sql->setParameter('app', $notification->getApp());
172 2
		}
173
174 2
		if ($notification->getUser() !== '') {
175 2
			$sql->andWhere($sql->expr()->eq('user', $sql->createParameter('user')))
176 2
				->setParameter('user', $notification->getUser());
177 2
		}
178
179 2
		if ($notification->getDateTime()->getTimestamp() !== 0) {
180
			$sql->andWhere($sql->expr()->eq('timestamp', $sql->createParameter('timestamp')))
181
				->setParameter('timestamp', $notification->getDateTime()->getTimestamp());
182
		}
183
184 2
		if ($notification->getObjectType() !== '') {
185
			$sql->andWhere($sql->expr()->eq('object_type', $sql->createParameter('objectType')))
186
				->setParameter('objectType', $notification->getObjectType());
187
		}
188
189 2
		if ($notification->getObjectId() !== '') {
190
			$sql->andWhere($sql->expr()->eq('object_id', $sql->createParameter('objectId')))
191
				->setParameter('objectId', $notification->getObjectId());
192
		}
193
194 2
		if ($notification->getSubject() !== '') {
195
			$sql->andWhere($sql->expr()->eq('subject', $sql->createParameter('subject')))
196
				->setParameter('subject', $notification->getSubject());
197
		}
198
199 2
		if ($notification->getMessage() !== '') {
200
			$sql->andWhere($sql->expr()->eq('message', $sql->createParameter('message')))
201
				->setParameter('message', $notification->getMessage());
202
		}
203
204 2
		if ($notification->getLink() !== '') {
205
			$sql->andWhere($sql->expr()->eq('link', $sql->createParameter('link')))
206
				->setParameter('link', $notification->getLink());
207
		}
208 2
	}
209
210
	/**
211
	 * Turn a notification into an input statement
212
	 *
213
	 * @param IQueryBuilder $sql
214
	 * @param INotification $notification
215
	 */
216 2
	protected function sqlInsert(IQueryBuilder $sql, INotification $notification) {
217 2
		$sql->setValue('app', $sql->createParameter('app'))
218 2
			->setParameter('app', $notification->getApp());
219
220 2
		$sql->setValue('user', $sql->createParameter('user'))
221 2
			->setParameter('user', $notification->getUser());
222
223 2
		$sql->setValue('timestamp', $sql->createParameter('timestamp'))
224 2
			->setParameter('timestamp', $notification->getDateTime()->getTimestamp());
225
226 2
		$sql->setValue('object_type', $sql->createParameter('objectType'))
227 2
			->setParameter('objectType', $notification->getObjectType());
228
229 2
		$sql->setValue('object_id', $sql->createParameter('objectId'))
230 2
			->setParameter('objectId', $notification->getObjectId());
231
232 2
		$sql->setValue('subject', $sql->createParameter('subject'))
233 2
			->setParameter('subject', $notification->getSubject());
234
235 2
		$sql->setValue('subject_parameters', $sql->createParameter('subject_parameters'))
236 2
			->setParameter('subject_parameters', json_encode($notification->getSubjectParameters()));
237
238 2
		$sql->setValue('message', $sql->createParameter('message'))
239 2
			->setParameter('message', $notification->getMessage());
240
241 2
		$sql->setValue('message_parameters', $sql->createParameter('message_parameters'))
242 2
			->setParameter('message_parameters', json_encode($notification->getMessageParameters()));
243
244 2
		$sql->setValue('link', $sql->createParameter('link'))
245 2
			->setParameter('link', $notification->getLink());
246
247 2
		$actions = [];
248 2
		foreach ($notification->getActions() as $action) {
249
			/** @var IAction $action */
250 2
			$actions[] = [
251 2
				'label' => $action->getLabel(),
252 2
				'link' => $action->getLink(),
253 2
				'type' => $action->getRequestType(),
254 2
				'primary' => $action->isPrimary(),
255
			];
256 2
		}
257 2
		$sql->setValue('actions', $sql->createParameter('actions'))
258 2
			->setParameter('actions', json_encode($actions));
259 2
	}
260
261
	/**
262
	 * Turn a database row into a INotification
263
	 *
264
	 * @param array $row
265
	 * @return INotification
266
	 */
267 1
	protected function notificationFromRow(array $row) {
268 1
		$dateTime = new \DateTime();
269 1
		$dateTime->setTimestamp((int) $row['timestamp']);
270
271 1
		$notification = $this->manager->createNotification();
272 1
		$notification->setApp($row['app'])
273 1
			->setUser($row['user'])
274 1
			->setDateTime($dateTime)
275 1
			->setObject($row['object_type'], $row['object_id'])
276 1
			->setSubject($row['subject'], (array) json_decode($row['subject_parameters'], true));
277
278 1
		if ($row['message'] !== '') {
279 1
			$notification->setMessage($row['message'], (array) json_decode($row['message_parameters'], true));
280 1
		}
281 1
		if ($row['link'] !== '') {
282 1
			$notification->setLink($row['link']);
283 1
		}
284
285 1
		$actions = (array) json_decode($row['actions'], true);
286 1
		foreach ($actions as $actionData) {
287 1
			$action = $notification->createAction();
288 1
			$action->setLabel($actionData['label'])
289 1
				->setLink($actionData['link'], $actionData['type']);
290 1
			if (isset($actionData['primary'])) {
291 1
				$action->setPrimary($actionData['primary']);
292 1
			}
293 1
			$notification->addAction($action);
294 1
		}
295
296 1
		return $notification;
297
	}
298
}
299