maxAttempts = $maxAttempts; $this->timeWindow = $timeWindow; $this->cache = $cache ?? new FileCache(); } public function isAllowed(string $identifier): bool { $key = 'ratelimit_' . md5($identifier); $attempts = $this->cache->get($key) ?? []; // Clean old attempts $now = time(); $windowStart = $now - $this->timeWindow; $attempts = array_filter($attempts, fn($time) => $time > $windowStart); $attemptCount = count($attempts); if ($attemptCount >= $this->maxAttempts) { return false; } $attempts[] = $now; $this->cache->set($key, $attempts, $this->timeWindow); return true; } public function getRemainingAttempts(string $identifier): int { $key = 'ratelimit_' . md5($identifier); $attempts = $this->cache->get($key) ?? []; // Clean old attempts $now = time(); $windowStart = $now - $this->timeWindow; $attempts = array_filter($attempts, fn($time) => $time > $windowStart); return max(0, $this->maxAttempts - count($attempts)); } public function reset(string $identifier): void { $key = 'ratelimit_' . md5($identifier); $this->cache->delete($key); } }