Team Ai
Apppublic

Alcazard/vibe-coding-api

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes
QuotaManager.php68 linesDownload Raw Back to Service
1<?php2 3namespace src\Service;4namespace VibeCoding\Service;5 6use Symfony\Component\Cache\Adapter\FilesystemAdapter;7 8class QuotaManager9{10    private FilesystemAdapter $cache;11    private array $limits = [12        'openrouter' => 200,13        'gemini' => 150014    ];15 16    public function __construct()17    {18        $this->cache = new FilesystemAdapter();19    }20 21    public function getAvailableProviders(): array22    {23        $available = [];24        25        foreach (array_keys($this->limits) as $provider) {26            if ($this->canUse($provider)) {27                $available[] = $provider;28            }29        }30        31        return $available;32    }33 34    public function canUse(string $provider): bool35    {36        return $this->getUsage($provider) < $this->limits[$provider];37    }38 39    public function incrementUsage(string $provider): void40    {41        $key = $this->getKey($provider);42        $usage = $this->getUsage($provider) + 1;43        44        $item = $this->cache->getItem($key);45        $item->set($usage);46        $item->expiresAt(new \DateTime('tomorrow midnight'));47        $this->cache->save($item);48    }49 50    public function getRemaining(string $provider): int51    {52        $usage = $this->getUsage($provider);53        return max(0, $this->limits[$provider] - $usage);54    }55 56    private function getUsage(string $provider): int57    {58        $key = $this->getKey($provider);59        $item = $this->cache->getItem($key);60        61        return $item->isHit() ? $item->get() : 0;62    }63 64    private function getKey(string $provider): string65    {66        return sprintf('%s_%s', $provider, date('Y-m-d'));67    }68}