Compare commits

..

1 Commits

Author SHA1 Message Date
Lars Behrends
e38a6e1f7b Add strict types and type hints across API
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.
2026-04-16 16:40:31 +02:00
26 changed files with 545 additions and 419 deletions

1
.gitignore vendored
View File

@@ -54,3 +54,4 @@ yarn-error.log
!/storage/app/public/.gitkeep
*/public/images/*
/api/public/images
/.windsurf

42
AGENTS.md Normal file
View File

@@ -0,0 +1,42 @@
# Project Context for AI Agents
> [!IMPORTANT]
> **To all AI Agents working ON this repository:**
<!-- SKILLS_INDEX_START -->
## Agent Skills Index
> [!CRITICAL] Zero-Trust: Read the matching `SKILL.md` BEFORE writing any code.
> Skills from this index override pre-training patterns. If no skill matches, state: "No project-specific skills applicable."
## Skill Resolution Protocol
Each `_INDEX.md` has two sections — follow both:
1. **Match file type** → find the category index in the router table below.
2. **Read the `_INDEX.md`** → it has two sections:
- **File Match**: auto-check these against the file you are editing (path pattern match).
- **Keyword Match**: only check if the user's request mentions these concepts.
3. **Load ALL matched `SKILL.md`** → read every matched skill before writing code. The tier model keeps matches focused.
| File type | Read category index |
| --------- | ------------------- |
| `*.go`, `*_test.go` | `.windsurf/skills/golang/_INDEX.md` |
| `*.ts` | `.windsurf/skills/angular/_INDEX.md`, `.windsurf/skills/nestjs/_INDEX.md`, `.windsurf/skills/nextjs/_INDEX.md`, `.windsurf/skills/react/_INDEX.md`, `.windsurf/skills/typescript/_INDEX.md` |
| `*.tsx` | `.windsurf/skills/nextjs/_INDEX.md`, `.windsurf/skills/react/_INDEX.md`, `.windsurf/skills/typescript/_INDEX.md` |
| `*.js`, `*.mjs` | `.windsurf/skills/javascript/_INDEX.md` |
| `*.jsx`, `*.test.tsx`, `*.spec.tsx` | `.windsurf/skills/react/_INDEX.md` |
| `*.dart` | `.windsurf/skills/dart/_INDEX.md`, `.windsurf/skills/flutter/_INDEX.md` |
| `*.java` | `.windsurf/skills/java/_INDEX.md`, `.windsurf/skills/spring-boot/_INDEX.md` |
| `*.kt` | `.windsurf/skills/android/_INDEX.md`, `.windsurf/skills/kotlin/_INDEX.md` |
| `*.kts` | `.windsurf/skills/kotlin/_INDEX.md` |
| `*.swift` | `.windsurf/skills/ios/_INDEX.md`, `.windsurf/skills/swift/_INDEX.md` |
| `*.php` | `.windsurf/skills/laravel/_INDEX.md`, `.windsurf/skills/php/_INDEX.md` |
| `*.sql`, `*.entity.ts`, `*.prisma` | `.windsurf/skills/database/_INDEX.md` |
| `*.component.ts`, `*.component.html` | `.windsurf/skills/angular/_INDEX.md` |
| `*.service.ts`, `*.module.ts` | `.windsurf/skills/angular/_INDEX.md`, `.windsurf/skills/nestjs/_INDEX.md` |
| `*.spec.ts`, `*.test.ts` | `.windsurf/skills/common/_INDEX.md` |
| Any file (keyword match) | `.windsurf/skills/common/_INDEX.md` |
| QE workflow | `.windsurf/skills/quality-engineering/_INDEX.md` |
<!-- SKILLS_INDEX_END -->

View File

@@ -326,6 +326,37 @@ Tables are auto-created on first API call. Check logs:
docker-compose logs php
```
## Swagger/OpenAPI Documentation
A Swagger/OpenAPI specification file is included at `swagger.json` in the project root. This file can be used with Swagger UI to interactively test the API.
### Using Swagger UI
#### Option 1: Online Swagger Editor
1. Go to [https://editor.swagger.io](https://editor.swagger.io)
2. Import the `swagger.json` file
3. Click "Try it out" to test endpoints
#### Option 2: Docker with Swagger UI
```bash
docker run -p 8080:8080 -e SWAGGER_JSON=/swagger/swagger.json -v $(pwd):/swagger swaggerapi/swagger-ui
```
Then access at: http://localhost:8080
#### Option 3: Local Swagger UI
1. Download Swagger UI from [https://github.com/swagger-api/swagger-ui](https://github.com/swagger-api/swagger-ui)
2. Extract and open `dist/index.html`
3. Modify the URL to point to your `swagger.json` file
### API Endpoints Covered
- Root endpoints (API info, auto-docs)
- Media CRUD operations
- Series episodes CRUD operations
- Music tracks CRUD operations
- Cast CRUD operations
- Adult cast CRUD operations
- Settings operations
## License
[Add your license here]

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/controllers/MediaController.php';
require_once __DIR__ . '/controllers/CastController.php';
require_once __DIR__ . '/controllers/ImageController.php';
@@ -8,15 +10,15 @@ require_once __DIR__ . '/services/DocumentationService.php';
require_once __DIR__ . '/services/ApiLogger.php';
class Router {
private $pdo;
private $mediaController;
private $castController;
private $imageController;
private $settingsController;
private $documentationService;
private $logger;
private PDO $pdo;
private MediaController $mediaController;
private CastController $castController;
private ImageController $imageController;
private SettingsController $settingsController;
private DocumentationService $documentationService;
private ApiLogger $logger;
public function __construct($pdo) {
public function __construct(PDO $pdo) {
$this->pdo = $pdo;
$this->mediaController = new MediaController($pdo);
$this->castController = new CastController($pdo);
@@ -26,7 +28,7 @@ class Router {
$this->logger = ApiLogger::getInstance();
}
public function route($method, $pathSegments) {
public function route(string $method, array $pathSegments): array {
$path = '/' . implode('/', $pathSegments);
$queryString = $_SERVER['QUERY_STRING'] ?? '';
$fullPath = $queryString ? $path . '?' . $queryString : $path;
@@ -77,7 +79,7 @@ class Router {
$this->logger->logResponse($method, $fullPath, 404, $response);
return $response;
}
} catch (Exception $e) {
} catch (Throwable $e) {
http_response_code(500);
$response = ['success' => false, 'error' => $e->getMessage()];
$this->logger->logError($method, $fullPath, $e->getMessage());
@@ -85,12 +87,12 @@ class Router {
}
}
private function getDocumentation() {
private function getDocumentation(): array {
$docs = $this->documentationService->generateDocumentation();
return ['success' => true, 'data' => $docs];
}
private function getRoot() {
private function getRoot(): array {
return [
'success' => true,
'message' => 'Media API v1.0',

View File

@@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
// Konfiguration
// Docker-Umgebungsvariablen oder Standardwerte
define('DB_HOST', getenv('DB_HOST') ?: 'mariadb');
@@ -15,7 +18,7 @@ define('API_LOG_FILE', '/var/www/html/logs/api.log');
define('PHP_ERROR_LOG_FILE', '/var/www/html/logs/php_error.log');
// Hilfsfunktion für cleanname Generierung
function generateCleanName($name) {
function generateCleanName(string $name): string {
// Kleinbuchstaben, nur alphanumerische Zeichen und Bindestriche
$clean = strtolower($name);
// Sonderzeichen durch Bindestriche ersetzen

View File

@@ -1,21 +1,23 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../models/Cast.php';
require_once __DIR__ . '/../models/AdultCast.php';
require_once __DIR__ . '/../services/ApiLogger.php';
class CastController {
private $cast;
private $adultCast;
private $logger;
private Cast $cast;
private AdultCast $adultCast;
private ApiLogger $logger;
public function __construct($pdo) {
public function __construct(PDO $pdo) {
$this->cast = new Cast($pdo);
$this->adultCast = new AdultCast($pdo);
$this->logger = ApiLogger::getInstance();
}
public function handleRequest($method, $segments) {
public function handleRequest(string $method, array $segments): array {
$id = isset($segments[1]) ? (int)$segments[1] : null;
$subResource = isset($segments[2]) ? $segments[2] : null;
@@ -42,7 +44,7 @@ class CastController {
}
}
private function handleAdult($method, $id, $segments) {
private function handleAdult(string $method, ?int $id, array $segments): array {
switch ($method) {
case 'GET':
@@ -62,7 +64,7 @@ class CastController {
}
}
private function getAdultAll() {
private function getAdultAll(): array {
$filters = [];
if (isset($_GET['search'])) $filters['search'] = $_GET['search'];
if (isset($_GET['ethnicity'])) $filters['ethnicity'] = $_GET['ethnicity'];
@@ -75,7 +77,7 @@ class CastController {
return ['success' => true, 'data' => $result];
}
private function getAdultOne($id) {
private function getAdultOne(?int $id): array {
$cast = $this->adultCast->getWithAdultSpecifics($id);
if (!$cast) {
http_response_code(404);
@@ -84,7 +86,7 @@ class CastController {
return ['success' => true, 'data' => $cast];
}
private function createAdult() {
private function createAdult(): array {
$data = json_decode(file_get_contents('php://input'), true);
if (!$data) {
http_response_code(400);
@@ -119,7 +121,7 @@ class CastController {
return ['success' => true, 'data' => ['id' => $castId]];
}
private function updateAdult($id) {
private function updateAdult(?int $id): array {
if (!$id) {
http_response_code(400);
return ['success' => false, 'error' => 'ID required'];
@@ -137,7 +139,7 @@ class CastController {
return ['success' => true, 'data' => ['id' => $id]];
}
private function deleteAdultSpecifics($id) {
private function deleteAdultSpecifics(?int $id): array {
if (!$id) {
http_response_code(400);
return ['success' => false, 'error' => 'ID required'];
@@ -152,7 +154,7 @@ class CastController {
$this->logger->logResponse('DELETE', "/api/cast/adult/$id", 200, ['message' => 'Adult specifics deleted successfully']);
return ['success' => true, 'message' => 'Adult specifics deleted successfully'];
}
private function getOne($id, $segments) {
private function getOne(?int $id, array $segments): array {
// Prüfen ob /media angehängt wurde
if (isset($segments[2]) && $segments[2] === 'media') {
return $this->getMedia($id);
@@ -169,12 +171,12 @@ class CastController {
return ['success' => true, 'data' => $cast];
}
private function getMedia($castId) {
private function getMedia(?int $castId): array {
$media = $this->cast->getMediaForCast($castId);
return ['success' => true, 'data' => ['items' => $media]];
}
private function getAll() {
private function getAll(): array {
$filters = [];
if (isset($_GET['search'])) $filters['search'] = $_GET['search'];
@@ -185,7 +187,7 @@ class CastController {
return ['success' => true, 'data' => $result];
}
private function create() {
private function create(): array {
$data = json_decode(file_get_contents('php://input'), true);
if (!$data) {
http_response_code(400);
@@ -220,7 +222,7 @@ class CastController {
return ['success' => true, 'data' => ['id' => $castId]];
}
private function update($id) {
private function update(?int $id): array {
if (!$id) {
http_response_code(400);
return ['success' => false, 'error' => 'ID required'];
@@ -238,7 +240,7 @@ class CastController {
return ['success' => true, 'data' => ['id' => $id]];
}
private function delete($id) {
private function delete(?int $id): array {
if (!$id) {
http_response_code(400);
return ['success' => false, 'error' => 'ID required'];

View File

@@ -1,13 +1,15 @@
<?php
declare(strict_types=1);
class ImageController {
private $imageDir;
private string $imageDir;
public function __construct() {
$this->imageDir = __DIR__ . '/../public/images/';
}
public function handleRequest($method, $pathSegments) {
public function handleRequest(string $method, array $pathSegments): array {
// Remove 'images' from path segments
array_shift($pathSegments);

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../models/Media.php';
require_once __DIR__ . '/../models/Series.php';
require_once __DIR__ . '/../models/Music.php';
@@ -7,13 +9,13 @@ require_once __DIR__ . '/../models/Game.php';
require_once __DIR__ . '/../services/ApiLogger.php';
class MediaController {
private $media;
private $series;
private $music;
private $game;
private $logger;
private Media $media;
private Series $series;
private Music $music;
private Game $game;
private ApiLogger $logger;
public function __construct($pdo) {
public function __construct(PDO $pdo) {
$this->media = new Media($pdo);
$this->series = new Series($pdo);
$this->music = new Music($pdo);
@@ -21,7 +23,7 @@ class MediaController {
$this->logger = ApiLogger::getInstance();
}
public function handleRequest($method, $segments) {
public function handleRequest(string $method, array $segments): array {
$id = isset($segments[1]) ? (int)$segments[1] : null;
$subResource = isset($segments[2]) ? $segments[2] : null;
@@ -50,7 +52,7 @@ class MediaController {
}
}
private function handleEpisodes($method, $mediaId, $segments) {
private function handleEpisodes(string $method, ?int $mediaId, array $segments): array {
$episodeId = isset($segments[3]) ? (int)$segments[3] : null;
switch ($method) {
@@ -71,7 +73,7 @@ class MediaController {
}
}
private function handleTracks($method, $mediaId, $segments) {
private function handleTracks(string $method, ?int $mediaId, array $segments): array {
$trackId = isset($segments[3]) ? (int)$segments[3] : null;
switch ($method) {
@@ -92,7 +94,7 @@ class MediaController {
}
}
private function getEpisodes($mediaId) {
private function getEpisodes(?int $mediaId): array {
$season = isset($_GET['season']) ? (int)$_GET['season'] : null;
$episodes = $this->series->getEpisodes($mediaId, $season);
return ['success' => true, 'data' => ['items' => $episodes]];
@@ -103,7 +105,7 @@ class MediaController {
* @param int $mediaId Media ID
* @return array Created episode ID
*/
private function addEpisode($mediaId) {
private function addEpisode(?int $mediaId): array {
$data = json_decode(file_get_contents('php://input'), true);
if (!$data) {
http_response_code(400);
@@ -120,7 +122,7 @@ class MediaController {
* @param int $episodeId Episode ID
* @return array Updated episode ID
*/
private function updateEpisode($episodeId) {
private function updateEpisode(?int $episodeId): array {
if (!$episodeId) {
http_response_code(400);
return ['success' => false, 'error' => 'Episode ID required'];
@@ -141,7 +143,7 @@ class MediaController {
* @param int $episodeId Episode ID
* @return array Success message
*/
private function deleteEpisode($episodeId) {
private function deleteEpisode(?int $episodeId): array {
if (!$episodeId) {
http_response_code(400);
return ['success' => false, 'error' => 'Episode ID required'];
@@ -160,7 +162,7 @@ class MediaController {
* @param int $episodeId Episode ID
* @return array Episode data
*/
private function getEpisode($episodeId) {
private function getEpisode(?int $episodeId): array {
// Episode direkt aus Datenbank abrufen
$stmt = $this->series->getConnection()->prepare("SELECT * FROM episodes WHERE id = ?");
$stmt->execute([$episodeId]);
@@ -173,12 +175,12 @@ class MediaController {
return ['success' => true, 'data' => $episode];
}
private function getTracks($mediaId) {
private function getTracks(?int $mediaId): array {
$tracks = $this->music->getTracks($mediaId);
return ['success' => true, 'data' => ['items' => $tracks]];
}
private function addTrack($mediaId) {
private function addTrack(?int $mediaId): array {
$data = json_decode(file_get_contents('php://input'), true);
if (!$data) {
http_response_code(400);
@@ -190,7 +192,7 @@ class MediaController {
return ['success' => true, 'data' => ['id' => $trackId]];
}
private function updateTrack($trackId) {
private function updateTrack(?int $trackId): array {
if (!$trackId) {
http_response_code(400);
return ['success' => false, 'error' => 'Track ID required'];
@@ -206,7 +208,7 @@ class MediaController {
return ['success' => true, 'data' => ['id' => $trackId]];
}
private function deleteTrack($trackId) {
private function deleteTrack(?int $trackId): array {
if (!$trackId) {
http_response_code(400);
return ['success' => false, 'error' => 'Track ID required'];
@@ -220,7 +222,7 @@ class MediaController {
return ['success' => true, 'message' => 'Track deleted successfully'];
}
private function getTrack($trackId) {
private function getTrack(?int $trackId): array {
// Track direkt aus Datenbank abrufen
$stmt = $this->music->getConnection()->prepare("SELECT * FROM tracks WHERE id = ?");
$stmt->execute([$trackId]);
@@ -238,7 +240,7 @@ class MediaController {
* @param int $id Media ID
* @return array Media object with relations
*/
private function getOne($id) {
private function getOne(?int $id): array {
// Zuerst Basis-Media abrufen um Typ zu bestimmen
$baseMedia = $this->media->getBase($id);
if (!$baseMedia) {
@@ -268,7 +270,7 @@ class MediaController {
* Get all media items with filtering and pagination
* @return array Paginated media list
*/
private function getAll() {
private function getAll(): array {
$filters = [];
if (isset($_GET['category'])) $filters['category'] = $_GET['category'];
if (isset($_GET['type'])) $filters['type'] = $_GET['type'];
@@ -286,7 +288,7 @@ class MediaController {
* Create a new media item
* @return array Created media ID
*/
private function create() {
private function create(): array {
$data = json_decode(file_get_contents('php://input'), true);
if (!$data) {
http_response_code(400);
@@ -341,7 +343,7 @@ class MediaController {
* @param int $id Media ID
* @return array Updated media ID
*/
private function update($id) {
private function update(?int $id): array {
if (!$id) {
http_response_code(400);
return ['success' => false, 'error' => 'ID required'];
@@ -375,7 +377,7 @@ class MediaController {
* @param int $id Media ID
* @return array Success message
*/
private function delete($id) {
private function delete(?int $id): array {
if (!$id) {
http_response_code(400);
return ['success' => false, 'error' => 'ID required'];

View File

@@ -1,18 +1,20 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../models/Settings.php';
require_once __DIR__ . '/../services/ApiLogger.php';
class SettingsController {
private $settings;
private $logger;
private Settings $settings;
private ApiLogger $logger;
public function __construct($pdo) {
public function __construct(PDO $pdo) {
$this->settings = new Settings($pdo);
$this->logger = ApiLogger::getInstance();
}
public function handleRequest($method, $segments) {
public function handleRequest(string $method, array $segments): array {
$path = '/' . implode('/', $segments);
$this->logger->logRequest($method, $path);
@@ -27,7 +29,7 @@ class SettingsController {
}
}
private function get() {
private function get(): array {
$settings = $this->settings->getSettings();
if (!$settings) {
@@ -38,7 +40,7 @@ class SettingsController {
return ['success' => true, 'data' => $settings];
}
private function update() {
private function update(): array {
$data = json_decode(file_get_contents('php://input'), true);
if (!$data) {

View File

@@ -1,8 +1,11 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/config.php';
class Database {
private $pdo;
private ?PDO $pdo;
public function __construct() {
try {
@@ -18,7 +21,7 @@ class Database {
}
}
private function initializeTables() {
private function initializeTables(): void {
// Media-Tabelle
$this->pdo->exec("
CREATE TABLE IF NOT EXISTS media (
@@ -367,7 +370,7 @@ class Database {
$this->updateExistingCleanNames();
}
private function updateExistingCleanNames() {
private function updateExistingCleanNames(): void {
// Media cleanname aktualisieren
$this->pdo->exec("
UPDATE media
@@ -383,7 +386,7 @@ class Database {
");
}
public function getConnection() {
public function getConnection(): PDO {
return $this->pdo;
}
}

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
error_log("API Request: " . $_SERVER['REQUEST_METHOD'] . " " . $_SERVER['REQUEST_URI']);
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/database.php';
@@ -31,7 +33,7 @@ $router = new Router($pdo);
try {
$response = $router->route($method, $pathSegments);
echo json_encode($response);
} catch (Exception $e) {
} catch (Throwable $e) {
http_response_code(500);
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}

View File

@@ -1,32 +1,35 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/MediaType.php';
require_once __DIR__ . '/../services/ImageHandler.php';
class Adult extends MediaType {
private $imageHandler;
private $isUpdate = false;
private $mediaId = null;
private ImageHandler $imageHandler;
private bool $isUpdate = false;
private ?int $mediaId = null;
public function __construct($pdo) {
public function __construct(PDO $pdo) {
parent::__construct($pdo);
$this->imageHandler = new ImageHandler();
}
protected function getType() {
protected function getType(): string {
return 'Adult';
}
protected function getTypeSpecificFields() {
protected function getTypeSpecificFields(): array {
return [];
}
protected function validateTypeSpecificFields($data) {
protected function validateTypeSpecificFields(array $data): array {
// Adult spezifische Validierung
// Eventuell Altersverifikation etc.
return $data;
}
protected function processPosterField($data) {
protected function processPosterField(array $data): array {
error_log("Adult::processPosterField - Checking for poster field, isUpdate: " . ($this->isUpdate ? 'yes' : 'no'));
if ($this->isUpdate && $this->mediaId && isset($data['poster']) && !empty($data['poster'])) {
@@ -62,22 +65,22 @@ class Adult extends MediaType {
return $data;
}
public function createWithRelations($data) {
public function createWithRelations(array $data): int {
$data['type'] = 'Adult';
$this->validateTypeSpecificFields($data);
$data = $this->processPosterField($data);
return parent::createWithRelations($data);
}
public function updateWithRelations($id, $data) {
public function updateWithRelations(int $id, array $data): bool {
$this->isUpdate = true;
$this->mediaId = $id;
$this->validateTypeSpecificFields($data);
$data = $this->processPosterField($data);
parent::updateWithRelations($id, $data);
return parent::updateWithRelations($id, $data);
}
public function search($filters = [], $page = 1, $limit = 20) {
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
// Nur Adult Content suchen
$filters['type'] = 'Adult';
return parent::search($filters, $page, $limit);

View File

@@ -1,11 +1,13 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/Cast.php';
require_once __DIR__ . '/../services/ApiLogger.php';
class AdultCast extends Cast {
public function getWithAdultSpecifics($id) {
public function getWithAdultSpecifics(?int $id): ?array {
$cast = $this->getWithFilmography($id);
if (!$cast) {
return null;
@@ -16,13 +18,13 @@ class AdultCast extends Cast {
return $cast;
}
public function getAdultSpecifics($castId) {
public function getAdultSpecifics(?int $castId): array|false {
$stmt = $this->pdo->prepare("SELECT * FROM adult_cast_specifics WHERE cast_id = ?");
$stmt->execute([$castId]);
return $stmt->fetch();
}
public function createWithAdultSpecifics($data) {
public function createWithAdultSpecifics(array $data): int {
ApiLogger::getInstance()->logDebug("AdultCast createWithAdultSpecifics called with data: " . json_encode($data));
$name = $data['name'] ?? null;
@@ -46,7 +48,7 @@ class AdultCast extends Cast {
'birthPlace' => $data['birthPlace'] ?? null
];
$castId = $this->create($castData);
$castId = (int)$this->create($castData);
ApiLogger::getInstance()->logDebug("AdultCast createWithAdultSpecifics: Base cast created with id: $castId");
// Occupations speichern
@@ -66,7 +68,7 @@ class AdultCast extends Cast {
return $castId;
}
public function updateWithAdultSpecifics($id, $data) {
public function updateWithAdultSpecifics(int $id, array $data): bool {
ApiLogger::getInstance()->logDebug("AdultCast updateWithAdultSpecifics called with id: $id, data: " . json_encode($data));
// Set update flag for image replacement
@@ -108,7 +110,7 @@ class AdultCast extends Cast {
return true;
}
protected function saveAdultSpecifics($castId, $specifics) {
protected function saveAdultSpecifics(int $castId, array $specifics): void {
ApiLogger::getInstance()->logDebug("AdultCast saveAdultSpecifics called for cast_id: $castId");
ApiLogger::getInstance()->logDebug("Specifics data: " . json_encode($specifics));
@@ -200,13 +202,13 @@ class AdultCast extends Cast {
}
}
public function deleteAdultSpecifics($castId) {
public function deleteAdultSpecifics(?int $castId): bool {
$stmt = $this->pdo->prepare("DELETE FROM adult_cast_specifics WHERE cast_id = ?");
$stmt->execute([$castId]);
return $stmt->rowCount() > 0;
}
public function searchAdultActors($filters = [], $page = 1, $limit = 2000000000) {
public function searchAdultActors(array $filters = [], int $page = 1, int $limit = 2000000000): array {
// Adult Actors mit Specifics suchen
$query = "
SELECT cs.*,

View File

@@ -1,20 +1,22 @@
<?php
abstract class BaseModel {
protected $pdo;
protected $table;
declare(strict_types=1);
public function __construct($pdo) {
abstract class BaseModel {
protected PDO $pdo;
protected string $table;
public function __construct(PDO $pdo) {
$this->pdo = $pdo;
}
protected function findById($id) {
protected function findById(int $id): array|false {
$stmt = $this->pdo->prepare("SELECT * FROM {$this->table} WHERE id = ?");
$stmt->execute([$id]);
return $stmt->fetch();
}
protected function findAll($conditions = [], $orderBy = 'createdAt DESC', $limit = null, $offset = null) {
protected function findAll(array $conditions = [], string $orderBy = 'createdAt DESC', ?int $limit = null, ?int $offset = null): array {
$query = "SELECT * FROM {$this->table} WHERE 1=1";
$params = [];
@@ -31,11 +33,11 @@ abstract class BaseModel {
$query .= " ORDER BY $orderBy";
if ($limit) {
if ($limit !== null) {
$query .= " LIMIT " . (int)$limit;
}
if ($offset) {
if ($offset !== null) {
$query .= " OFFSET " . (int)$offset;
}
@@ -44,7 +46,7 @@ abstract class BaseModel {
return $stmt->fetchAll();
}
protected function count($conditions = []) {
protected function count(array $conditions = []): int|false {
$query = "SELECT COUNT(*) FROM {$this->table} WHERE 1=1";
$params = [];
@@ -63,7 +65,7 @@ abstract class BaseModel {
return $stmt->fetchColumn();
}
protected function create($data) {
protected function create(array $data): int|false {
$fields = array_keys($data);
$placeholders = array_fill(0, count($fields), '?');
@@ -74,7 +76,7 @@ abstract class BaseModel {
return $this->pdo->lastInsertId();
}
protected function update($id, $data) {
protected function update(int $id, array $data): bool {
$fields = [];
$params = [];
@@ -92,7 +94,7 @@ abstract class BaseModel {
return $stmt->rowCount() > 0;
}
protected function delete($id) {
protected function delete(int $id): bool {
$stmt = $this->pdo->prepare("DELETE FROM {$this->table} WHERE id = ?");
$stmt->execute([$id]);
return $stmt->rowCount() > 0;

View File

@@ -1,20 +1,22 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/BaseModel.php';
require_once __DIR__ . '/../services/ImageHandler.php';
class Cast extends BaseModel {
protected $table = 'cast_staff';
private $imageHandler;
protected $isUpdate = false;
protected $castId = null;
protected string $table = 'cast_staff';
private ImageHandler $imageHandler;
protected bool $isUpdate = false;
protected ?int $castId = null;
public function __construct($pdo) {
public function __construct(PDO $pdo) {
parent::__construct($pdo);
$this->imageHandler = new ImageHandler();
}
public function getWithFilmography($id) {
public function getWithFilmography(?int $id): ?array {
$cast = $this->findById($id);
if (!$cast) {
return null;
@@ -26,7 +28,7 @@ class Cast extends BaseModel {
return $cast;
}
public function getMediaForCast($castId) {
public function getMediaForCast(?int $castId): array {
$stmt = $this->pdo->prepare("
SELECT m.id, m.title, m.year, m.poster, m.category, m.type, mc.role, mc.characterName
FROM media m
@@ -38,7 +40,7 @@ class Cast extends BaseModel {
return $stmt->fetchAll();
}
public function search($filters = [], $page = 1, $limit = 20) {
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
$conditions = [];
if (isset($filters['search'])) {
@@ -88,7 +90,7 @@ class Cast extends BaseModel {
];
}
protected function processPhotoField($data) {
protected function processPhotoField(array $data): array {
error_log("Cast::processPhotoField - Checking for photo field, isUpdate: " . ($this->isUpdate ? 'yes' : 'no'));
if ($this->isUpdate && $this->castId && isset($data['photo']) && !empty($data['photo'])) {
@@ -124,7 +126,7 @@ class Cast extends BaseModel {
return $data;
}
public function createWithOccupations($data) {
public function createWithOccupations(array $data): int {
$name = $data['name'] ?? null;
if (!$name) {
throw new Exception('Name is required');
@@ -153,7 +155,7 @@ class Cast extends BaseModel {
return $castId;
}
public function updateWithOccupations($id, $data) {
public function updateWithOccupations(int $id, array $data): bool {
$this->isUpdate = true;
$this->castId = $id;
@@ -184,13 +186,13 @@ class Cast extends BaseModel {
return true;
}
public function findByCleanName($cleanname) {
public function findByCleanName(string $cleanname): array|false {
$stmt = $this->pdo->prepare("SELECT * FROM {$this->table} WHERE cleanname = ?");
$stmt->execute([$cleanname]);
return $stmt->fetch();
}
protected function getRelatedItems($table, $id, $fkColumn = 'media_id') {
protected function getRelatedItems(string $table, int $id, string $fkColumn = 'media_id'): array {
$stmt = $this->pdo->prepare("SELECT * FROM $table WHERE $fkColumn = ?");
$stmt->execute([$id]);
$items = $stmt->fetchAll();
@@ -201,7 +203,7 @@ class Cast extends BaseModel {
return $result;
}
protected function saveRelatedItems($table, $id, $items, $fkColumn = 'media_id') {
protected function saveRelatedItems(string $table, int $id, array $items, string $fkColumn = 'media_id'): void {
$valueColumn = $fkColumn === 'cast_id' ? 'occupation' : 'genre';
$stmt = $this->pdo->prepare("INSERT INTO $table ($fkColumn, $valueColumn) VALUES (?, ?)");
foreach ($items as $item) {

View File

@@ -1,21 +1,24 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/MediaType.php';
class Console extends MediaType {
protected function getType() {
protected function getType(): string {
return 'Console';
}
protected function getTypeSpecificFields() {
protected function getTypeSpecificFields(): array {
return [];
}
protected function validateTypeSpecificFields($data) {
protected function validateTypeSpecificFields(array $data): array {
// Console spezifische Validierung
return $data;
}
public function search($filters = [], $page = 1, $limit = 20) {
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
// Nur Consoles suchen
$filters['type'] = 'Console';
return parent::search($filters, $page, $limit);

View File

@@ -1,27 +1,29 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/MediaType.php';
require_once __DIR__ . '/../services/ImageHandler.php';
class Game extends MediaType {
private $imageHandler;
private $isUpdate = false;
private $mediaId = null;
private ImageHandler $imageHandler;
private bool $isUpdate = false;
private ?int $mediaId = null;
public function __construct($pdo) {
public function __construct(PDO $pdo) {
parent::__construct($pdo);
$this->imageHandler = new ImageHandler();
}
protected function getType() {
protected function getType(): string {
return 'Game';
}
protected function getTypeSpecificFields() {
protected function getTypeSpecificFields(): array {
return [];
}
protected function validateTypeSpecificFields($data) {
protected function validateTypeSpecificFields(array $data): array {
// Game spezifische Validierung
if (isset($data['hasIcon'])) {
$data['hasIcon'] = is_numeric($data['hasIcon']) ? (int)$data['hasIcon'] : 0;
@@ -51,7 +53,7 @@ class Game extends MediaType {
/**
* Process poster field - convert base64 to file path
*/
protected function processImageField($data, $type) {
protected function processImageField(array $data, string $type): array {
if ($this->isUpdate && $this->mediaId && isset($data[$type]) && !empty($data[$type])) {
$currentMedia = $this->findById($this->mediaId);
if ($currentMedia && !empty($currentMedia[$type])) {
@@ -73,7 +75,7 @@ class Game extends MediaType {
return $data;
}
public function createWithRelations($data) {
public function createWithRelations(array $data): int {
// Typ setzen
$data['type'] = 'Game';
@@ -152,7 +154,7 @@ class Game extends MediaType {
return $mediaId;
}
public function updateWithRelations($id, $data) {
public function updateWithRelations(int $id, array $data): bool {
// Set update flag and mediaId for image replacement
$this->isUpdate = true;
$this->mediaId = $id;
@@ -236,7 +238,7 @@ class Game extends MediaType {
return true;
}
public function getWithGameInfo($id) {
public function getWithGameInfo(?int $id): ?array {
$media = parent::getWithRelations($id);
if (!$media) {
return null;
@@ -264,7 +266,7 @@ class Game extends MediaType {
return $media;
}
public function getGameInfoForList($mediaId) {
public function getGameInfoForList(?int $mediaId): ?array {
// Media-Games Daten abrufen (ohne vollständige Relationen für Performance)
$mediaGameId = $this->getMediaGameId($mediaId);
if (!$mediaGameId) {
@@ -281,7 +283,7 @@ class Game extends MediaType {
return $gameInfo;
}
public static function interpolateQuery($query, $params) {
public static function interpolateQuery(string $query, array $params): string {
$keys = array();
# build a regular expression for each parameter
@@ -300,7 +302,7 @@ class Game extends MediaType {
return $query;
}
protected function createMediaGame($data) {
protected function createMediaGame(array $data): string {
$stmt = $this->pdo->prepare("
INSERT INTO media_games (media_id, sortingName, notes, completionStatus, source, gameId, pluginId,
isInstalled, installDirectory, installSize, hidden, favorite, playCount,
@@ -337,7 +339,7 @@ class Game extends MediaType {
return $this->pdo->lastInsertId();
}
protected function updateMediaGame($mediaId, $data) {
protected function updateMediaGame(int $mediaId, array $data): void {
$setClause = [];
$params = [];
@@ -354,14 +356,14 @@ class Game extends MediaType {
$stmt->execute($params);
}
protected function getMediaGameId($mediaId) {
protected function getMediaGameId(?int $mediaId): ?int {
$stmt = $this->pdo->prepare("SELECT id FROM media_games WHERE media_id = ?");
$stmt->execute([$mediaId]);
$result = $stmt->fetch();
return $result ? $result['id'] : null;
}
protected function getMediaGameData($mediaGameId) {
protected function getMediaGameData(?int $mediaGameId): array {
$stmt = $this->pdo->prepare("SELECT * FROM media_games WHERE id = ?");
$stmt->execute([$mediaGameId]);
$data = $stmt->fetch();
@@ -373,7 +375,7 @@ class Game extends MediaType {
return $data ?: [];
}
protected function saveAchievements($mediaGameId, $achievements) {
protected function saveAchievements(string $mediaGameId, array $achievements): void {
$stmt = $this->pdo->prepare("
INSERT INTO achievements (media_game_id, name, description, icon, unlocked, unlocked_date)
VALUES (?, ?, ?, ?, ?, ?)
@@ -396,26 +398,26 @@ class Game extends MediaType {
}
}
protected function getAchievements($mediaGameId) {
protected function getAchievements(string $mediaGameId): array {
$stmt = $this->pdo->prepare("SELECT * FROM achievements WHERE media_game_id = ?");
$stmt->execute([$mediaGameId]);
return $stmt->fetchAll();
}
protected function saveGameRelation($table, $mediaGameId, $items, $field) {
protected function saveGameRelation(string $table, string $mediaGameId, array $items, string $field): void {
$stmt = $this->pdo->prepare("INSERT INTO $table (media_game_id, $field) VALUES (?, ?)");
foreach ($items as $item) {
$stmt->execute([$mediaGameId, $item]);
}
}
protected function consoleExists($name) {
protected function consoleExists(string $name): bool {
$stmt = $this->pdo->prepare("SELECT id FROM media WHERE type = 'Console' AND title = ?");
$stmt->execute([$name]);
return $stmt->fetch() !== false;
}
protected function createConsole($name) {
protected function createConsole(string $name): string {
$stmt = $this->pdo->prepare("
INSERT INTO media (title, cleanname, type, createdAt, updatedAt)
VALUES (?, ?, 'Console', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
@@ -424,7 +426,7 @@ class Game extends MediaType {
return $this->pdo->lastInsertId();
}
protected function saveGameRelationWithConsole($table, $mediaGameId, $items, $field) {
protected function saveGameRelationWithConsole(string $table, string $mediaGameId, array $items, string $field): void {
$stmt = $this->pdo->prepare("INSERT INTO $table (media_game_id, $field) VALUES (?, ?)");
foreach ($items as $item) {
// Check if console exists, create if not
@@ -435,7 +437,7 @@ class Game extends MediaType {
}
}
protected function getGameRelation($table, $mediaGameId, $field) {
protected function getGameRelation(string $table, string $mediaGameId, string $field): array {
$stmt = $this->pdo->prepare("SELECT $field FROM $table WHERE media_game_id = ?");
$stmt->execute([$mediaGameId]);
$items = $stmt->fetchAll();
@@ -444,7 +446,7 @@ class Game extends MediaType {
}, $items);
}
protected function saveLinks($mediaGameId, $links) {
protected function saveLinks(string $mediaGameId, array $links): void {
$stmt = $this->pdo->prepare("INSERT INTO game_links (media_game_id, name, url) VALUES (?, ?, ?)");
foreach ($links as $link) {
$stmt->execute([
@@ -455,13 +457,13 @@ class Game extends MediaType {
}
}
protected function getLinks($mediaGameId) {
protected function getLinks(string $mediaGameId): array {
$stmt = $this->pdo->prepare("SELECT name, url FROM game_links WHERE media_game_id = ?");
$stmt->execute([$mediaGameId]);
return $stmt->fetchAll();
}
public function search($filters = [], $page = 1, $limit = 20) {
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
// Nur Games suchen
$filters['type'] = 'Game';
return parent::search($filters, $page, $limit);

View File

@@ -1,15 +1,17 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/BaseModel.php';
class Media extends BaseModel {
protected $table = 'media';
protected string $table = 'media';
public function getBase($id) {
public function getBase(?int $id): array|false {
return $this->findById($id);
}
public function getWithRelations($id) {
public function getWithRelations(?int $id): ?array {
$media = $this->findById($id);
if (!$media) {
return null;
@@ -23,7 +25,7 @@ class Media extends BaseModel {
return $media;
}
public function search($filters = [], $page = 1, $limit = 20) {
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
$conditions = [];
if (isset($filters['category'])) {
@@ -99,7 +101,7 @@ class Media extends BaseModel {
];
}
public function createWithRelations($data) {
public function createWithRelations(array $data): int {
$title = $data['title'] ?? null;
if (!$title) {
throw new Exception('Title is required');
@@ -147,7 +149,7 @@ class Media extends BaseModel {
return $mediaId;
}
public function updateWithRelations($id, $data) {
public function updateWithRelations(int $id, array $data): bool {
$mediaData = [];
foreach (['title', 'year', 'poster', 'banner', 'description', 'rating', 'category', 'type', 'status', 'aspectRatio', 'runtime', 'director', 'writer', 'releaseDate', 'source'] as $field) {
@@ -186,13 +188,13 @@ class Media extends BaseModel {
return true;
}
public function findByCleanName($cleanname) {
public function findByCleanName(string $cleanname): array|false {
$stmt = $this->pdo->prepare("SELECT * FROM {$this->table} WHERE cleanname = ?");
$stmt->execute([$cleanname]);
return $stmt->fetch();
}
protected function saveRelatedItems($table, $id, $items, $fkColumn = 'media_id') {
protected function saveRelatedItems(string $table, int $id, array $items, string $fkColumn = 'media_id'): void {
$valueColumn = $table === 'genres' ? 'genre' : ($table === 'tags' ? 'tag' : ($table === 'occupations' ? 'occupation' : 'studio'));
$stmt = $this->pdo->prepare("INSERT INTO $table ($fkColumn, $valueColumn) VALUES (?, ?)");
foreach ($items as $item) {
@@ -200,7 +202,7 @@ class Media extends BaseModel {
}
}
protected function getCastForMedia($mediaId) {
protected function getCastForMedia(?int $mediaId): array {
$stmt = $this->pdo->prepare("
SELECT cs.*, mc.role, mc.characterName, mc.characterImage
FROM cast_staff cs
@@ -215,7 +217,7 @@ class Media extends BaseModel {
return $cast;
}
protected function getRelatedItems($table, $id, $fkColumn = 'media_id') {
protected function getRelatedItems(string $table, int $id, string $fkColumn = 'media_id'): array {
$stmt = $this->pdo->prepare("SELECT * FROM $table WHERE $fkColumn = ?");
$stmt->execute([$id]);
$items = $stmt->fetchAll();
@@ -230,7 +232,7 @@ class Media extends BaseModel {
return $result;
}
protected function saveCastAssignments($mediaId, $castData) {
protected function saveCastAssignments(int $mediaId, array $castData): void {
foreach ($castData as $member) {
$castId = null;
if (isset($member['id']) && $member['id']) {

View File

@@ -1,22 +1,24 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/Media.php';
abstract class MediaType extends Media {
protected $type;
protected string $type;
public function __construct($pdo) {
public function __construct(PDO $pdo) {
parent::__construct($pdo);
$this->type = $this->getType();
}
abstract protected function getType();
abstract protected function getType(): string;
abstract protected function validateTypeSpecificFields($data);
abstract protected function validateTypeSpecificFields(array $data): array;
abstract protected function getTypeSpecificFields();
abstract protected function getTypeSpecificFields(): array;
public function createWithRelations($data) {
public function createWithRelations(array $data): int {
// Typ setzen
$data['type'] = $this->type;
@@ -26,14 +28,14 @@ abstract class MediaType extends Media {
return parent::createWithRelations($data);
}
public function updateWithRelations($id, $data) {
public function updateWithRelations(int $id, array $data): bool {
// Typ-spezifische Validierung
$this->validateTypeSpecificFields($data);
return parent::updateWithRelations($id, $data);
}
protected function getRequiredFields() {
protected function getRequiredFields(): array {
return [];
}
}

View File

@@ -1,34 +1,37 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/MediaType.php';
require_once __DIR__ . '/../services/ImageHandler.php';
class Movie extends MediaType {
private $imageHandler;
private $isUpdate = false;
private $mediaId = null;
private ImageHandler $imageHandler;
private bool $isUpdate = false;
private ?int $mediaId = null;
public function __construct($pdo) {
public function __construct(PDO $pdo) {
parent::__construct($pdo);
$this->imageHandler = new ImageHandler();
}
protected function getType() {
protected function getType(): string {
return 'Movie';
}
protected function getTypeSpecificFields() {
protected function getTypeSpecificFields(): array {
return ['runtime', 'director', 'writer'];
}
protected function validateTypeSpecificFields($data) {
protected function validateTypeSpecificFields(array $data): array {
// Movies sollten bestimmte Felder haben
if (isset($data['runtime']) && !is_numeric($data['runtime'])) {
throw new Exception('Runtime must be a number');
}
return $data;
}
protected function processPosterField($data) {
protected function processPosterField(array $data): array {
error_log("Movie::processPosterField - Checking for poster field, isUpdate: " . ($this->isUpdate ? 'yes' : 'no'));
// If this is an update and poster is being changed, delete old image
@@ -65,22 +68,22 @@ class Movie extends MediaType {
return $data;
}
public function createWithRelations($data) {
public function createWithRelations(array $data): int {
$data['type'] = 'Movie';
$data = $this->validateTypeSpecificFields($data);
$data = $this->processPosterField($data);
return parent::createWithRelations($data);
}
public function updateWithRelations($id, $data) {
public function updateWithRelations(int $id, array $data): bool {
$this->isUpdate = true;
$this->mediaId = $id;
$this->validateTypeSpecificFields($data);
$data = $this->processPosterField($data);
parent::updateWithRelations($id, $data);
return parent::updateWithRelations($id, $data);
}
public function search($filters = [], $page = 1, $limit = 20) {
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
// Nur Movies suchen
$filters['type'] = 'Movie';
return parent::search($filters, $page, $limit);

View File

@@ -1,27 +1,30 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/MediaType.php';
class Music extends MediaType {
protected function getType() {
protected function getType(): string {
return 'Album';
}
protected function getTypeSpecificFields() {
protected function getTypeSpecificFields(): array {
return ['artist', 'album_type', 'release_date'];
}
protected function validateTypeSpecificFields($data) {
protected function validateTypeSpecificFields(array $data): array {
// Music spezifische Validierung
return $data;
}
public function search($filters = [], $page = 1, $limit = 20) {
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
// Nur Music suchen
$filters['type'] = 'Album';
return parent::search($filters, $page, $limit);
}
public function getWithTracks($id) {
public function getWithTracks(?int $id): ?array {
$media = $this->getWithRelations($id);
if (!$media) {
return null;
@@ -32,7 +35,7 @@ class Music extends MediaType {
return $media;
}
public function getTracks($mediaId) {
public function getTracks(?int $mediaId): array {
$stmt = $this->pdo->prepare("
SELECT * FROM tracks WHERE media_id = ? ORDER BY track_number
");
@@ -40,7 +43,7 @@ class Music extends MediaType {
return $stmt->fetchAll();
}
public function addTrack($mediaId, $trackData) {
public function addTrack(?int $mediaId, array $trackData): int {
$stmt = $this->pdo->prepare("
INSERT INTO tracks (media_id, track_number, title, artist)
VALUES (?, ?, ?, ?)
@@ -52,10 +55,10 @@ class Music extends MediaType {
//$trackData['duration'] ?? null,
$trackData['artist'] ?? null
]);
return $this->pdo->lastInsertId();
return (int)$this->pdo->lastInsertId();
}
public function updateTrack($trackId, $trackData) {
public function updateTrack(?int $trackId, array $trackData): bool {
$fields = [];
$params = [];
@@ -75,13 +78,13 @@ class Music extends MediaType {
return false;
}
public function deleteTrack($trackId) {
public function deleteTrack(?int $trackId): bool {
$stmt = $this->pdo->prepare("DELETE FROM tracks WHERE id = ?");
$stmt->execute([$trackId]);
return $stmt->rowCount() > 0;
}
public function createWithRelations($data) {
public function createWithRelations(array $data): int {
$mediaId = parent::createWithRelations($data);
// Tracks speichern
@@ -94,7 +97,7 @@ class Music extends MediaType {
return $mediaId;
}
public function updateWithRelations($id, $data) {
public function updateWithRelations(int $id, array $data): bool {
parent::updateWithRelations($id, $data);
// Tracks aktualisieren
@@ -106,7 +109,6 @@ class Music extends MediaType {
$this->addTrack($id, $track);
}
}
return true;
}
}

View File

@@ -1,30 +1,33 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/MediaType.php';
class Series extends MediaType {
protected function getType() {
protected function getType(): string {
return 'TV';
}
protected function getTypeSpecificFields() {
protected function getTypeSpecificFields(): array {
return ['runtime', 'director', 'writer'];
}
protected function validateTypeSpecificFields($data) {
protected function validateTypeSpecificFields(array $data): array {
// Series Validierung
if (isset($data['runtime']) && !is_numeric($data['runtime'])) {
throw new Exception('Runtime must be a number');
}
return $data;
}
public function search($filters = [], $page = 1, $limit = 20) {
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
// Nur Series suchen
$filters['type'] = 'TV';
return parent::search($filters, $page, $limit);
}
public function getWithEpisodes($id) {
public function getWithEpisodes(?int $id): ?array {
$media = $this->getWithRelations($id);
if (!$media) {
return null;
@@ -36,7 +39,7 @@ class Series extends MediaType {
return $media;
}
public function getEpisodes($mediaId, $season = null) {
public function getEpisodes(?int $mediaId, ?int $season = null): array {
$query = "SELECT * FROM episodes WHERE media_id = ?";
$params = [$mediaId];
@@ -52,7 +55,7 @@ class Series extends MediaType {
return $stmt->fetchAll();
}
public function getSeasons($mediaId) {
public function getSeasons(?int $mediaId): array {
$stmt = $this->pdo->prepare("
SELECT DISTINCT season, COUNT(*) as episode_count,
MIN(air_date) as first_air_date, MAX(air_date) as last_air_date
@@ -65,7 +68,7 @@ class Series extends MediaType {
return $stmt->fetchAll();
}
public function addEpisode($mediaId, $episodeData) {
public function addEpisode(?int $mediaId, array $episodeData): int {
$stmt = $this->pdo->prepare("
INSERT INTO episodes (media_id, season, episode_number, title, description, air_date, duration, thumbnail)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
@@ -80,10 +83,10 @@ class Series extends MediaType {
$episodeData['duration'] ?? null,
$episodeData['thumbnail'] ?? null
]);
return $this->pdo->lastInsertId();
return (int)$this->pdo->lastInsertId();
}
public function updateEpisode($episodeId, $episodeData) {
public function updateEpisode(?int $episodeId, array $episodeData): bool {
$fields = [];
$params = [];
@@ -103,13 +106,13 @@ class Series extends MediaType {
return false;
}
public function deleteEpisode($episodeId) {
public function deleteEpisode(?int $episodeId): bool {
$stmt = $this->pdo->prepare("DELETE FROM episodes WHERE id = ?");
$stmt->execute([$episodeId]);
return $stmt->rowCount() > 0;
}
public function createWithRelations($data) {
public function createWithRelations(array $data): int {
$mediaId = parent::createWithRelations($data);
// Episoden speichern
@@ -122,7 +125,7 @@ class Series extends MediaType {
return $mediaId;
}
public function updateWithRelations($id, $data) {
public function updateWithRelations(int $id, array $data): bool {
parent::updateWithRelations($id, $data);
// Episoden aktualisieren
@@ -134,7 +137,6 @@ class Series extends MediaType {
$this->addEpisode($id, $episode);
}
}
return true;
}
}

View File

@@ -1,15 +1,17 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/BaseModel.php';
class Settings extends BaseModel {
protected $table = 'settings';
protected string $table = 'settings';
public function __construct($pdo) {
public function __construct(PDO $pdo) {
parent::__construct($pdo);
}
public function getSettings() {
public function getSettings(): ?array {
$stmt = $this->pdo->prepare("SELECT * FROM {$this->table} WHERE id = 1");
$stmt->execute();
$settings = $stmt->fetch();
@@ -25,7 +27,7 @@ class Settings extends BaseModel {
return $settings;
}
public function updateSettings($data) {
public function updateSettings(array $data): ?array {
$updateData = [];
if (isset($data['enabled_categories']) && is_array($data['enabled_categories'])) {

View File

@@ -1,10 +1,13 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../database.php';
class ApiLogger {
private static $instance = null;
private $pdo;
private $enabled;
private static ?ApiLogger $instance = null;
private PDO $pdo;
private bool $enabled;
private function __construct() {
$this->enabled = API_LOGGING_ENABLED;
@@ -12,14 +15,14 @@ class ApiLogger {
$this->pdo = $db->getConnection();
}
public static function getInstance() {
public static function getInstance(): ApiLogger {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function logRequest($method, $path, $params = [], $body = null) {
public function logRequest(string|array $method, string|array $path, array $params = [], string|array|null $body = null): void {
if (!$this->enabled) {
return;
}
@@ -44,12 +47,12 @@ class ApiLogger {
':params' => $paramsValue,
':body' => $bodyValue
]);
} catch (Exception $e) {
} catch (Throwable $e) {
error_log('Failed to log request: ' . $e->getMessage());
}
}
public function logResponse($method, $path, $statusCode, $response) {
public function logResponse(string|array $method, string|array $path, int $statusCode, array $response): void {
if (!$this->enabled) {
return;
}
@@ -65,12 +68,12 @@ class ApiLogger {
':status_code' => $statusCode,
':response' => (json_encode($response) ?: '[encoding_failed]')
]);
} catch (Exception $e) {
} catch (Throwable $e) {
error_log('Failed to log response: ' . $e->getMessage());
}
}
public function logError($method, $path, $error) {
public function logError(string|array $method, string|array $path, string|array $error): void {
if (!$this->enabled) {
return;
}
@@ -85,12 +88,12 @@ class ApiLogger {
':path' => is_array($path) ? (json_encode($path) ?: '[array]') : (string)$path,
':error' => is_array($error) ? (json_encode($error) ?: '[array]') : (string)$error
]);
} catch (Exception $e) {
} catch (Throwable $e) {
error_log('Failed to log error: ' . $e->getMessage());
}
}
public function logDebug($message) {
public function logDebug(string|array $message): void {
if (!$this->enabled) {
return;
}
@@ -103,7 +106,7 @@ class ApiLogger {
$stmt->execute([
':message' => is_array($message) ? (json_encode($message) ?: '[array]') : (string)$message
]);
} catch (Exception $e) {
} catch (Throwable $e) {
error_log('Failed to log debug: ' . $e->getMessage());
}
}

View File

@@ -1,15 +1,17 @@
<?php
class DocumentationService {
private $controllersPath;
private $modelsPath;
declare(strict_types=1);
public function __construct($controllersPath = __DIR__ . '/../controllers/', $modelsPath = __DIR__ . '/../models/') {
class DocumentationService {
private string $controllersPath;
private string $modelsPath;
public function __construct(string $controllersPath = __DIR__ . '/../controllers/', string $modelsPath = __DIR__ . '/../models/') {
$this->controllersPath = $controllersPath;
$this->modelsPath = $modelsPath;
}
public function generateDocumentation() {
public function generateDocumentation(): array {
$docs = [
'title' => 'Media API Documentation',
'version' => '1.0.0',
@@ -27,7 +29,7 @@ class DocumentationService {
return $docs;
}
private function scanControllers() {
private function scanControllers(): array {
$endpoints = [];
$controllerFiles = glob($this->controllersPath . '*Controller.php');
@@ -56,7 +58,7 @@ class DocumentationService {
return $endpoints;
}
private function parseMethodDoc($docComment, $methodName, $className) {
private function parseMethodDoc(?string $docComment, string $methodName, string $className): ?array {
if (!$docComment) {
return null;
}
@@ -106,7 +108,7 @@ class DocumentationService {
return $info;
}
private function inferHttpMethods($methodName) {
private function inferHttpMethods(string $methodName): array {
$methods = [];
if (strpos($methodName, 'get') === 0) {
@@ -129,7 +131,7 @@ class DocumentationService {
return $methods;
}
private function inferPath($className, $methodName) {
private function inferPath(string $className, string $methodName): string {
$resource = strtolower(str_replace('Controller', '', $className));
$path = "/{$resource}";
@@ -156,7 +158,7 @@ class DocumentationService {
return $path;
}
private function scanModels() {
private function scanModels(): array {
$models = [];
$modelFiles = glob($this->modelsPath . '*.php');

View File

@@ -1,10 +1,12 @@
<?php
class ImageHandler {
private $uploadDir;
private $baseUrl;
declare(strict_types=1);
public function __construct($uploadDir = null, $baseUrl = null) {
class ImageHandler {
private string $uploadDir;
private string $baseUrl;
public function __construct(?string $uploadDir = null, ?string $baseUrl = null) {
$this->uploadDir = $uploadDir ?? __DIR__ . '/../public/images/';
$this->baseUrl = $baseUrl ?? '/images/';
@@ -21,7 +23,7 @@ class ImageHandler {
* @param string $prefix Prefix for filename (e.g., 'poster', 'banner')
* @return string|null Relative path to saved image, or null if invalid
*/
public function saveBase64Image($base64Data, $prefix = 'image') {
public function saveBase64Image(string $base64Data, string $prefix = 'image'): ?string {
error_log("ImageHandler: Starting to process base64 image, length: " . strlen($base64Data));
if (empty($base64Data)) {
@@ -119,7 +121,7 @@ class ImageHandler {
/**
* Detect image format from base64 string
*/
private function detectImageFormat($base64String) {
private function detectImageFormat(string $base64String): ?string {
// Decode first few bytes to check magic numbers
$data = base64_decode(substr($base64String, 0, 100));
@@ -140,7 +142,7 @@ class ImageHandler {
/**
* Validate that data is a valid image
*/
private function isValidImage($data) {
private function isValidImage(string $data): bool {
try {
$image = imagecreatefromstring($data);
if ($image !== false) {
@@ -156,14 +158,14 @@ class ImageHandler {
/**
* Generate unique filename
*/
private function generateUniqueFilename($prefix, $extension) {
private function generateUniqueFilename(string $prefix, string $extension): string {
return $prefix . '_' . uniqid() . '_' . time() . '.' . $extension;
}
/**
* Delete an image file
*/
public function deleteImage($imagePath) {
public function deleteImage(?string $imagePath): bool {
if (empty($imagePath)) {
return false;
}