ContentVersionRepository.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 5

Klassen 1

Funktionen 5

Verwendet von 1

Versionen 1

Code

<?php

declare(strict_types=1);

namespace Infrastructure\Persistence;

// @responsibility: Persistenz für Content-Versions

use Domain\Repository\ContentVersionRepositoryInterface;
use Infrastructure\Traits\JsonDecodeTrait;

class ContentVersionRepository implements ContentVersionRepositoryInterface
{
    use JsonDecodeTrait;

    private \PDO $pdo;

    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    public function findVersionsByOrder(int $orderId): array
    {
        $stmt = $this->pdo->prepare('
            SELECT * FROM content_versions
            WHERE order_id = :order_id
            ORDER BY version_number DESC
        ');
        $stmt->execute(['order_id' => $orderId]);

        return $stmt->fetchAll();
    }

    public function findLatestVersion(int $orderId): ?array
    {
        $stmt = $this->pdo->prepare('
            SELECT * FROM content_versions
            WHERE order_id = :order_id
            ORDER BY version_number DESC
            LIMIT 1
        ');
        $stmt->execute(['order_id' => $orderId]);
        $result = $stmt->fetch();

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

    public function findVersion(int $id): ?array
    {
        $stmt = $this->pdo->prepare('SELECT * FROM content_versions WHERE id = :id');
        $stmt->execute(['id' => $id]);
        $result = $stmt->fetch();

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

    public function findCritiquesByVersion(int $versionId): array
    {
        $stmt = $this->pdo->prepare("
            SELECT cc.*, c.name as critic_name
            FROM content_critiques cc
            LEFT JOIN content_config c ON cc.critic_id = c.id AND c.type = 'critic'
            WHERE cc.version_id = :version_id
            ORDER BY cc.round DESC, c.sort_order ASC
        ");
        $stmt->execute(['version_id' => $versionId]);

        $results = $stmt->fetchAll();

        // Parse feedback JSON and merge with row
        foreach ($results as &$row) {
            if (isset($row['feedback']) && $row['feedback'] !== '') {
                $feedback = $this->decodeJsonArray($row['feedback']);
                if ($feedback !== []) {
                    $row = array_merge($row, $feedback);
                }
            }
        }

        return $results;
    }
}
← Übersicht Graph