|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fenos\Notifynder\Notifications; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Fenos\Notifynder\Contracts\NotificationDB; |
|
7
|
|
|
use Fenos\Notifynder\Models\Notification; |
|
8
|
|
|
use Illuminate\Database\DatabaseManager; |
|
9
|
|
|
use Illuminate\Database\Connection; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
11
|
|
|
use Illuminate\Database\Query\Builder as BuilderDB; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class NotificationRepository. |
|
15
|
|
|
*/ |
|
16
|
|
|
class NotificationRepository implements NotificationDB |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Notification | Builder | BuilderDB |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $notification; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var DatabaseManager | Connection |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $db; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param Notification $notification |
|
30
|
|
|
* @param \Illuminate\Database\DatabaseManager $db |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct( |
|
33
|
|
|
Notification $notification, |
|
34
|
|
|
DatabaseManager $db |
|
35
|
|
|
) { |
|
36
|
|
|
$this->notification = $notification; |
|
37
|
|
|
$this->db = $db; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Find notification by id. |
|
42
|
|
|
* |
|
43
|
|
|
* @param $notificationId |
|
44
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|static |
|
45
|
|
|
*/ |
|
46
|
|
|
public function find($notificationId) |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->notification->find($notificationId); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Save a single notification sent. |
|
53
|
|
|
* |
|
54
|
|
|
* @param array $info |
|
55
|
|
|
* @return Notification |
|
56
|
|
|
*/ |
|
57
|
|
|
public function storeSingle(array $info) |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->notification->create($info); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Save multiple notifications sent |
|
64
|
|
|
* at once. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $notifications |
|
67
|
|
|
* @return mixed |
|
68
|
|
|
*/ |
|
69
|
|
|
public function storeMultiple(array $notifications) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->db->beginTransaction(); |
|
72
|
|
|
$stackId = $this->db->table( |
|
73
|
|
|
$this->notification->getTable() |
|
74
|
|
|
)->max('stack_id') + 1; |
|
75
|
|
|
foreach ($notifications as $key => $notification) { |
|
76
|
|
|
$notifications[$key]['stack_id'] = $stackId; |
|
77
|
|
|
} |
|
78
|
|
|
$insert = $this->db->table( |
|
79
|
|
|
$this->notification->getTable() |
|
80
|
|
|
)->insert($notifications); |
|
81
|
|
|
$this->db->commit(); |
|
82
|
|
|
|
|
83
|
|
|
return $insert; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Make Read One Notification. |
|
88
|
|
|
* |
|
89
|
|
|
* @param Notification $notification |
|
90
|
|
|
* @return bool|Notification |
|
91
|
|
|
*/ |
|
92
|
|
|
public function readOne(Notification $notification) |
|
93
|
|
|
{ |
|
94
|
|
|
$notification->read = 1; |
|
95
|
|
|
|
|
96
|
|
|
if ($notification->save()) { |
|
97
|
|
|
return $notification; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Read notifications in base the number |
|
105
|
|
|
* Given. |
|
106
|
|
|
* |
|
107
|
|
|
* @param $toId |
|
108
|
|
|
* @param $entity |
|
109
|
|
|
* @param $numbers |
|
110
|
|
|
* @param $order |
|
111
|
|
|
* @return int |
|
112
|
|
|
*/ |
|
113
|
|
|
public function readLimit($toId, $entity, $numbers, $order) |
|
114
|
|
|
{ |
|
115
|
|
|
$notifications = $this->notification->withNotRead() |
|
|
|
|
|
|
116
|
|
|
->wherePolymorphic($toId, $entity) |
|
117
|
|
|
->limit($numbers) |
|
118
|
|
|
->orderBy('id', $order) |
|
119
|
|
|
->lists('id'); |
|
120
|
|
|
|
|
121
|
|
|
return $this->notification->whereIn('id', $notifications) |
|
|
|
|
|
|
122
|
|
|
->update(['read' => 1]); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Make read all notification not read. |
|
127
|
|
|
* |
|
128
|
|
|
* @param $toId |
|
129
|
|
|
* @param $entity |
|
130
|
|
|
* @return int |
|
131
|
|
|
*/ |
|
132
|
|
|
public function readAll($toId, $entity) |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->notification->withNotRead() |
|
|
|
|
|
|
135
|
|
|
->wherePolymorphic($toId, $entity) |
|
136
|
|
|
->update(['read' => 1]); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Delete a notification giving the id |
|
141
|
|
|
* of it. |
|
142
|
|
|
* |
|
143
|
|
|
* @param $notificationId |
|
144
|
|
|
* @return bool |
|
145
|
|
|
*/ |
|
146
|
|
|
public function delete($notificationId) |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->notification->where('id', $notificationId)->delete(); |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Delete All notifications about the |
|
153
|
|
|
* current user. |
|
154
|
|
|
* |
|
155
|
|
|
* @param $toId int |
|
156
|
|
|
* @param $entity |
|
157
|
|
|
* @return bool |
|
158
|
|
|
*/ |
|
159
|
|
|
public function deleteAll($toId, $entity) |
|
160
|
|
|
{ |
|
161
|
|
|
$query = $this->db->table( |
|
162
|
|
|
$this->notification->getTable() |
|
163
|
|
|
); |
|
164
|
|
|
|
|
165
|
|
|
return $this->notification->scopeWherePolymorphic($query, $toId, $entity) |
|
166
|
|
|
->delete(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Delete All notifications from a |
|
171
|
|
|
* defined category. |
|
172
|
|
|
* |
|
173
|
|
|
* @param $categoryName int |
|
174
|
|
|
* @param $expired Bool |
|
175
|
|
|
* @return bool |
|
176
|
|
|
*/ |
|
177
|
|
|
public function deleteByCategory($categoryName, $expired = false) |
|
178
|
|
|
{ |
|
179
|
|
|
$query = $this->notification->whereHas( |
|
|
|
|
|
|
180
|
|
|
'body', |
|
181
|
|
|
function ($query) use ($categoryName) { |
|
182
|
|
|
$query->where('name', $categoryName); |
|
183
|
|
|
} |
|
184
|
|
|
); |
|
185
|
|
|
|
|
186
|
|
|
if ($expired == true) { |
|
|
|
|
|
|
187
|
|
|
return $query->onlyExpired()->delete(); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $query->delete(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Delete numbers of notifications equals |
|
195
|
|
|
* to the number passing as 2 parameter of |
|
196
|
|
|
* the current user. |
|
197
|
|
|
* |
|
198
|
|
|
* @param $userId int |
|
199
|
|
|
* @param $entity |
|
200
|
|
|
* @param $number int |
|
201
|
|
|
* @param $order string |
|
202
|
|
|
* @return int |
|
203
|
|
|
* @throws \Exception |
|
204
|
|
|
*/ |
|
205
|
|
|
public function deleteLimit($userId, $entity, $number, $order) |
|
206
|
|
|
{ |
|
207
|
|
|
$notificationsIds = $this->notification |
|
|
|
|
|
|
208
|
|
|
->wherePolymorphic($userId, $entity) |
|
209
|
|
|
->orderBy('id', $order) |
|
210
|
|
|
->select('id') |
|
211
|
|
|
->limit($number) |
|
212
|
|
|
->lists('id'); |
|
213
|
|
|
|
|
214
|
|
|
if (count($notificationsIds) == 0) { |
|
215
|
|
|
return false; |
|
|
|
|
|
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
return $this->notification->whereIn('id', $notificationsIds) |
|
|
|
|
|
|
219
|
|
|
->delete(); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Retrieve notifications not Read |
|
224
|
|
|
* You can also limit the number of |
|
225
|
|
|
* Notification if you don't it will get all. |
|
226
|
|
|
* |
|
227
|
|
|
* @param $toId |
|
228
|
|
|
* @param $entity |
|
229
|
|
|
* @param int|null $limit |
|
230
|
|
|
* @param int|null $paginate |
|
231
|
|
|
* @param string $orderDate |
|
232
|
|
|
* @param Closure|null $filterScope |
|
233
|
|
|
* @return mixed |
|
234
|
|
|
*/ |
|
235
|
|
View Code Duplication |
public function getNotRead( |
|
|
|
|
|
|
236
|
|
|
$toId, |
|
237
|
|
|
$entity, |
|
238
|
|
|
$limit = null, |
|
239
|
|
|
$paginate = null, |
|
240
|
|
|
$orderDate = 'desc', |
|
241
|
|
|
Closure $filterScope = null |
|
242
|
|
|
) { |
|
243
|
|
|
$query = $this->notification->with('body', 'from') |
|
244
|
|
|
->wherePolymorphic($toId, $entity) |
|
245
|
|
|
->withNotRead() |
|
246
|
|
|
->orderBy('read', 'ASC') |
|
247
|
|
|
->orderBy('created_at', $orderDate); |
|
248
|
|
|
|
|
249
|
|
|
if ($limit && ! $paginate) { |
|
|
|
|
|
|
250
|
|
|
$query->limit($limit); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
$query = $this->applyFilter($filterScope, $query); |
|
254
|
|
|
|
|
255
|
|
|
if (is_int(intval($paginate)) && $paginate) { |
|
|
|
|
|
|
256
|
|
|
return $query->paginate($limit); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
return $query->get(); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Retrieve all notifications, not read |
|
264
|
|
|
* in first. |
|
265
|
|
|
* You can also limit the number of |
|
266
|
|
|
* Notifications if you don't, it will get all. |
|
267
|
|
|
* |
|
268
|
|
|
* @param $toId |
|
269
|
|
|
* @param $entity |
|
270
|
|
|
* @param null $limit |
|
271
|
|
|
* @param int|null $paginate |
|
272
|
|
|
* @param string $orderDate |
|
273
|
|
|
* @param Closure $filterScope |
|
274
|
|
|
* @return mixed |
|
275
|
|
|
*/ |
|
276
|
|
View Code Duplication |
public function getAll( |
|
|
|
|
|
|
277
|
|
|
$toId, |
|
278
|
|
|
$entity, |
|
279
|
|
|
$limit = null, |
|
280
|
|
|
$paginate = null, |
|
281
|
|
|
$orderDate = 'desc', |
|
282
|
|
|
Closure $filterScope = null |
|
283
|
|
|
) { |
|
284
|
|
|
$query = $this->notification->with('body', 'from') |
|
285
|
|
|
->wherePolymorphic($toId, $entity) |
|
286
|
|
|
->orderBy('read', 'ASC') |
|
287
|
|
|
->orderBy('created_at', $orderDate); |
|
288
|
|
|
|
|
289
|
|
|
if ($limit && ! $paginate) { |
|
|
|
|
|
|
290
|
|
|
$query->limit($limit); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
$query = $this->applyFilter($filterScope, $query); |
|
294
|
|
|
|
|
295
|
|
|
if (is_int(intval($paginate)) && $paginate) { |
|
|
|
|
|
|
296
|
|
|
return $query->paginate($limit); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
return $query->get(); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* get number Notifications |
|
304
|
|
|
* not read. |
|
305
|
|
|
* |
|
306
|
|
|
* @param $toId |
|
307
|
|
|
* @param $entity |
|
308
|
|
|
* @param Closure $filterScope |
|
309
|
|
|
* @return mixed |
|
310
|
|
|
*/ |
|
311
|
|
View Code Duplication |
public function countNotRead($toId, $entity, Closure $filterScope = null) |
|
|
|
|
|
|
312
|
|
|
{ |
|
313
|
|
|
$query = $this->notification->wherePolymorphic($toId, $entity) |
|
|
|
|
|
|
314
|
|
|
->withNotRead() |
|
315
|
|
|
->select($this->db->raw('Count(*) as notRead')); |
|
316
|
|
|
|
|
317
|
|
|
$query = $this->applyFilter($filterScope, $query); |
|
318
|
|
|
|
|
319
|
|
|
return $query->count(); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* Get last notification of the current |
|
324
|
|
|
* entity. |
|
325
|
|
|
* |
|
326
|
|
|
* @param $toId |
|
327
|
|
|
* @param $entity |
|
328
|
|
|
* @param Closure $filterScope |
|
329
|
|
|
* @return mixed |
|
330
|
|
|
*/ |
|
331
|
|
View Code Duplication |
public function getLastNotification($toId, $entity, Closure $filterScope = null) |
|
|
|
|
|
|
332
|
|
|
{ |
|
333
|
|
|
$query = $this->notification->wherePolymorphic($toId, $entity) |
|
|
|
|
|
|
334
|
|
|
->orderBy('created_at', 'DESC'); |
|
335
|
|
|
|
|
336
|
|
|
$query = $this->applyFilter($filterScope, $query); |
|
337
|
|
|
|
|
338
|
|
|
return $query->first(); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* Get last notification of the current |
|
343
|
|
|
* entity of a specific category. |
|
344
|
|
|
* |
|
345
|
|
|
* @param $category |
|
346
|
|
|
* @param $toId |
|
347
|
|
|
* @param $entity |
|
348
|
|
|
* @param Closure $filterScope |
|
349
|
|
|
* @return mixed |
|
350
|
|
|
*/ |
|
351
|
|
View Code Duplication |
public function getLastNotificationByCategory($category, $toId, $entity, Closure $filterScope = null) |
|
|
|
|
|
|
352
|
|
|
{ |
|
353
|
|
|
$query = $this->notification |
|
|
|
|
|
|
354
|
|
|
->wherePolymorphic($toId, $entity) |
|
355
|
|
|
->byCategory($category) |
|
356
|
|
|
->orderBy('created_at', 'desc'); |
|
357
|
|
|
|
|
358
|
|
|
$query = $this->applyFilter($filterScope, $query); |
|
359
|
|
|
|
|
360
|
|
|
return $query->first(); |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* Apply scope filters. |
|
365
|
|
|
* |
|
366
|
|
|
* @param Closure $filterScope |
|
367
|
|
|
* @param $query |
|
368
|
|
|
*/ |
|
369
|
|
|
protected function applyFilter(Closure $filterScope = null, $query) |
|
370
|
|
|
{ |
|
371
|
|
|
if (! $filterScope) { |
|
372
|
|
|
return $query; |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
$filterScope($query); |
|
376
|
|
|
|
|
377
|
|
|
return $query; |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
/** |
|
381
|
|
|
* Retrive all notifications, in a stack. |
|
382
|
|
|
* You can also limit the number of |
|
383
|
|
|
* Notifications if you don't, it will get all. |
|
384
|
|
|
* |
|
385
|
|
|
* @param $stackId |
|
386
|
|
|
* @param null $limit |
|
387
|
|
|
* @param int|null $paginate |
|
388
|
|
|
* @param string $orderDate |
|
389
|
|
|
* @param Closure $filterScope |
|
390
|
|
|
* @return mixed |
|
391
|
|
|
*/ |
|
392
|
|
View Code Duplication |
public function getStack( |
|
|
|
|
|
|
393
|
|
|
$stackId, |
|
394
|
|
|
$limit = null, |
|
395
|
|
|
$paginate = null, |
|
396
|
|
|
$orderDate = 'desc', |
|
397
|
|
|
Closure $filterScope = null |
|
398
|
|
|
) { |
|
399
|
|
|
$query = $this->notification->with('body', 'from', 'to') |
|
400
|
|
|
->byStack($stackId) |
|
401
|
|
|
->orderBy('read', 'ASC') |
|
402
|
|
|
->orderBy('created_at', $orderDate); |
|
403
|
|
|
|
|
404
|
|
|
if ($limit && ! $paginate) { |
|
|
|
|
|
|
405
|
|
|
$query->limit($limit); |
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
|
|
$query = $this->applyFilter($filterScope, $query); |
|
409
|
|
|
|
|
410
|
|
|
if (is_int(intval($paginate)) && $paginate) { |
|
|
|
|
|
|
411
|
|
|
return $query->paginate($limit); |
|
412
|
|
|
} |
|
413
|
|
|
|
|
414
|
|
|
return $query->get(); |
|
415
|
|
|
} |
|
416
|
|
|
} |
|
417
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: