EntityController.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 6

Klassen 1

Funktionen 8

Versionen 18

Code

<?php

declare(strict_types=1);

namespace Controller;

// @responsibility: HTTP-Endpunkte für Semantic Explorer Entitäten (CRUD)

use Domain\Repository\EntityRepositoryInterface;
use Framework\Controller;
use Infrastructure\Formatter\ApiResponseFormatter;

class EntityController extends Controller
{
    private EntityRepositoryInterface $repository;
    private ApiResponseFormatter $apiFormatter;

    public function __construct(
        EntityRepositoryInterface $repository,
        ApiResponseFormatter $apiFormatter
    ) {
        $this->repository = $repository;
        $this->apiFormatter = $apiFormatter;
    }

    /**
     * GET /semantic-explorer/entitaeten
     * Liste aller Entitaeten
     */
    public function index(): void
    {
        $type = $this->getString('type');
        $search = $this->getString('search');

        $entities = $this->repository->findFiltered($type, $search);
        $stats = $this->repository->getStats();

        $this->view('semantic-explorer.entitaeten.index', [
            'title' => 'Entitaeten',
            'entities' => $entities,
            'stats' => $stats,
            'currentType' => $type,
            'currentSearch' => $search,
        ]);
    }

    /**
     * GET /semantic-explorer/entitaeten/{id}
     * Entitaet-Details
     */
    public function show(int $id): void
    {
        $entity = $this->repository->find($id);

        if ($entity === null) {
            $this->notFound('Entitaet nicht gefunden');
        }

        $synonyms = $this->repository->findSynonyms($id);
        $outgoingRelations = $this->repository->getOutgoingRelations($id);
        $incomingRelations = $this->repository->getIncomingRelations($id);
        $chunks = $this->repository->findChunks($id);
        $classifications = $this->repository->findClassifications($id);
        $knowledgeSemantics = $this->repository->getKnowledgeSemantics($id);

        $this->view('semantic-explorer.entitaeten.show', [
            'title' => $entity['name'],
            'entity' => $entity,
            'synonyms' => $synonyms,
            'outgoingRelations' => $outgoingRelations,
            'incomingRelations' => $incomingRelations,
            'chunks' => $chunks,
            'classifications' => $classifications,
            'knowledgeSemantics' => $knowledgeSemantics,
        ]);
    }

    /**
     * GET /semantic-explorer/entitaeten/new
     */
    public function create(): void
    {
        $this->view('semantic-explorer.entitaeten.new', [
            'title' => 'Neue Entitaet',
            'types' => $this->repository->getTypes(),
        ]);
    }

    /**
     * POST /semantic-explorer/entitaeten
     */
    public function store(): void
    {
        $this->requireCsrf();

        $input = $this->getJsonInput();
        if (empty($input)) {
            $input = $_POST;
        }

        $name = trim($input['name'] ?? '');
        $type = trim($input['type'] ?? '');
        $description = trim($input['description'] ?? '') ?: null;

        if ($name === '' || $type === '') {
            if ($this->isHtmxRequest()) {
                $this->htmxError('Name und Typ sind erforderlich');

                return;
            }
            $this->json($this->apiFormatter->validationError('Name und Typ sind erforderlich'), 400);

            return;
        }

        try {
            $id = $this->repository->create($name, $type, $description);

            if ($this->isHtmxRequest()) {
                $this->htmxRedirect('/semantic-explorer/entitaeten/' . $id);

                return;
            }

            $this->json($this->apiFormatter->created($id, 'Entität erstellt'));
        } catch (\Exception $e) {
            if ($this->isHtmxRequest()) {
                $this->htmxError($e->getMessage());

                return;
            }
            $this->json($this->apiFormatter->error($e->getMessage(), 'SERVER_ERROR'), 500);
        }
    }

    /**
     * GET /semantic-explorer/entitaeten/{id}/edit
     */
    public function edit(int $id): void
    {
        $entity = $this->repository->find($id);

        if ($entity === null) {
            $this->notFound('Entitaet nicht gefunden');
        }

        $this->view('semantic-explorer.entitaeten.edit', [
            'title' => 'Entitaet bearbeiten',
            'entity' => $entity,
            'types' => $this->repository->getTypes(),
        ]);
    }

    /**
     * POST /semantic-explorer/entitaeten/{id}
     */
    public function update(int $id): void
    {
        $this->requireCsrf();

        $input = $this->getJsonInput();
        if (empty($input)) {
            $input = $_POST;
        }

        $name = trim($input['name'] ?? '');
        $type = trim($input['type'] ?? '');
        $description = trim($input['description'] ?? '') ?: null;

        if ($name === '' || $type === '') {
            if ($this->isHtmxRequest()) {
                $this->htmxError('Name und Typ sind erforderlich');

                return;
            }
            $this->json($this->apiFormatter->validationError('Name und Typ sind erforderlich'), 400);

            return;
        }

        try {
            $this->repository->update($id, $name, $type, $description);

            if ($this->isHtmxRequest()) {
                $this->htmxRedirect('/semantic-explorer/entitaeten/' . $id);

                return;
            }

            $this->json($this->apiFormatter->ok('Entität aktualisiert'));
        } catch (\Exception $e) {
            if ($this->isHtmxRequest()) {
                $this->htmxError($e->getMessage());

                return;
            }
            $this->json($this->apiFormatter->error($e->getMessage(), 'SERVER_ERROR'), 500);
        }
    }

    /**
     * POST /semantic-explorer/entitaeten/{id}/delete
     */
    public function delete(int $id): void
    {
        $this->requireCsrf();

        try {
            $success = $this->repository->delete($id);
            if ($success) {
                if ($this->isHtmxRequest()) {
                    $this->htmxRedirect('/semantic-explorer/entitaeten');

                    return;
                }
                $this->json($this->apiFormatter->ok('Entität gelöscht'));
            } else {
                if ($this->isHtmxRequest()) {
                    $this->htmxError('Entität hat noch Relationen');

                    return;
                }
                $this->json($this->apiFormatter->error('Entität hat noch Relationen', 'HAS_RELATIONS'), 400);
            }
        } catch (\Exception $e) {
            if ($this->isHtmxRequest()) {
                $this->htmxError($e->getMessage());

                return;
            }
            $this->json($this->apiFormatter->error($e->getMessage(), 'SERVER_ERROR'), 500);
        }
    }
}
← Übersicht Graph