ConfigController.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 4

Klassen 1

Funktionen 3

Code

<?php

declare(strict_types=1);

namespace Controller\Api;

// @responsibility: JSON-API für Config-Operationen (Chat-Integration)

use Framework\Controller;
use UseCases\Config\ManageConfigUseCase;

class ConfigController extends Controller
{
    public function __construct(
        private ManageConfigUseCase $configUseCase
    ) {
    }

    /**
     * GET /api/v1/config/{id}
     */
    public function show(string $id): void
    {
        $config = $this->configUseCase->getById((int) $id);

        if ($config === null) {
            $this->json(['error' => 'Config nicht gefunden'], 404);

            return;
        }

        $this->json([
            'id' => $config->id,
            'name' => $config->name,
            'type' => $config->type,
            'content' => $config->content,
            'version' => $config->version,
        ]);
    }

    /**
     * POST /api/v1/config/{id}
     */
    public function update(string $id): void
    {
        $this->requireCsrf();

        $config = $this->configUseCase->getById((int) $id);
        if ($config === null) {
            $this->json(['error' => 'Config nicht gefunden'], 404);

            return;
        }

        $newContent = $_POST['content'] ?? '';

        // Auto-increment Version
        $currentVersion = $config->version ?? '1.0';
        $versionParts = explode('.', $currentVersion);
        $newVersion = $versionParts[0] . '.' . ((int) ($versionParts[1] ?? 0) + 1);

        $result = $this->configUseCase->update(
            id: (int) $id,
            name: $config->name,
            slug: $config->slug ?? '',
            description: $config->description,
            content: $newContent,
            newVersion: $newVersion,
            changeDescription: 'Chat UI Update',
            status: $config->status ?? 'active',
            parentId: $config->parentId
        );

        if (!$result->success) {
            $this->json(['error' => $result->message], 400);

            return;
        }

        $this->json([
            'success' => true,
            'version' => $newVersion,
            'message' => "Version {$newVersion} gespeichert",
        ]);
    }
}
← Übersicht Graph