ConfigController.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 6

Klassen 1

Funktionen 8

Versionen 22

Code

<?php

declare(strict_types=1);

namespace Controller;

// @responsibility: HTTP-Endpunkte für Content-Konfiguration (Profile, Strukturen, Contracts, Critics)

use Domain\Repository\PromptsRepositoryInterface;
use Framework\Controller;
use UseCases\Config\ManageConfigUseCase;

class ConfigController extends Controller
{
    private ManageConfigUseCase $configUseCase;
    private PromptsRepositoryInterface $promptsRepo;

    private const TYPES = ['author_profile', 'structure', 'organization', 'contract', 'rule', 'system_prompt', 'critic'];
    private const TYPE_LABELS = [
        'author_profile' => 'Autorenprofil',
        'structure' => 'Struktur',
        'organization' => 'Organisation',
        'contract' => 'Contract',
        'rule' => 'Regel',
        'system_prompt' => 'System-Prompt',
        'critic' => 'Kritiker',
    ];

    public function __construct(ManageConfigUseCase $configUseCase, PromptsRepositoryInterface $promptsRepo)
    {
        $this->configUseCase = $configUseCase;
        $this->promptsRepo = $promptsRepo;
    }

    public function index(): void
    {
        $typeFilter = $this->getString('type');
        $statusFilter = $this->getString('status');

        $this->view('config.index', [
            'title' => 'Content-Konfiguration',
            'configs' => array_map(fn ($dto) => $dto->toArray(), $this->configUseCase->getAll($typeFilter, $statusFilter)),
            'stats' => $this->configUseCase->getStatistics(),
            'types' => self::TYPES,
            'typeLabels' => self::TYPE_LABELS,
            'currentType' => $typeFilter,
            'currentStatus' => $statusFilter,
        ]);
    }

    public function configNew(): void
    {
        $this->view('config.form', [
            'title' => 'Neue Konfiguration',
            'config' => null,
            'types' => self::TYPES,
            'typeLabels' => self::TYPE_LABELS,
            'parents' => $this->configUseCase->getParentOptions(),
            'prompts' => $this->promptsRepo->findActivePrompts(),
            'isEdit' => false,
        ]);
    }

    public function store(): void
    {
        $this->requireCsrf();

        $isHtmx = $this->isHtmxRequest();
        $result = $this->configUseCase->create(
            type: $_POST['type'] ?? '',
            name: $_POST['name'] ?? '',
            slug: $_POST['slug'] ?? '',
            description: $_POST['description'] ?? null,
            content: $_POST['content'] ?? '{}',
            version: $_POST['version'] ?? '1.0',
            status: $_POST['status'] ?? 'draft',
            parentId: !empty($_POST['parent_id']) ? (int) $_POST['parent_id'] : null,
            promptId: !empty($_POST['prompt_id']) ? (int) $_POST['prompt_id'] : null,
            sortOrder: (int) ($_POST['sort_order'] ?? 0)
        );

        if (!$result->success) {
            if ($isHtmx) {
                $this->htmxError($result->message);

                return;
            }
            $_SESSION['error'] = $result->message;
            $this->redirect('/config/new');
        }

        if ($isHtmx) {
            $this->htmxRedirect('/config/' . $result->id);

            return;
        }

        $_SESSION['success'] = $result->message;
        $this->redirect('/config/' . $result->id);
    }

    public function show(string $id): void
    {
        $config = $this->configUseCase->getById((int) $id);
        if ($config === null) {
            $this->notFound('Konfiguration nicht gefunden');
        }

        $this->view('config.show', [
            'title' => $config->name,
            'config' => $config->toArray(),
            'children' => $this->configUseCase->getChildren((int) $id),
            'history' => $this->configUseCase->getHistory((int) $id),
            'typeLabels' => self::TYPE_LABELS,
        ]);
    }

    public function edit(string $id): void
    {
        $config = $this->configUseCase->getById((int) $id);
        if ($config === null) {
            $this->notFound('Konfiguration nicht gefunden');
        }

        $this->view('config.form', [
            'title' => 'Bearbeiten: ' . $config->name,
            'config' => $config->toArray(),
            'types' => self::TYPES,
            'typeLabels' => self::TYPE_LABELS,
            'parents' => $this->configUseCase->getParentOptions((int) $id),
            'prompts' => $this->promptsRepo->findActivePrompts(),
            'isEdit' => true,
        ]);
    }

    public function update(string $id): void
    {
        $this->requireCsrf();

        $isHtmx = $this->isHtmxRequest();
        $result = $this->configUseCase->update(
            id: (int) $id,
            name: $_POST['name'] ?? '',
            slug: $_POST['slug'] ?? '',
            description: $_POST['description'] ?? null,
            content: $_POST['content'] ?? '{}',
            newVersion: $_POST['new_version'] ?? '',
            changeDescription: $_POST['change_description'] ?? '',
            status: $_POST['status'] ?? 'draft',
            parentId: !empty($_POST['parent_id']) ? (int) $_POST['parent_id'] : null,
            promptId: !empty($_POST['prompt_id']) ? (int) $_POST['prompt_id'] : null,
            sortOrder: (int) ($_POST['sort_order'] ?? 0)
        );

        if (!$result->success) {
            if ($isHtmx) {
                $this->htmxError($result->message);

                return;
            }
            $_SESSION['error'] = $result->message;
            $this->redirect('/config/' . $id . '/edit');
        }

        if ($isHtmx) {
            $this->htmxRedirect('/config/' . $id);

            return;
        }

        $_SESSION['success'] = $result->message;
        $this->redirect('/config/' . $id);
    }

    public function delete(string $id): void
    {
        $this->requireCsrf();

        $isHtmx = $this->isHtmxRequest();
        $result = $this->configUseCase->delete((int) $id);

        if (!$result->success) {
            if ($isHtmx) {
                $this->htmxError($result->message);

                return;
            }
            $_SESSION['error'] = $result->message;
            $this->redirect('/config/' . $id);
        }

        if ($isHtmx) {
            $this->htmxRedirect('/config');

            return;
        }

        $_SESSION['success'] = $result->message;
        $this->redirect('/config');
    }
}
← Übersicht Graph