58 lines
1.4 KiB
PHP
58 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Events;
|
|
|
|
use App\Models\Notification;
|
|
use Illuminate\Broadcasting\Channel;
|
|
use Illuminate\Broadcasting\InteractsWithSockets;
|
|
use Illuminate\Broadcasting\PrivateChannel;
|
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
|
use Illuminate\Foundation\Events\Dispatchable;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
class NotificationSent implements ShouldBroadcast
|
|
{
|
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
|
|
|
public $notification;
|
|
|
|
/**
|
|
* Create a new event instance.
|
|
*/
|
|
public function __construct(Notification $notification)
|
|
{
|
|
$this->notification = $notification;
|
|
}
|
|
|
|
/**
|
|
* Get the channels the event should broadcast on.
|
|
*
|
|
* @return array<int, Channel>
|
|
*/
|
|
public function broadcastOn(): array
|
|
{
|
|
$recipient = $this->notification->recipient;
|
|
|
|
// 1. GLOBAL / PUBLIC
|
|
if ($recipient === 'all') {
|
|
return [new Channel('notifications')];
|
|
}
|
|
|
|
// 2. SPECIFIC USER (ID is numeric)
|
|
if (is_numeric($recipient)) {
|
|
return [new PrivateChannel('App.Models.User.'.$recipient)];
|
|
}
|
|
|
|
// 3. BY ROLE (Default fallback for strings like 'admin', 'superadmin', 'user', etc.)
|
|
return [new PrivateChannel('roles.'.$recipient)];
|
|
}
|
|
|
|
/**
|
|
* The event's broadcast name.
|
|
*/
|
|
public function broadcastAs(): string
|
|
{
|
|
return 'notification.sent';
|
|
}
|
|
}
|