1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fenos\Notifynder\Notifications; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Fenos\Notifynder\Contracts\NotificationDB; |
7
|
|
|
use Fenos\Notifynder\Contracts\NotifynderNotification; |
8
|
|
|
use Fenos\Notifynder\Exceptions\NotificationNotFoundException; |
9
|
|
|
use Fenos\Notifynder\Models\Notification as NotificationModel; |
10
|
|
|
use Fenos\Notifynder\Models\NotifynderCollection; |
11
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class NotifynderNotification. |
15
|
|
|
* |
16
|
|
|
* The notification manager is responsible to manage the CRUD operations |
17
|
|
|
* of the notifications. |
18
|
|
|
*/ |
19
|
|
|
class NotificationManager implements NotifynderNotification |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var NotificationDB |
23
|
|
|
*/ |
24
|
|
|
protected $notifynderRepo; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string | null |
28
|
|
|
*/ |
29
|
|
|
protected $entity; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param NotificationDB $notifynderRepo |
33
|
|
|
*/ |
34
|
|
|
public function __construct(NotificationDB $notifynderRepo) |
35
|
|
|
{ |
36
|
|
|
$this->notifynderRepo = $notifynderRepo; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set the entity for polymorphic. |
41
|
|
|
* |
42
|
|
|
* @param $name |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
|
|
public function entity($name) |
46
|
|
|
{ |
47
|
|
|
$this->entity = $name; |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Find a notification by ID. |
54
|
|
|
* |
55
|
|
|
* @param $notificationId |
56
|
|
|
* @return NotificationModel|\Illuminate\Database\Eloquent\Model|static |
57
|
|
|
* @throws \Fenos\Notifynder\Exceptions\NotificationNotFoundException |
58
|
|
|
*/ |
59
|
|
|
public function find($notificationId) |
60
|
|
|
{ |
61
|
|
|
$notification = $this->notifynderRepo->find($notificationId); |
62
|
|
|
|
63
|
|
|
if (is_null($notification)) { |
64
|
|
|
$error = 'Notification Not found'; |
65
|
|
|
throw new NotificationNotFoundException($error); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $notification; |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Make read one notification giving |
73
|
|
|
* the ID of it. |
74
|
|
|
* |
75
|
|
|
* @param $notificationId |
76
|
|
|
* @return bool|\Fenos\Notifynder\Models\Notification |
77
|
|
|
*/ |
78
|
|
|
public function readOne($notificationId) |
79
|
|
|
{ |
80
|
|
|
$notification = $this->find($notificationId); |
81
|
|
|
|
82
|
|
|
return $this->notifynderRepo->readOne($notification); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Read notifications in base the number |
87
|
|
|
* Given. |
88
|
|
|
* |
89
|
|
|
* @param $toId |
90
|
|
|
* @param $numbers |
91
|
|
|
* @param string $order |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
|
|
public function readLimit($toId, $numbers, $order = 'ASC') |
95
|
|
|
{ |
96
|
|
|
return $this->notifynderRepo->readLimit($toId, $this->entity, $numbers, $order); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Read all notification of the |
101
|
|
|
* given entity. |
102
|
|
|
* |
103
|
|
|
* @param $toId |
104
|
|
|
* @return Number |
105
|
|
|
*/ |
106
|
|
|
public function readAll($toId) |
107
|
|
|
{ |
108
|
|
|
return $this->notifynderRepo->readAll($toId, $this->entity); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Delete a notification giving the id |
113
|
|
|
* of it. |
114
|
|
|
* |
115
|
|
|
* @param $notificationId |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function delete($notificationId) |
119
|
|
|
{ |
120
|
|
|
return $this->notifynderRepo->delete($notificationId); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Delete numbers of notifications equals |
125
|
|
|
* to the number passing as 2 parameter of |
126
|
|
|
* the current user. |
127
|
|
|
* |
128
|
|
|
* @param $entityId |
129
|
|
|
* @param $number |
130
|
|
|
* @param $order |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
|
public function deleteLimit($entityId, $number, $order = 'asc') |
134
|
|
|
{ |
135
|
|
|
return $this->notifynderRepo->deleteLimit($entityId, $this->entity, $number, $order); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Delete all notification of a given |
140
|
|
|
* Entity. |
141
|
|
|
* |
142
|
|
|
* @param $entityId |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public function deleteAll($entityId) |
146
|
|
|
{ |
147
|
|
|
return $this->notifynderRepo->deleteAll($entityId, $this->entity); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Delete All notifications from a |
152
|
|
|
* defined category. |
153
|
|
|
* |
154
|
|
|
* @param $categoryName string |
155
|
|
|
* @param $expired Bool |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
public function deleteByCategory($categoryName, $expired = false) |
159
|
|
|
{ |
160
|
|
|
return $this->notifynderRepo->deleteByCategory($categoryName, $expired); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get notifications not read |
165
|
|
|
* of the entity given. |
166
|
|
|
* |
167
|
|
|
* @param $toId |
168
|
|
|
* @param $limit |
169
|
|
|
* @param int|null $paginate |
170
|
|
|
* @param string $orderDate |
171
|
|
|
* @param Closure $filterScope |
172
|
|
|
* @return mixed |
173
|
|
|
*/ |
174
|
|
|
public function getNotRead($toId, $limit = null, $paginate = null, $orderDate = 'desc', Closure $filterScope = null) |
175
|
|
|
{ |
176
|
|
|
$notifications = $this->notifynderRepo->getNotRead( |
177
|
|
|
$toId, $this->entity, |
178
|
|
|
$limit, null, $orderDate, |
179
|
|
|
$filterScope |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
return $this->getPaginatedIfNeeded($notifications, $limit, $paginate); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get All notifications. |
187
|
|
|
* |
188
|
|
|
* @param $toId |
189
|
|
|
* @param $limit |
190
|
|
|
* @param int|null $paginate |
191
|
|
|
* @param string $orderDate |
192
|
|
|
* @param Closure $filterScope |
193
|
|
|
* @return mixed |
194
|
|
|
*/ |
195
|
|
|
public function getAll($toId, $limit = null, $paginate = null, $orderDate = 'desc', Closure $filterScope = null) |
196
|
|
|
{ |
197
|
|
|
$queryLimit = $limit; |
198
|
|
|
if ($this->isPaginated($paginate)) { |
199
|
|
|
$queryLimit = null; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$notifications = $this->notifynderRepo->getAll( |
203
|
|
|
$toId, $this->entity, |
204
|
|
|
$queryLimit, null, $orderDate, |
205
|
|
|
$filterScope |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
return $this->getPaginatedIfNeeded($notifications, $limit, $paginate); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
protected function isPaginated($paginate) |
212
|
|
|
{ |
213
|
|
|
return ! ($paginate === false || is_null($paginate)); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
protected function getPaginatedIfNeeded(NotifynderCollection $notifications, $perPage, $paginate) |
217
|
|
|
{ |
218
|
|
|
if (! $this->isPaginated($paginate)) { |
219
|
|
|
return $notifications->parse(); |
220
|
|
|
} elseif ($paginate === true) { |
221
|
|
|
$paginate = null; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$page = LengthAwarePaginator::resolveCurrentPage(); |
225
|
|
|
$total = $notifications->count(); |
226
|
|
|
$notifications = $notifications->forPage($page, $perPage); |
227
|
|
|
|
228
|
|
|
return new LengthAwarePaginator($notifications->parse(), $total, $perPage, $paginate, [ |
229
|
|
|
'path' => LengthAwarePaginator::resolveCurrentPath(), |
230
|
|
|
]); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Get last notification of the |
235
|
|
|
* given entity. |
236
|
|
|
* |
237
|
|
|
* @param $toId |
238
|
|
|
* @param Closure $filterScope |
239
|
|
|
* @return mixed |
240
|
|
|
*/ |
241
|
|
|
public function getLastNotification($toId, Closure $filterScope = null) |
242
|
|
|
{ |
243
|
|
|
return $this->notifynderRepo->getLastNotification($toId, $this->entity, $filterScope); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Get last notification of the |
248
|
|
|
* given entity of the specific category. |
249
|
|
|
* |
250
|
|
|
* @param $category |
251
|
|
|
* @param $toId |
252
|
|
|
* @param Closure $filterScope |
253
|
|
|
* @return mixed |
254
|
|
|
*/ |
255
|
|
|
public function getLastNotificationByCategory($category, $toId, Closure $filterScope = null) |
256
|
|
|
{ |
257
|
|
|
return $this->notifynderRepo->getLastNotificationByCategory($category, $toId, $this->entity, $filterScope); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Send single notification. |
262
|
|
|
* |
263
|
|
|
* @param array $info |
264
|
|
|
* @return static |
265
|
|
|
*/ |
266
|
|
|
public function sendOne(array $info) |
267
|
|
|
{ |
268
|
|
|
return $this->notifynderRepo->storeSingle($info); |
|
|
|
|
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Send multiple notifications. |
273
|
|
|
* |
274
|
|
|
* @param array $info |
275
|
|
|
* @return mixed |
276
|
|
|
*/ |
277
|
|
|
public function sendMultiple(array $info) |
278
|
|
|
{ |
279
|
|
|
return $this->notifynderRepo->storeMultiple($info); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Get number of notification |
284
|
|
|
* not read. |
285
|
|
|
* |
286
|
|
|
* @param $toId |
287
|
|
|
* @param Closure $filterScope |
288
|
|
|
* @return mixed |
289
|
|
|
*/ |
290
|
|
|
public function countNotRead($toId, Closure $filterScope = null) |
291
|
|
|
{ |
292
|
|
|
return $this->notifynderRepo->countNotRead($toId, $this->entity, $filterScope); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.