where('key_hash', $hash)->where('is_active', 1)->first(); if ($key !== null) { $this->update((int) $key['id'], [ 'request_count' => (int) $key['request_count'] + 1, 'last_used_at' => date('Y-m-d H:i:s'), ]); } return $key; } /** Creates a key and returns [row, plaintext] — plaintext shown exactly once. */ public function issue(string $name, int $ratePerHour = 120): array { helper('text'); $plain = 'tv_live_' . bin2hex(random_bytes(20)); $row = [ 'name' => $name, 'key_hash' => hash('sha256', $plain), 'key_prefix' => substr($plain, 0, 12), 'rate_limit_per_hour' => $ratePerHour, 'is_active' => 1, ]; $this->insert($row); return [$row, $plain]; } }