*/ final class InMemoryNotificationBus implements NotificationBusInterface { /** @var array */ private array $entries = []; private int $next = 0; /** * @param int $backlog how many notifications to keep before dropping the oldest */ public function __construct( private readonly int $backlog = 256, ) { if ($this->backlog < 1) { throw new InvalidArgumentException(\sprintf('The notification backlog must be at least one entry, got %d.', $this->backlog)); } } public function publish(Notification $notification): void { $this->entries[$this->next] = $notification; ++$this->next; // A stream that went away must not make this grow forever. if (\count($this->entries) > $this->backlog) { $this->entries = \array_slice($this->entries, -$this->backlog, preserve_keys: true); } } public function cursor(): int { return $this->next; } public function since(int $cursor): array { $found = []; foreach ($this->entries as $sequence => $notification) { if ($sequence >= $cursor) { $found[] = $notification; } } return [$found, $this->next]; } }