Completed
Push — master ( 8e687f...b5ae1a )
by Morris
13s
created

Handler::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
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 8
	public function __construct(IDBConnection $connection, IManager $manager) {
43 8
		$this->connection = $connection;
44 8
		$this->manager = $manager;
45 8
	}
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
		}
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 2
			$notifications[(int) $row['notification_id']] = $this->notificationFromRow($row);
156
		}
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
		}
173
174 2
		if ($notification->getUser() !== '') {
175 2
			$sql->andWhere($sql->expr()->eq('user', $sql->createParameter('user')))
176 2
				->setParameter('user', $notification->getUser());
177
		}
178
179 2
		if ($notification->getDateTime()->getTimestamp() !== 0) {
180 1
			$sql->andWhere($sql->expr()->eq('timestamp', $sql->createParameter('timestamp')))
181 1
				->setParameter('timestamp', $notification->getDateTime()->getTimestamp());
182
		}
183
184 2
		if ($notification->getObjectType() !== '') {
185 1
			$sql->andWhere($sql->expr()->eq('object_type', $sql->createParameter('objectType')))
186 1
				->setParameter('objectType', $notification->getObjectType());
187
		}
188
189 2
		if ($notification->getObjectId() !== '') {
190 1
			$sql->andWhere($sql->expr()->eq('object_id', $sql->createParameter('objectId')))
191 1
				->setParameter('objectId', $notification->getObjectId());
192
		}
193
194 2
		if ($notification->getSubject() !== '') {
195 1
			$sql->andWhere($sql->expr()->eq('subject', $sql->createParameter('subject')))
196 1
				->setParameter('subject', $notification->getSubject());
197
		}
198
199 2
		if ($notification->getMessage() !== '') {
200 1
			$sql->andWhere($sql->expr()->eq('message', $sql->createParameter('message')))
201 1
				->setParameter('message', $notification->getMessage());
202
		}
203
204 2
		if ($notification->getLink() !== '') {
205 1
			$sql->andWhere($sql->expr()->eq('link', $sql->createParameter('link')))
206 1
				->setParameter('link', $notification->getLink());
207
		}
208
209 2
		if (method_exists($notification, 'getIcon') && $notification->getIcon() !== '') {
210 1
			$sql->andWhere($sql->expr()->eq('icon', $sql->createParameter('icon')))
211 1
				->setParameter('icon', $notification->getIcon());
212
		}
213 2
	}
214
215
	/**
216
	 * Turn a notification into an input statement
217
	 *
218
	 * @param IQueryBuilder $sql
219
	 * @param INotification $notification
220
	 */
221 2
	protected function sqlInsert(IQueryBuilder $sql, INotification $notification) {
222 2
		$sql->setValue('app', $sql->createParameter('app'))
223 2
			->setParameter('app', $notification->getApp());
224
225 2
		$sql->setValue('user', $sql->createParameter('user'))
226 2
			->setParameter('user', $notification->getUser());
227
228 2
		$sql->setValue('timestamp', $sql->createParameter('timestamp'))
229 2
			->setParameter('timestamp', $notification->getDateTime()->getTimestamp());
230
231 2
		$sql->setValue('object_type', $sql->createParameter('objectType'))
232 2
			->setParameter('objectType', $notification->getObjectType());
233
234 2
		$sql->setValue('object_id', $sql->createParameter('objectId'))
235 2
			->setParameter('objectId', $notification->getObjectId());
236
237 2
		$sql->setValue('subject', $sql->createParameter('subject'))
238 2
			->setParameter('subject', $notification->getSubject());
239
240 2
		$sql->setValue('subject_parameters', $sql->createParameter('subject_parameters'))
241 2
			->setParameter('subject_parameters', json_encode($notification->getSubjectParameters()));
242
243 2
		$sql->setValue('message', $sql->createParameter('message'))
244 2
			->setParameter('message', $notification->getMessage());
245
246 2
		$sql->setValue('message_parameters', $sql->createParameter('message_parameters'))
247 2
			->setParameter('message_parameters', json_encode($notification->getMessageParameters()));
248
249 2
		$sql->setValue('link', $sql->createParameter('link'))
250 2
			->setParameter('link', $notification->getLink());
251
252 2
		if (method_exists($notification, 'getIcon')) {
253 2
			$sql->setValue('icon', $sql->createParameter('icon'))
254 2
				->setParameter('icon', $notification->getIcon());
255
		} else {
256
			$sql->setValue('icon', $sql->createParameter('icon'))
257
				->setParameter('icon', '');
258
		}
259
260 2
		$actions = [];
261 2
		foreach ($notification->getActions() as $action) {
262
			/** @var IAction $action */
263 2
			$actions[] = [
264 2
				'label' => $action->getLabel(),
265 2
				'link' => $action->getLink(),
266 2
				'type' => $action->getRequestType(),
267 2
				'primary' => $action->isPrimary(),
268
			];
269
		}
270 2
		$sql->setValue('actions', $sql->createParameter('actions'))
271 2
			->setParameter('actions', json_encode($actions));
272 2
	}
273
274
	/**
275
	 * Turn a database row into a INotification
276
	 *
277
	 * @param array $row
278
	 * @return INotification
279
	 */
280 2
	protected function notificationFromRow(array $row) {
281 2
		$dateTime = new \DateTime();
282 2
		$dateTime->setTimestamp((int) $row['timestamp']);
283
284 2
		$notification = $this->manager->createNotification();
285 2
		$notification->setApp($row['app'])
286 2
			->setUser($row['user'])
287 2
			->setDateTime($dateTime)
288 2
			->setObject($row['object_type'], $row['object_id'])
289 2
			->setSubject($row['subject'], (array) json_decode($row['subject_parameters'], true));
290
291 2
		if ($row['message'] !== '') {
292 2
			$notification->setMessage($row['message'], (array) json_decode($row['message_parameters'], true));
293
		}
294 2
		if ($row['link'] !== '' && $row['link'] !== null) {
295 2
			$notification->setLink($row['link']);
296
		}
297 2
		if ($row['icon'] !== '' && $row['icon'] !== null) {
298 2
			$notification->setIcon($row['icon']);
299
		}
300
301 2
		$actions = (array) json_decode($row['actions'], true);
302 2
		foreach ($actions as $actionData) {
303 2
			$action = $notification->createAction();
304 2
			$action->setLabel($actionData['label'])
305 2
				->setLink($actionData['link'], $actionData['type']);
306 2
			if (isset($actionData['primary'])) {
307 2
				$action->setPrimary($actionData['primary']);
308
			}
309 2
			$notification->addAction($action);
310
		}
311
312 2
		return $notification;
313
	}
314
}
315