Files
scrum-poker-internal/app/Events/RoomStateChanged.php
T
Twyco 640a2970c3
CI/CD / test_and_check_app (push) Failing after 2m7s
CI/CD / Publish immutable image (push) Skipped
Handle authenticated room instance takeover
2026-09-10 16:33:13 +02:00

60 lines
1.6 KiB
PHP

<?php
namespace App\Events;
use App\Enums\RoomStateChange;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class RoomStateChanged implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* @param array<string, mixed>|null $state
*/
public function __construct(
public readonly string $roomId,
public readonly int $version,
public readonly RoomStateChange $change,
public readonly ?array $state = null,
public readonly ?int $affectedPlayerId = null,
public readonly ?string $affectedInstanceId = null,
) {}
public function broadcastOn(): Channel
{
return new Channel("rooms.{$this->roomId}");
}
public function broadcastAs(): string
{
return 'room.state.changed';
}
/**
* @return array{version: int, change: string, state: array<string, mixed>|null, affected_player_id?: int, affected_instance_id?: string}
*/
public function broadcastWith(): array
{
$payload = [
'version' => $this->version,
'change' => $this->change->value,
'state' => $this->state,
];
if ($this->affectedPlayerId !== null) {
$payload['affected_player_id'] = $this->affectedPlayerId;
}
if ($this->affectedInstanceId !== null) {
$payload['affected_instance_id'] = $this->affectedInstanceId;
}
return $payload;
}
}