StopwordController.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 4

Klassen 1

Funktionen 8

Versionen 14

Code

<?php

declare(strict_types=1);

namespace Controller;

// @responsibility: HTTP-Endpunkte für Stopword-Verwaltung (Entity-Extraction Filter)

use Domain\Repository\StopwordRepositoryInterface;
use Framework\Controller;

class StopwordController extends Controller
{
    private StopwordRepositoryInterface $repo;

    public function __construct(StopwordRepositoryInterface $repo)
    {
        $this->repo = $repo;
    }

    public function index(): void
    {
        $category = $this->getString('category');
        $showAll = $this->getString('all') === '1';

        $this->view('semantic-explorer.stopwords.index', [
            'title' => 'Stopwords - Semantic Explorer',
            'stopwords' => $this->repo->findAll(!$showAll, $category ?: null),
            'stats' => $this->repo->getStats(),
            'currentCategory' => $category,
            'showAll' => $showAll,
        ]);
    }

    public function create(): void
    {
        $this->view('semantic-explorer.stopwords.new', [
            'title' => 'Neues Stopword - Semantic Explorer',
        ]);
    }

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

        $isHtmx = $this->isHtmxRequest();
        $word = trim($this->getString('word'));

        if ($word === '') {
            if ($isHtmx) {
                $this->htmxError('Wort darf nicht leer sein.');

                return;
            }
            $_SESSION['error'] = 'Wort darf nicht leer sein.';
            $this->redirect('/semantic-explorer/stopwords/new');
        }

        try {
            $id = $this->repo->create([
                'word' => $word,
                'category' => $this->getString('category') ?: 'generic',
                'reason' => $this->getString('reason') ?: null,
                'is_active' => isset($_POST['is_active']) ? 1 : 0,
            ]);

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

                return;
            }

            $_SESSION['success'] = "Stopword '{$word}' wurde erstellt.";
            $this->redirect('/semantic-explorer/stopwords/' . $id);
        } catch (\PDOException $e) {
            $errorMsg = str_contains($e->getMessage(), 'Duplicate')
                ? "Stopword '{$word}' existiert bereits."
                : 'Fehler beim Speichern: ' . $e->getMessage();

            // @phpstan-ignore if.alwaysFalse (Exception thrown before success-path check)
            if ($isHtmx) {
                $this->htmxError($errorMsg);

                return;
            }

            $_SESSION['error'] = $errorMsg;
            $this->redirect('/semantic-explorer/stopwords/new');
        }
    }

    public function show(string $id): void
    {
        $stopword = $this->repo->find((int) $id);
        if ($stopword === null) {
            $this->notFound('Stopword nicht gefunden');
        }

        $this->view('semantic-explorer.stopwords.edit', [
            'title' => 'Stopword: ' . $stopword['word'],
            'stopword' => $stopword,
        ]);
    }

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

        $isHtmx = $this->isHtmxRequest();
        $stopword = $this->repo->find((int) $id);

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

        $word = trim($this->getString('word'));
        if ($word === '') {
            if ($isHtmx) {
                $this->htmxError('Wort darf nicht leer sein.');

                return;
            }
            $_SESSION['error'] = 'Wort darf nicht leer sein.';
            $this->redirect('/semantic-explorer/stopwords/' . $id);
        }

        try {
            $this->repo->update((int) $id, [
                'word' => $word,
                'category' => $this->getString('category') ?: 'generic',
                'reason' => $this->getString('reason') ?: null,
                'is_active' => isset($_POST['is_active']) ? 1 : 0,
            ]);

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

                return;
            }

            $_SESSION['success'] = 'Stopword wurde aktualisiert.';
            $this->redirect('/semantic-explorer/stopwords/' . $id);
        } catch (\PDOException $e) {
            $errorMsg = str_contains($e->getMessage(), 'Duplicate')
                ? "Stopword '{$word}' existiert bereits."
                : 'Fehler beim Speichern: ' . $e->getMessage();

            // @phpstan-ignore if.alwaysFalse (Exception thrown before success-path check)
            if ($isHtmx) {
                $this->htmxError($errorMsg);

                return;
            }

            $_SESSION['error'] = $errorMsg;
            $this->redirect('/semantic-explorer/stopwords/' . $id);
        }
    }

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

        $stopword = $this->repo->find((int) $id);
        if ($stopword === null) {
            $this->notFound('Stopword nicht gefunden');
        }

        $this->repo->delete((int) $id);

        if ($this->isHtmxRequest()) {
            $this->htmxRedirect('/semantic-explorer/stopwords');

            return;
        }

        $_SESSION['success'] = "Stopword '{$stopword['word']}' wurde geloescht.";
        $this->redirect('/semantic-explorer/stopwords');
    }

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

        $stopword = $this->repo->find((int) $id);
        if ($stopword === null) {
            $this->notFound('Stopword nicht gefunden');
        }

        $this->repo->toggleActive((int) $id);

        if ($this->isHtmxRequest()) {
            $this->htmxRedirect('/semantic-explorer/stopwords');

            return;
        }

        $newState = $stopword['is_active'] ? 'deaktiviert' : 'aktiviert';
        $_SESSION['success'] = "Stopword '{$stopword['word']}' wurde {$newState}.";
        $this->redirect('/semantic-explorer/stopwords');
    }
}
← Übersicht Graph