| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Notifications\Channels;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Notifications\Notification;
|
|
|
6 |
use RuntimeException;
|
|
|
7 |
|
|
|
8 |
class DatabaseChannel
|
|
|
9 |
{
|
|
|
10 |
/**
|
|
|
11 |
* Send the given notification.
|
|
|
12 |
*
|
|
|
13 |
* @param mixed $notifiable
|
|
|
14 |
* @param \Illuminate\Notifications\Notification $notification
|
|
|
15 |
* @return \Illuminate\Database\Eloquent\Model
|
|
|
16 |
*/
|
|
|
17 |
public function send($notifiable, Notification $notification)
|
|
|
18 |
{
|
|
|
19 |
return $notifiable->routeNotificationFor('database', $notification)->create(
|
|
|
20 |
$this->buildPayload($notifiable, $notification)
|
|
|
21 |
);
|
|
|
22 |
}
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Build an array payload for the DatabaseNotification Model.
|
|
|
26 |
*
|
|
|
27 |
* @param mixed $notifiable
|
|
|
28 |
* @param \Illuminate\Notifications\Notification $notification
|
|
|
29 |
* @return array
|
|
|
30 |
*/
|
|
|
31 |
protected function buildPayload($notifiable, Notification $notification)
|
|
|
32 |
{
|
|
|
33 |
return [
|
|
|
34 |
'id' => $notification->id,
|
|
|
35 |
'type' => method_exists($notification, 'databaseType')
|
|
|
36 |
? $notification->databaseType($notifiable)
|
|
|
37 |
: get_class($notification),
|
|
|
38 |
'data' => $this->getData($notifiable, $notification),
|
|
|
39 |
'read_at' => null,
|
|
|
40 |
];
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Get the data for the notification.
|
|
|
45 |
*
|
|
|
46 |
* @param mixed $notifiable
|
|
|
47 |
* @param \Illuminate\Notifications\Notification $notification
|
|
|
48 |
* @return array
|
|
|
49 |
*
|
|
|
50 |
* @throws \RuntimeException
|
|
|
51 |
*/
|
|
|
52 |
protected function getData($notifiable, Notification $notification)
|
|
|
53 |
{
|
|
|
54 |
if (method_exists($notification, 'toDatabase')) {
|
|
|
55 |
return is_array($data = $notification->toDatabase($notifiable))
|
|
|
56 |
? $data : $data->data;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
if (method_exists($notification, 'toArray')) {
|
|
|
60 |
return $notification->toArray($notifiable);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
throw new RuntimeException('Notification is missing toDatabase / toArray method.');
|
|
|
64 |
}
|
|
|
65 |
}
|