ChunkDataRepository.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 2

Klassen 1

Funktionen 7

Verwendet von 2

Versionen 1

Code

<?php

declare(strict_types=1);

namespace Infrastructure\Docs;

// @responsibility: Datenbank-Operationen für dokumentation_chunks

use PDO;

final class ChunkDataRepository
{
    public function __construct(
        private PDO $pdo
    ) {
    }

    /**
     * Gets a chunk by ID.
     *
     * @return array<string, mixed>|null
     */
    public function findById(int $id): ?array
    {
        $stmt = $this->pdo->prepare('SELECT * FROM dokumentation_chunks WHERE id = :id');
        $stmt->execute(['id' => $id]);
        $result = $stmt->fetch(PDO::FETCH_ASSOC);

        return $result !== false ? $result : null;
    }

    /**
     * Gets pending chunks for analysis.
     *
     * @return array<array<string, mixed>>
     */
    public function findPending(int $limit): array
    {
        $stmt = $this->pdo->prepare("
            SELECT * FROM dokumentation_chunks
            WHERE analysis_status = 'pending'
            ORDER BY dokumentation_id, chunk_index
            LIMIT :limit
        ");
        $stmt->bindValue('limit', $limit, PDO::PARAM_INT);
        $stmt->execute();

        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    /**
     * Gets document context by ID.
     *
     * @return array{title: string, path: string}
     */
    public function getDocumentContext(int $docId): array
    {
        $stmt = $this->pdo->prepare('SELECT title, path FROM dokumentation WHERE id = :id');
        $stmt->execute(['id' => $docId]);
        $result = $stmt->fetch(PDO::FETCH_ASSOC);

        return [
            'title' => $result['title'] ?? 'Unbekannt',
            'path' => $result['path'] ?? '/',
        ];
    }

    /**
     * Updates chunk analysis status.
     */
    public function updateStatus(int $chunkId, string $status, ?string $error = null): void
    {
        $sql = 'UPDATE dokumentation_chunks
                SET analysis_status = :status, analysis_error = :error
                WHERE id = :id';
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute(['id' => $chunkId, 'status' => $status, 'error' => $error]);
    }

    /**
     * Stores analysis results.
     *
     * @param array{taxonomy: array<string>, entities: array<array{name: string, type: string}>, keywords: array<string>} $analysis
     */
    public function storeAnalysisResults(int $chunkId, array $analysis, string $model): void
    {
        $taxonomyPath = $analysis['taxonomy'];
        $taxonomyCategory = !empty($taxonomyPath) ? $taxonomyPath[0] : null;

        $sql = "UPDATE dokumentation_chunks SET
                taxonomy_category = :category,
                taxonomy_path = :taxonomy,
                entities = :entities,
                keywords = :keywords,
                analysis_model = :model,
                analysis_status = 'completed',
                analysis_error = NULL,
                analyzed_at = NOW()
                WHERE id = :id";

        $stmt = $this->pdo->prepare($sql);
        $stmt->execute([
            'id' => $chunkId,
            'category' => $taxonomyCategory,
            'taxonomy' => json_encode($taxonomyPath),
            'entities' => json_encode($analysis['entities']),
            'keywords' => json_encode($analysis['keywords']),
            'model' => $model,
        ]);
    }

    /**
     * Gets analysis statistics.
     *
     * @return array{pending: int, processing: int, completed: int, failed: int, by_category: array<array{category: string, count: int}>}
     */
    public function getStats(): array
    {
        $stmt = $this->pdo->query("
            SELECT
                SUM(CASE WHEN analysis_status = 'pending' THEN 1 ELSE 0 END) as pending,
                SUM(CASE WHEN analysis_status = 'processing' THEN 1 ELSE 0 END) as processing,
                SUM(CASE WHEN analysis_status = 'completed' THEN 1 ELSE 0 END) as completed,
                SUM(CASE WHEN analysis_status = 'failed' THEN 1 ELSE 0 END) as failed
            FROM dokumentation_chunks
        ");
        $counts = $stmt->fetch(PDO::FETCH_ASSOC);

        $stmt = $this->pdo->query('
            SELECT taxonomy_category as category, COUNT(*) as count
            FROM dokumentation_chunks
            WHERE taxonomy_category IS NOT NULL
            GROUP BY taxonomy_category
            ORDER BY count DESC
        ');
        $byCategory = $stmt->fetchAll(PDO::FETCH_ASSOC);

        return [
            'pending' => (int) ($counts['pending'] ?? 0),
            'processing' => (int) ($counts['processing'] ?? 0),
            'completed' => (int) ($counts['completed'] ?? 0),
            'failed' => (int) ($counts['failed'] ?? 0),
            'by_category' => $byCategory,
        ];
    }
}
← Übersicht Graph