agent/php/Mod/Api/RateLimit/RateLimitResult.php
Snider 5385385314 feat(agent/api): RFC foundation — API keys, webhooks, rate limiting, docs split
Foundation slice for Mantis #844 php/Mod/Api RFC implementation:

* New php/Mod/Api/ package: Boot, Controllers, Documentation, Jobs,
  Middleware, Models, RateLimit, Routes, Services
* Models: ApiKey, WebhookEndpoint, WebhookDelivery
* WebhookService::dispatch() with DB::transaction + afterCommit
* DeliverWebhookJob with retry/backoff
* WebhookSignature with timing-safe verification + 5-minute tolerance +
  dual-secret rotation support
* Sliding-window rate limiter in RateLimit/RateLimitService.php
* AuthenticateApiKey middleware: hk_ prefix + Sanctum fallback
* DocsController / DocumentationController split
* 3 root migrations: api_keys, webhook_endpoints, webhook_deliveries
* Foundation tests under php/tests/Feature/Mod/Api/
* FOLLOWUP.md tracks remaining RFC scope

php -l clean across 21 PHP files. Pest unrunnable in sandbox (no vendor/).

Co-authored-by: Codex <noreply@openai.com>
Closes tasks.lthn.sh/view.php?id=844
2026-04-25 21:01:54 +01:00

52 lines
1.2 KiB
PHP

<?php
// SPDX-License-Identifier: EUPL-1.2
declare(strict_types=1);
namespace Core\Mod\Agentic\Mod\Api\RateLimit;
use Carbon\Carbon;
readonly class RateLimitResult
{
public Carbon $resetsAt;
public function __construct(
public bool $allowed,
public int $limit,
public int $remaining,
public ?int $retryAfter,
public Carbon $resetAt,
) {
$this->resetsAt = $resetAt;
}
public static function allowed(int $limit, int $remaining, Carbon $resetAt): self
{
return new self(true, $limit, $remaining, null, $resetAt);
}
public static function denied(int $limit, int $retryAfter, Carbon $resetAt): self
{
return new self(false, $limit, 0, $retryAfter, $resetAt);
}
/**
* @return array<string, int>
*/
public function headers(): array
{
$headers = [
'X-RateLimit-Limit' => $this->limit,
'X-RateLimit-Remaining' => $this->remaining,
'X-RateLimit-Reset' => $this->resetAt->timestamp,
];
if (! $this->allowed && $this->retryAfter !== null) {
$headers['Retry-After'] = $this->retryAfter;
}
return $headers;
}
}