Files
smueller 4b20efe4bc
Some checks failed
CI / Node — lint, typecheck, test, build (push) Failing after 10s
CI / Go — node-agent tests (push) Failing after 8s
CI / Go — edge-gateway build (push) Successful in 17s
Implement WHMCS Addon Dashboard with API for stats and integrate billing provider logic
2026-06-26 15:11:29 +02:00

65 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
final class HexaGameCloudEventPoller
{
public function __construct(
private readonly HexaGameCloudAddonApiClient $client,
private readonly HexaGameCloudModuleStorage $storage,
private readonly int $limit = 50,
) {
}
public function pollAndAcknowledge(): array
{
$cursor = $this->storage->getSyncCursor();
$response = $this->client->listEvents($cursor, $this->limit);
$events = $response['events'] ?? [];
$ackIds = [];
$stored = 0;
foreach ($events as $event) {
$remoteId = (string) ($event['id'] ?? '');
$eventType = (string) ($event['eventType'] ?? 'unknown');
$payload = is_array($event['payload'] ?? null) ? $event['payload'] : [];
if ($remoteId === '') {
continue;
}
$this->storage->storeRemoteEvent($remoteId, $eventType, $payload);
$stored++;
if ($this->shouldAutoAck($eventType)) {
$ackIds[] = $remoteId;
}
}
if ($ackIds !== []) {
$this->client->acknowledgeEvents($ackIds);
}
if (!empty($response['nextCursor'])) {
$this->storage->updateSyncCursor((string) $response['nextCursor']);
} elseif ($events !== []) {
$last = $events[count($events) - 1];
if (!empty($last['id'])) {
$this->storage->updateSyncCursor((string) $last['id']);
}
}
return [
'fetched' => count($events),
'stored' => $stored,
'acknowledged' => count($ackIds),
'nextCursor' => $response['nextCursor'] ?? null,
];
}
private function shouldAutoAck(string $eventType): bool
{
return in_array($eventType, ['usage_period_ready', 'usage_period_closed'], true);
}
}