1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Illuminate\Notifications\Channels; |
4
|
|
|
|
5
|
|
|
use RuntimeException; |
6
|
|
|
use Illuminate\Notifications\Notification; |
7
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
8
|
|
|
use Illuminate\Notifications\Events\BroadcastNotificationCreated; |
9
|
|
|
|
10
|
|
|
class BroadcastChannel |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The event dispatcher. |
14
|
|
|
* |
15
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher |
16
|
|
|
*/ |
17
|
|
|
protected $events; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Create a new database channel. |
21
|
|
|
* |
22
|
|
|
* @param \Illuminate\Contracts\Events\Dispatcher $events |
23
|
|
|
* @return void |
|
|
|
|
24
|
|
|
*/ |
25
|
1 |
|
public function __construct(Dispatcher $events) |
26
|
|
|
{ |
27
|
1 |
|
$this->events = $events; |
28
|
1 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Send the given notification. |
32
|
|
|
* |
33
|
|
|
* @param mixed $notifiable |
34
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
1 |
|
public function send($notifiable, Notification $notification) |
38
|
|
|
{ |
39
|
1 |
|
$this->events->fire(new BroadcastNotificationCreated( |
40
|
1 |
|
$notifiable, $notification, $this->getData($notifiable, $notification) |
41
|
|
|
)); |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the data for the notification. |
46
|
|
|
* |
47
|
|
|
* @param mixed $notifiable |
48
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
49
|
|
|
* @return array |
50
|
|
|
* |
51
|
|
|
* @throws \RuntimeException |
52
|
|
|
*/ |
53
|
1 |
|
protected function getData($notifiable, Notification $notification) |
54
|
|
|
{ |
55
|
1 |
|
if (method_exists($notification, 'toBroadcast')) { |
56
|
|
|
return $notification->toBroadcast($notifiable); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
if (method_exists($notification, 'toArray')) { |
60
|
1 |
|
return $notification->toArray($notifiable); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
throw new RuntimeException( |
64
|
|
|
'Notification is missing toArray method.' |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.