Files

69 lines
1.8 KiB
PHP

<?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\Models\Rider;
class RiderLocationUpdated implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct(
public readonly Rider $rider,
public readonly float $latitude,
public readonly float $longitude,
public readonly ?float $speed = null,
public readonly ?float $bearing = null
) {}
/**
* Get the channels the event should broadcast on.
*/
public function broadcastOn(): array
{
$channels = [
new PrivateChannel("rider.{$this->rider->id}.location"),
];
// Broadcast to all active delivery channels for this rider
foreach ($this->rider->activeDeliveries as $delivery) {
$channels[] = new Channel("delivery.{$delivery->tracking_code}");
}
return $channels;
}
/**
* Get the data to broadcast.
*/
public function broadcastWith(): array
{
return [
'rider_id' => $this->rider->id,
'location' => [
'latitude' => $this->latitude,
'longitude' => $this->longitude,
'speed' => $this->speed,
'bearing' => $this->bearing,
],
'timestamp' => now()->toIso8601String(),
];
}
/**
* The event's broadcast name.
*/
public function broadcastAs(): string
{
return 'rider.location.updated';
}
}