Files

70 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2026-03-15 17:08:23 +07:00
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Events;
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;
use Modules\RestaurantDelivery\Enums\DeliveryStatus;
use Modules\RestaurantDelivery\Models\Delivery;
class DeliveryStatusChanged implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct(
public readonly Delivery $delivery,
public readonly ?DeliveryStatus $previousStatus = null,
public readonly ?array $metadata = null
) {}
/**
* Get the channels the event should broadcast on.
*/
public function broadcastOn(): array
{
$channels = [
new Channel("delivery.{$this->delivery->tracking_code}"),
new PrivateChannel("restaurant.{$this->delivery->restaurant_id}.deliveries"),
];
if ($this->delivery->rider_id) {
$channels[] = new PrivateChannel("rider.{$this->delivery->rider_id}.deliveries");
}
return $channels;
}
/**
* Get the data to broadcast.
*/
public function broadcastWith(): array
{
return [
'delivery_id' => $this->delivery->id,
'tracking_code' => $this->delivery->tracking_code,
'status' => $this->delivery->status->value,
'status_label' => $this->delivery->status->label(),
'status_description' => $this->delivery->status->description(),
'status_color' => $this->delivery->status->color(),
'previous_status' => $this->previousStatus?->value,
'rider_id' => $this->delivery->rider_id,
'changed_at' => now()->toIso8601String(),
'metadata' => $this->metadata,
];
}
/**
* The event's broadcast name.
*/
public function broadcastAs(): string
{
return 'delivery.status.changed';
}
}