PipelineStepService.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 7

Klassen 1

Funktionen 5

Verwendet von 3

Versionen 8

Code

<?php

declare(strict_types=1);

namespace Application;

// @responsibility: Service für Pipeline-Step-Operationen (Toggle, Konfiguration)

use Domain\Repository\PipelineConfigRepositoryInterface;
use Domain\Repository\PipelineStepRepositoryInterface;
use Infrastructure\AI\ModelRegistry;
use Infrastructure\Config\PipelineStepConfig;

final class PipelineStepService
{
    public function __construct(
        private PipelineConfigRepositoryInterface $configRepository,
        private PipelineStepRepositoryInterface $stepRepository,
        private ModelRegistry $modelRegistry
    ) {
    }

    /**
     * Toggle a step's enabled status.
     *
     * @return bool True if toggled successfully
     */
    public function toggleStep(int $pipelineId, int $stepId): bool
    {
        $pipeline = $this->configRepository->findById($pipelineId);

        if ($pipeline === null) {
            return false;
        }

        $steps = $this->stepRepository->findSteps($pipelineId);
        foreach ($steps as $step) {
            if ((int) $step['id'] === $stepId) {
                $this->stepRepository->updateStep($stepId, [
                    'enabled' => $step['enabled'] ? 0 : 1,
                ]);

                return true;
            }
        }

        return false;
    }

    /**
     * Update the model for a step.
     *
     * @return array{success: bool, error?: string, model?: string, label?: string}
     */
    public function updateModel(int $pipelineId, int $stepId, string $model): array
    {
        $pipeline = $this->configRepository->findById($pipelineId);

        if ($pipeline === null) {
            return ['success' => false, 'error' => 'Pipeline nicht gefunden'];
        }

        $model = trim($model);

        if ($model === '' || !$this->modelRegistry->isValid($model)) {
            return ['success' => false, 'error' => 'Ungültiges Modell'];
        }

        $steps = $this->stepRepository->findSteps($pipelineId);
        foreach ($steps as $step) {
            if ((int) $step['id'] === $stepId) {
                $config = $step['config'] ?? [];

                // Determine provider from model
                $provider = $this->modelRegistry->isLocal($model) ? 'ollama' : 'anthropic';

                // Update config with new model
                $config['model'] = $this->modelRegistry->isLocal($model)
                    ? substr($model, 7)  // Remove 'ollama:' prefix
                    : $model;
                $config['provider'] = $provider;

                $this->stepRepository->updateStep($stepId, [
                    'config' => $config,
                ]);

                return [
                    'success' => true,
                    'model' => $model,
                    'label' => $this->modelRegistry->getLabel($model),
                ];
            }
        }

        return ['success' => false, 'error' => 'Schritt nicht gefunden'];
    }

    /**
     * Update the collection for a step.
     *
     * @return array{success: bool, error?: string, collection?: string, label?: string}
     */
    public function updateCollection(int $pipelineId, int $stepId, string $collection): array
    {
        $pipeline = $this->configRepository->findById($pipelineId);

        if ($pipeline === null) {
            return ['success' => false, 'error' => 'Pipeline nicht gefunden'];
        }

        $collection = trim($collection);
        $validCollections = array_keys(PipelineStepConfig::getCollections());

        if ($collection === '' || !in_array($collection, $validCollections, true)) {
            return ['success' => false, 'error' => 'Ungültige Collection'];
        }

        $steps = $this->stepRepository->findSteps($pipelineId);
        foreach ($steps as $step) {
            if ((int) $step['id'] === $stepId) {
                $config = $step['config'] ?? [];
                $config['collection'] = $collection;

                $this->stepRepository->updateStep($stepId, [
                    'config' => $config,
                ]);

                $collections = PipelineStepConfig::getCollections();

                return [
                    'success' => true,
                    'collection' => $collection,
                    'label' => $collections[$collection] ?? $collection,
                ];
            }
        }

        return ['success' => false, 'error' => 'Schritt nicht gefunden'];
    }

    /**
     * Create default steps for a new pipeline.
     */
    public function createDefaultSteps(int $pipelineId): void
    {
        foreach (PipelineStepConfig::getDefaultSteps() as $step) {
            $this->stepRepository->addStep($pipelineId, $step);
        }
    }
}
← Übersicht Graph