Initial commit
This commit is contained in:
67
src/Broker/Topic.php
Normal file
67
src/Broker/Topic.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\Mercureact\Broker;
|
||||
|
||||
use SplObjectStorage;
|
||||
|
||||
class Topic
|
||||
{
|
||||
const MAX_HISTORY_AGE = 180;
|
||||
|
||||
/** @var string Topic name */
|
||||
private string $topic;
|
||||
/** @var array<string,Message> */
|
||||
private array $messages = [];
|
||||
/** @var int Creation unixtime */
|
||||
private int $created;
|
||||
|
||||
private SubscriptionList $subscribers;
|
||||
|
||||
public function __construct(string $topic)
|
||||
{
|
||||
$this->topic = $topic;
|
||||
$this->subscribers = new SubscriptionList();
|
||||
$this->created = time();
|
||||
}
|
||||
|
||||
public function publish(Message $message)
|
||||
{
|
||||
// TODO check if message id has already been published
|
||||
|
||||
foreach ($this->subscribers as $subscriber) {
|
||||
if ($message->private === true) {
|
||||
// TODO check subscriber access
|
||||
} else {
|
||||
// TODO deliver to subscriber
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getAge(): int
|
||||
{
|
||||
return time() - $this->created;
|
||||
}
|
||||
|
||||
public function getHistorySize(): int
|
||||
{
|
||||
return count($this->messages);
|
||||
}
|
||||
|
||||
public function getSubscriberCount(): int
|
||||
{
|
||||
return count($this->subscribers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Garbage collect histry
|
||||
*
|
||||
*/
|
||||
public function garbageCollect(): void
|
||||
{
|
||||
$this->messages = array_filter(
|
||||
$this->messages,
|
||||
fn($message) => $message->getAge() < self::MAX_HISTORY_AGE
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user