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