ViewProtokollUseCase.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 3

Klassen 1

Funktionen 7

Verwendet von 2

Versionen 2

Code

<?php

declare(strict_types=1);

namespace UseCases\Protokoll;

// @responsibility: Liest KI-Protokoll-Einträge mit Filterung und Pagination

use Domain\ValueObject\Pagination;
use Infrastructure\Persistence\KiProtokollRepository;

final class ViewProtokollUseCase
{
    public function __construct(
        private KiProtokollRepository $protokollRepo
    ) {
    }

    public function getPaginated(
        ?string $search,
        ?string $status,
        ?string $model,
        Pagination $pagination
    ): array {
        $totalCount = $this->protokollRepo->countFiltered($search, $status, $model);
        $updatedPagination = $pagination->withTotal($totalCount);

        $entries = $this->protokollRepo->findPaginated(
            $search,
            $status,
            $model,
            $updatedPagination->limit,
            $updatedPagination->offset
        );

        return [
            'entries' => array_map(fn (array $row) => ProtokollDTO::fromArray($row), $entries),
            'pagination' => $updatedPagination,
        ];
    }

    public function getById(int $id): ?ProtokollDTO
    {
        $entry = $this->protokollRepo->findById($id);

        if ($entry === null) {
            return null;
        }

        $entry['request_formatted'] = $this->formatJson($entry['request']);
        $entry['response_formatted'] = $this->formatJson($entry['response']);

        return ProtokollDTO::fromArray($entry);
    }

    public function getNavigation(int $id): array
    {
        return [
            'prevId' => $this->protokollRepo->findPreviousId($id),
            'nextId' => $this->protokollRepo->findNextId($id),
        ];
    }

    public function getStatistics(): array
    {
        return $this->protokollRepo->getStatistics();
    }

    public function getDistinctModels(): array
    {
        return $this->protokollRepo->getDistinctModels();
    }

    private function formatJson(?string $json): string
    {
        if ($json === null || $json === '') {
            return '-';
        }

        $decoded = json_decode($json, true);
        if ($decoded === null) {
            return htmlspecialchars($json);
        }

        $formatted = json_encode($decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);

        return $formatted !== false ? $formatted : htmlspecialchars($json);
    }
}
← Übersicht Graph