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 | 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) { |
|
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) { |
|
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) { |
|
298 | } |
||
299 |