PipelineStepRepository.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 3

Klassen 1

Funktionen 6

Verwendet von 1

Code

<?php

declare(strict_types=1);

namespace Infrastructure\Persistence;

// @responsibility: Persistenz für Pipeline-Steps

use Domain\Repository\PipelineStepRepositoryInterface;

class PipelineStepRepository implements PipelineStepRepositoryInterface
{
    private \PDO $pdo;

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

    public function findSteps(int $pipelineId): array
    {
        $stmt = $this->pdo->prepare(
            'SELECT * FROM pipeline_steps WHERE pipeline_id = ? ORDER BY sort_order ASC'
        );
        $stmt->execute([$pipelineId]);
        $results = $stmt->fetchAll(\PDO::FETCH_ASSOC);

        foreach ($results as &$row) {
            $row['config'] = json_decode($row['config'] ?? '{}', true);
        }

        return $results;
    }

    public function addStep(int $pipelineId, array $stepData): int
    {
        $config = is_array($stepData['config'] ?? null)
            ? json_encode($stepData['config'])
            : ($stepData['config'] ?? '{}');

        $stmt = $this->pdo->prepare(
            'INSERT INTO pipeline_steps (pipeline_id, step_type, config, sort_order, enabled)
             VALUES (?, ?, ?, ?, ?)'
        );
        $stmt->execute([
            $pipelineId,
            $stepData['step_type'],
            $config,
            $stepData['sort_order'] ?? 0,
            $stepData['enabled'] ?? 1,
        ]);

        return (int) $this->pdo->lastInsertId();
    }

    public function updateStep(int $stepId, array $stepData): void
    {
        $sets = [];
        $params = [];

        if (isset($stepData['step_type'])) {
            $sets[] = 'step_type = ?';
            $params[] = $stepData['step_type'];
        }
        if (isset($stepData['config'])) {
            $sets[] = 'config = ?';
            $params[] = is_array($stepData['config'])
                ? json_encode($stepData['config'])
                : $stepData['config'];
        }
        if (isset($stepData['sort_order'])) {
            $sets[] = 'sort_order = ?';
            $params[] = $stepData['sort_order'];
        }
        if (isset($stepData['enabled'])) {
            $sets[] = 'enabled = ?';
            $params[] = $stepData['enabled'];
        }

        if ($sets === []) {
            return;
        }

        $params[] = $stepId;
        $stmt = $this->pdo->prepare(
            'UPDATE pipeline_steps SET ' . implode(', ', $sets) . ' WHERE id = ?'
        );
        $stmt->execute($params);
    }

    public function deleteStep(int $stepId): void
    {
        $stmt = $this->pdo->prepare('DELETE FROM pipeline_steps WHERE id = ?');
        $stmt->execute([$stepId]);
    }

    /**
     * @param array<int> $stepIds
     */
    public function reorderSteps(int $pipelineId, array $stepIds): void
    {
        $stmt = $this->pdo->prepare(
            'UPDATE pipeline_steps SET sort_order = ? WHERE id = ? AND pipeline_id = ?'
        );

        foreach ($stepIds as $order => $stepId) {
            $stmt->execute([$order + 1, $stepId, $pipelineId]);
        }
    }
}
← Übersicht Graph