ContentCollectionService.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 4

Klassen 1

Funktionen 5

Verwendet von 3

Versionen 1

Code

<?php

declare(strict_types=1);

namespace Application;

// @responsibility: Service für RAG-Collection-Operationen und Validierung

use Infrastructure\Persistence\CollectionRepository;
use Infrastructure\Validation\CollectionValidator;

final class ContentCollectionService
{
    public function __construct(
        private CollectionRepository $repository,
        private CollectionValidator $validator
    ) {
    }

    /**
     * Get available collections for content generation.
     *
     * @return array<int, array<string, mixed>>
     */
    public function getAvailable(): array
    {
        $collections = $this->repository->getSearchable();

        if ($collections === []) {
            return [
                ['collection_id' => 'documents', 'display_name' => 'Dokumente', 'points_count' => 0, 'vector_size' => 1024],
            ];
        }

        return $collections;
    }

    /**
     * Validate and filter collections against available ones.
     *
     * @param array<string> $collections
     * @return array<string>
     */
    public function validate(array $collections): array
    {
        $availableIds = array_column($this->getAvailable(), 'collection_id');
        $valid = array_filter($collections, fn ($c) => in_array($c, $availableIds, true));

        return array_values($valid);
    }

    /**
     * Check if collections are compatible (same vector dimensions).
     *
     * @param array<string> $collectionIds
     * @return array{valid: bool, error: string|null}
     */
    public function checkCompatibility(array $collectionIds): array
    {
        if (empty($collectionIds)) {
            return ['valid' => true, 'error' => null];
        }

        $result = $this->validator->validateSelection($collectionIds);

        return [
            'valid' => $result->isValid(),
            'error' => $result->getError(),
        ];
    }

    /**
     * Validate collections and check compatibility in one call.
     *
     * @param array<string> $collections
     * @return array{collections: array<string>, valid: bool, error: string|null}
     */
    public function validateWithCompatibility(array $collections): array
    {
        $validated = $this->validate($collections);

        if (empty($validated)) {
            return [
                'collections' => [],
                'valid' => false,
                'error' => 'Keine gültigen Collections ausgewählt',
            ];
        }

        $compatibility = $this->checkCompatibility($validated);

        return [
            'collections' => $validated,
            'valid' => $compatibility['valid'],
            'error' => $compatibility['error'],
        ];
    }
}
← Übersicht Graph