| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Notifications\Channels;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Contracts\Events\Dispatcher;
|
|
|
6 |
use Illuminate\Notifications\Events\BroadcastNotificationCreated;
|
|
|
7 |
use Illuminate\Notifications\Messages\BroadcastMessage;
|
|
|
8 |
use Illuminate\Notifications\Notification;
|
|
|
9 |
use RuntimeException;
|
|
|
10 |
|
|
|
11 |
class BroadcastChannel
|
|
|
12 |
{
|
|
|
13 |
/**
|
|
|
14 |
* The event dispatcher.
|
|
|
15 |
*
|
|
|
16 |
* @var \Illuminate\Contracts\Events\Dispatcher
|
|
|
17 |
*/
|
|
|
18 |
protected $events;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* Create a new broadcast channel.
|
|
|
22 |
*
|
|
|
23 |
* @param \Illuminate\Contracts\Events\Dispatcher $events
|
|
|
24 |
* @return void
|
|
|
25 |
*/
|
|
|
26 |
public function __construct(Dispatcher $events)
|
|
|
27 |
{
|
|
|
28 |
$this->events = $events;
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Send the given notification.
|
|
|
33 |
*
|
|
|
34 |
* @param mixed $notifiable
|
|
|
35 |
* @param \Illuminate\Notifications\Notification $notification
|
|
|
36 |
* @return array|null
|
|
|
37 |
*/
|
|
|
38 |
public function send($notifiable, Notification $notification)
|
|
|
39 |
{
|
|
|
40 |
$message = $this->getData($notifiable, $notification);
|
|
|
41 |
|
|
|
42 |
$event = new BroadcastNotificationCreated(
|
|
|
43 |
$notifiable, $notification, is_array($message) ? $message : $message->data
|
|
|
44 |
);
|
|
|
45 |
|
|
|
46 |
if ($message instanceof BroadcastMessage) {
|
|
|
47 |
$event->onConnection($message->connection)
|
|
|
48 |
->onQueue($message->queue);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
return $this->events->dispatch($event);
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Get the data for the notification.
|
|
|
56 |
*
|
|
|
57 |
* @param mixed $notifiable
|
|
|
58 |
* @param \Illuminate\Notifications\Notification $notification
|
|
|
59 |
* @return mixed
|
|
|
60 |
*
|
|
|
61 |
* @throws \RuntimeException
|
|
|
62 |
*/
|
|
|
63 |
protected function getData($notifiable, Notification $notification)
|
|
|
64 |
{
|
|
|
65 |
if (method_exists($notification, 'toBroadcast')) {
|
|
|
66 |
return $notification->toBroadcast($notifiable);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
if (method_exists($notification, 'toArray')) {
|
|
|
70 |
return $notification->toArray($notifiable);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
throw new RuntimeException('Notification is missing toArray method.');
|
|
|
74 |
}
|
|
|
75 |
}
|