1 | <?php |
||
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) { |
|
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) { |
|
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) { |
|
81 | |||
82 | /** |
||
83 | * Delete the notifications matching the given Notification |
||
84 | * |
||
85 | * @param INotification $notification |
||
86 | */ |
||
87 | 2 | public function delete(INotification $notification) { |
|
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) { |
|
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) { |
|
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) { |
|
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 | 1 | $sql->andWhere($sql->expr()->eq('timestamp', $sql->createParameter('timestamp'))) |
|
181 | 1 | ->setParameter('timestamp', $notification->getDateTime()->getTimestamp()); |
|
182 | 1 | } |
|
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 | 1 | } |
|
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 | 1 | } |
|
193 | |||
194 | 2 | if ($notification->getSubject() !== '') { |
|
195 | 1 | $sql->andWhere($sql->expr()->eq('subject', $sql->createParameter('subject'))) |
|
196 | 1 | ->setParameter('subject', $notification->getSubject()); |
|
197 | 1 | } |
|
198 | |||
199 | 2 | if ($notification->getMessage() !== '') { |
|
200 | 1 | $sql->andWhere($sql->expr()->eq('message', $sql->createParameter('message'))) |
|
201 | 1 | ->setParameter('message', $notification->getMessage()); |
|
202 | 1 | } |
|
203 | |||
204 | 2 | if ($notification->getLink() !== '') { |
|
205 | 1 | $sql->andWhere($sql->expr()->eq('link', $sql->createParameter('link'))) |
|
206 | 1 | ->setParameter('link', $notification->getLink()); |
|
207 | 1 | } |
|
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 | 1 | } |
|
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) { |
|
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) { |
|
314 | } |
||
315 |