laravel-webrtc/src/Rooms/Stores/ArrayRoomStore.php

57 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Blax\WebRtc\Rooms\Stores;
use Blax\WebRtc\Contracts\RoomStore;
/**
* In-process room store: membership + presence in PHP arrays. Right for the
* long-lived bundled server where one process owns every connection. A
* fork-per-message host binds a Cache-backed store instead (see {@see RoomStore}).
*/
final class ArrayRoomStore implements RoomStore
{
/** @var array<string,array<string,array<string,mixed>>> roomId => (peerId => presence info), insertion-ordered */
private array $rooms = [];
/** @var array<string,string> peerId => roomId */
private array $peerRoom = [];
public function members(string $roomId): array
{
return array_keys($this->rooms[$roomId] ?? []);
}
public function info(string $roomId, string $peerId): array
{
return $this->rooms[$roomId][$peerId] ?? [];
}
public function roomOf(string $peerId): ?string
{
return $this->peerRoom[$peerId] ?? null;
}
public function add(string $roomId, string $peerId, array $info): void
{
$this->rooms[$roomId][$peerId] = $info;
$this->peerRoom[$peerId] = $roomId;
}
public function remove(string $roomId, string $peerId): void
{
unset($this->rooms[$roomId][$peerId], $this->peerRoom[$peerId]);
if (($this->rooms[$roomId] ?? []) === []) {
unset($this->rooms[$roomId]);
}
}
public function roomIds(): array
{
return array_keys($this->rooms);
}
}