Apply strict_types and extensive type declarations throughout the API and models, improving type safety and error handling. Key changes: add declare(strict_types=1) to many files; convert properties, method parameters and return values to typed signatures (PDO, arrays, ints, strings, bools, nullables); switch exception handling to Throwable in index and Router; improve Router, controllers and model method signatures and nullability handling; refine file/image serving security checks and headers in ImageController; strengthen Database typing and initialization methods; return explicit types from BaseModel CRUD helpers and counting; update Media/Cast/Adult/Game/Console/Settings controllers and models to use typed methods, better validation, and clearer update/create return types. Also add AGENTS.md (agent skills index), update README with Swagger/OpenAPI usage instructions, and add /.windsurf to .gitignore. These changes aim to harden runtime correctness, make intended contracts explicit, and prepare the codebase for easier maintenance and static analysis.
64 lines
1.8 KiB
PHP
64 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/../models/Settings.php';
|
|
require_once __DIR__ . '/../services/ApiLogger.php';
|
|
|
|
class SettingsController {
|
|
private Settings $settings;
|
|
private ApiLogger $logger;
|
|
|
|
public function __construct(PDO $pdo) {
|
|
$this->settings = new Settings($pdo);
|
|
$this->logger = ApiLogger::getInstance();
|
|
}
|
|
|
|
public function handleRequest(string $method, array $segments): array {
|
|
$path = '/' . implode('/', $segments);
|
|
$this->logger->logRequest($method, $path);
|
|
|
|
switch ($method) {
|
|
case 'GET':
|
|
return $this->get();
|
|
case 'PUT':
|
|
return $this->update();
|
|
default:
|
|
http_response_code(405);
|
|
return ['success' => false, 'error' => 'Method not allowed'];
|
|
}
|
|
}
|
|
|
|
private function get(): array {
|
|
$settings = $this->settings->getSettings();
|
|
|
|
if (!$settings) {
|
|
http_response_code(404);
|
|
return ['success' => false, 'error' => 'Settings not found'];
|
|
}
|
|
|
|
return ['success' => true, 'data' => $settings];
|
|
}
|
|
|
|
private function update(): array {
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$data) {
|
|
http_response_code(400);
|
|
return ['success' => false, 'error' => 'Invalid JSON'];
|
|
}
|
|
|
|
$settings = $this->settings->updateSettings($data);
|
|
|
|
if (!$settings) {
|
|
http_response_code(500);
|
|
return ['success' => false, 'error' => 'Failed to update settings'];
|
|
}
|
|
|
|
$this->logger->logRequest('PUT', '/api/settings', [], $data);
|
|
$this->logger->logResponse('PUT', '/api/settings', 200, $settings);
|
|
|
|
return ['success' => true, 'data' => $settings];
|
|
}
|
|
}
|