LoadChatSessionUseCase.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 4

Klassen 1

Funktionen 3

Versionen 7

Code

<?php

declare(strict_types=1);

namespace UseCases\Chat;

// @responsibility: Lädt Chat-Session mit allen Nachrichten

use Domain\Repository\ChatMessageRepositoryInterface;
use Domain\Repository\ChatSessionRepositoryInterface;

class LoadChatSessionUseCase
{
    public function __construct(
        private ChatSessionRepositoryInterface $sessionRepo,
        private ChatMessageRepositoryInterface $messageRepo
    ) {
    }

    /**
     * Load session by UUID with all messages
     */
    public function execute(string $uuid): ?ChatSessionDTO
    {
        $session = $this->sessionRepo->findByUuid($uuid);

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

        $messages = $this->messageRepo->findBySessionId($session->getId() ?? 0);

        return new ChatSessionDTO($session, $messages);
    }

    /**
     * Create a new session and return it
     *
     * @param array<string> $collections
     */
    public function createNew(
        string $model = 'claude-opus-4-5-20251101',
        array $collections = ['documents'],
        int $contextLimit = 5
    ): ChatSessionDTO {
        $uuid = $this->sessionRepo->generateUuid();
        $metadata = \Domain\ValueObject\SessionMetadata::create($model, $collections, $contextLimit);
        $session = \Domain\Factory\ChatSessionFactory::createNew(
            \Domain\ValueObject\SessionUuid::fromString($uuid),
            $metadata
        );
        $sessionId = $this->sessionRepo->save($session);

        // Reload to get full data including timestamps
        $savedSession = $this->sessionRepo->findByUuid($uuid);

        return new ChatSessionDTO($savedSession ?? $session->withId($sessionId), []);
    }
}
← Übersicht Graph