Backup #2133

ID2133
Dateipfad/var/www/dev.campus.systemische-tools.de/src/UseCases/Config/ManageConfigUseCase.php
Version4
Typ modified
Größe5.1 KB
Hash39c3deeb92655af4bddba515b8de5e1c33afda88e357e24ba46817372f2a4450
Datum2025-12-30 20:30:27
Geändert vonclaude-code-hook
GrundClaude Code Pre-Hook Backup vor Edit-Operation
Datei existiert Ja

Dateiinhalt

<?php

declare(strict_types=1);

namespace UseCases\Config;

// @responsibility: Verwaltet Content-Konfigurationen (Profile, Strukturen, Contracts)

use Infrastructure\Persistence\ContentConfigRepository;

final class ManageConfigUseCase
{
    private const VALID_TYPES = ['author_profile', 'structure', 'organization', 'contract', 'rule', 'system_prompt', 'critic'];
    private const VALID_STATUSES = ['draft', 'active', 'deprecated'];

    public function __construct(
        private ContentConfigRepository $configRepo
    ) {
    }

    public function getAll(?string $type = null, ?string $status = null): array
    {
        $validType = in_array($type, self::VALID_TYPES, true) ? $type : null;
        $validStatus = in_array($status, self::VALID_STATUSES, true) ? $status : null;

        return array_map(
            fn (array $row) => ConfigDTO::fromArray($row),
            $this->configRepo->findAll($validType, $validStatus)
        );
    }

    public function getById(int $id): ?ConfigDTO
    {
        $data = $this->configRepo->findById($id);

        return $data !== null ? ConfigDTO::fromArray($data) : null;
    }

    public function create(
        string $type,
        string $name,
        string $slug,
        ?string $description,
        string $content,
        string $version,
        string $status,
        ?int $parentId
    ): ConfigResult {
        $name = trim($name);
        $slug = trim($slug);
        $description = $description !== null ? trim($description) : null;

        if (!in_array($type, self::VALID_TYPES, true)) {
            return ConfigResult::error('Ungültiger Konfigurationstyp.');
        }

        if ($name === '' || $slug === '') {
            return ConfigResult::error('Name und Slug sind erforderlich.');
        }

        if (!$this->isValidJson($content)) {
            return ConfigResult::error('Ungültiges JSON-Format.');
        }

        $id = $this->configRepo->create(
            $type,
            $name,
            $slug,
            $description !== '' ? $description : null,
            $content,
            trim($version) ?: '1.0',
            in_array($status, self::VALID_STATUSES, true) ? $status : 'draft',
            $parentId
        );

        return ConfigResult::success($id, 'Konfiguration erfolgreich erstellt.');
    }

    public function update(
        int $id,
        string $name,
        string $slug,
        ?string $description,
        string $content,
        string $newVersion,
        string $changeDescription,
        string $status,
        ?int $parentId
    ): ConfigResult {
        $name = trim($name);
        $slug = trim($slug);
        $newVersion = trim($newVersion);
        $description = $description !== null ? trim($description) : null;

        $existing = $this->configRepo->findById($id);
        if ($existing === null) {
            return ConfigResult::error('Konfiguration nicht gefunden.');
        }

        if ($name === '' || $slug === '' || $newVersion === '') {
            return ConfigResult::error('Name, Slug und neue Version sind erforderlich.');
        }

        if (!$this->isValidJson($content)) {
            return ConfigResult::error('Ungültiges JSON-Format.');
        }

        $this->configRepo->saveHistory(
            $id,
            $existing['content'],
            $existing['version'],
            'web-ui',
            $changeDescription !== '' ? $changeDescription : 'Update auf Version ' . $newVersion
        );

        $this->configRepo->update(
            $id,
            $name,
            $slug,
            $description !== '' ? $description : null,
            $content,
            $newVersion,
            in_array($status, self::VALID_STATUSES, true) ? $status : $existing['status'],
            $parentId
        );

        return ConfigResult::success($id, 'Konfiguration auf Version ' . $newVersion . ' aktualisiert.');
    }

    public function delete(int $id): ConfigResult
    {
        $existing = $this->configRepo->findById($id);
        if ($existing === null) {
            return ConfigResult::error('Konfiguration nicht gefunden.');
        }

        if ($this->configRepo->countChildren($id) > 0) {
            return ConfigResult::error('Kann nicht gelöscht werden: Es existieren verknüpfte Einträge.');
        }

        $this->configRepo->deleteHistory($id);
        $this->configRepo->delete($id);

        return ConfigResult::success($id, 'Konfiguration gelöscht.');
    }

    public function getChildren(int $id): array
    {
        return $this->configRepo->getChildren($id);
    }

    public function getHistory(int $id): array
    {
        return $this->configRepo->getHistory($id);
    }

    public function getParentOptions(?int $excludeId = null): array
    {
        return $this->configRepo->getParentOptions($excludeId);
    }

    public function getStatistics(): array
    {
        return $this->configRepo->getStatistics();
    }

    private function isValidJson(string $content): bool
    {
        if ($content === 'null') {
            return true;
        }

        json_decode($content, true);

        return json_last_error() === JSON_ERROR_NONE;
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

Andere Versionen dieser Datei

ID Version Typ Größe Datum
2134 5 modified 5.2 KB 2025-12-30 20:30
2133 4 modified 5.1 KB 2025-12-30 20:30
2130 3 modified 5.1 KB 2025-12-30 20:29
839 2 modified 5.2 KB 2025-12-23 08:17
718 1 modified 5.1 KB 2025-12-23 07:55

← Zurück zur Übersicht