getRecentRequests(); return count($requests) >= $this->limit; } /** * Check if we're approaching the rate limit threshold. */ public function isAboutToBeExceeded(int $additionalRequests = 10): bool { $requests = $this->getRecentRequests(); return (count($requests) + $additionalRequests) >= $this->limit; } /** * Get number of remaining requests in current window. */ public function getRemaining(): int { $requests = $this->getRecentRequests(); $remainingRequests = $this->limit - count($requests); return max($remainingRequests, 0); } /** * Record a new request. */ public function record(): void { $requests = Cache::get($this->getCacheKey(), []); $requests[] = Carbon::now(); $ttl = Carbon::now()->addMinutes($this->timeframeInMinutes + 1); Cache::put($this->getCacheKey(), $requests, $ttl); } /** * Get recent requests within the timeframe. * * @return array */ private function getRecentRequests(): array { $requests = Cache::get($this->getCacheKey(), []); // Filter requests within the sliding window $minutesAgo = Carbon::now()->subMinutes($this->timeframeInMinutes); return array_filter($requests, function ($timestamp) use ($minutesAgo) { return $timestamp > $minutesAgo; }); } /** * Generate cache key for this rate limiter. */ private function getCacheKey(): string { return "social:{$this->key}:requests"; } }