1 | <?php |
||
2 | |||
3 | namespace App\Events; |
||
4 | |||
5 | use Illuminate\Broadcasting\Channel; |
||
6 | use Illuminate\Broadcasting\InteractsWithSockets; |
||
7 | use Illuminate\Contracts\Broadcasting\ShouldBroadcast; |
||
8 | use Illuminate\Queue\SerializesModels; |
||
9 | |||
10 | /** |
||
11 | * Class NotificationEvent |
||
12 | * |
||
13 | * Use sockets to broadcast notification to user. |
||
14 | * |
||
15 | * @package App\Events |
||
16 | */ |
||
17 | class NotificationEvent extends Event implements ShouldBroadcast |
||
18 | { |
||
19 | use InteractsWithSockets, SerializesModels; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
20 | |||
21 | /** @var array */ |
||
22 | private $data; |
||
23 | |||
24 | /** @var int */ |
||
25 | private $userId; |
||
26 | |||
27 | /** |
||
28 | * NotificationEvent constructor. |
||
29 | * |
||
30 | * Initialize notification data and user that should receive the notification. |
||
31 | * |
||
32 | * @param array $data |
||
33 | * @param $userId |
||
34 | */ |
||
35 | public function __construct(array $data, $userId) |
||
36 | { |
||
37 | $this->data = $data; |
||
38 | $this->userId = $userId; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Get the channels the notification should broadcast on. |
||
43 | * |
||
44 | * @return Channel|Channel[] |
||
45 | */ |
||
46 | public function broadcastOn() |
||
47 | { |
||
48 | return new Channel('user.' . $this->userId); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Notification data |
||
53 | * |
||
54 | * @return array |
||
55 | */ |
||
56 | public function broadcastWith() |
||
57 | { |
||
58 | return [ |
||
59 | 'notification' => $this->data |
||
60 | ]; |
||
61 | } |
||
62 | } |
||
63 |