TaxonomyController.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 6

Klassen 1

Funktionen 8

Versionen 14

Code

<?php

declare(strict_types=1);

namespace Controller;

// @responsibility: HTTP-Endpunkte für Semantic Explorer Taxonomie (CRUD)

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

class TaxonomyController extends Controller
{
    private TaxonomyRepositoryInterface $repository;
    private ApiResponseFormatter $apiFormatter;

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

    /**
     * GET /semantic-explorer/taxonomie
     * Hierarchische Kategorisierung
     */
    public function index(): void
    {
        $terms = $this->repository->findAll();
        $hierarchy = $this->buildTaxonomyTree($terms);
        $stats = $this->repository->getStats();

        $this->view('semantic-explorer.taxonomie', [
            'title' => 'Taxonomie',
            'terms' => $terms,
            'hierarchy' => $hierarchy,
            'stats' => $stats,
        ]);
    }

    /**
     * GET /semantic-explorer/taxonomie/new
     */
    public function create(): void
    {
        $this->view('semantic-explorer.taxonomie.new', [
            'title' => 'Neuer Taxonomie-Begriff',
            'terms' => $this->repository->findForSelect(),
        ]);
    }

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

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

        $name = trim($input['name'] ?? '');
        $parentId = isset($input['parent_id']) && $input['parent_id'] !== '' ? (int) $input['parent_id'] : null;

        if ($name === '') {
            if ($this->isHtmxRequest()) {
                $this->htmxError('Name ist erforderlich');

                return;
            }
            $this->json($this->apiFormatter->validationError('Name ist erforderlich', ['name' => 'Pflichtfeld']), 400);

            return;
        }

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

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

                return;
            }

            $this->json($this->apiFormatter->created($id, 'Taxonomie-Begriff 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/taxonomie/{id}/edit
     */
    public function edit(int $id): void
    {
        $term = $this->repository->find($id);

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

        $this->view('semantic-explorer.taxonomie.edit', [
            'title' => 'Begriff bearbeiten',
            'term' => $term,
            'terms' => $this->repository->findForSelect(),
        ]);
    }

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

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

        $name = trim($input['name'] ?? '');
        $parentId = isset($input['parent_id']) && $input['parent_id'] !== '' ? (int) $input['parent_id'] : null;

        if ($name === '') {
            if ($this->isHtmxRequest()) {
                $this->htmxError('Name ist erforderlich');

                return;
            }
            $this->json($this->apiFormatter->validationError('Name ist erforderlich', ['name' => 'Pflichtfeld']), 400);

            return;
        }

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

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

                return;
            }

            $this->json($this->apiFormatter->ok('Taxonomie-Begriff 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/taxonomie/{id}/delete
     */
    public function destroy(int $id): void
    {
        $this->requireCsrf();

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

                    return;
                }
                $this->json($this->apiFormatter->ok('Taxonomie-Begriff gelöscht'));
            } else {
                if ($this->isHtmxRequest()) {
                    $this->htmxError('Begriff hat noch Unterbegriffe');

                    return;
                }
                $this->json($this->apiFormatter->error('Begriff hat noch Unterbegriffe', 'HAS_CHILDREN'), 400);
            }
        } catch (\Exception $e) {
            if ($this->isHtmxRequest()) {
                $this->htmxError($e->getMessage());

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

    /**
     * Baut Baum aus flacher Liste
     */
    private function buildTaxonomyTree(array $items, ?int $parentId = null): array
    {
        $tree = [];
        foreach ($items as $item) {
            if ($item['parent_id'] == $parentId) {
                $item['children'] = $this->buildTaxonomyTree($items, $item['id']);
                $tree[] = $item;
            }
        }

        return $tree;
    }
}
← Übersicht Graph