ContentController.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 4

Klassen 1

Funktionen 3

Versionen 6

Code

<?php

declare(strict_types=1);

namespace Controller\Api;

// @responsibility: REST-API für Content-Aufträge

use Domain\Repository\ContentRepositoryInterface;
use Framework\Controller;

class ContentController extends Controller
{
    private ContentRepositoryInterface $repository;

    public function __construct(ContentRepositoryInterface $repository)
    {
        $this->repository = $repository;
    }

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

        try {
            $order = $this->repository->findOrder($id);

            if ($order === null) {
                $this->sendError('Auftrag nicht gefunden', 404);

                return;
            }

            // Accept both JSON and form data
            $input = $this->getJsonInput();
            if (empty($input)) {
                $input = $_POST;
            }

            if (!isset($input['title']) || trim((string) $input['title']) === '') {
                $this->sendError('Titel ist erforderlich', 400);

                return;
            }

            if (!isset($input['briefing']) || trim((string) $input['briefing']) === '') {
                $this->sendError('Briefing ist erforderlich', 400);

                return;
            }

            $this->repository->updateOrder($id, [
                'title' => $input['title'],
                'briefing' => $input['briefing'],
                'author_profile_id' => !empty($input['author_profile_id']) ? (int) $input['author_profile_id'] : null,
                'contract_id' => !empty($input['contract_id']) ? (int) $input['contract_id'] : null,
                'structure_id' => !empty($input['structure_id']) ? (int) $input['structure_id'] : null,
            ]);

            // HTMX: Redirect to detail page
            if ($this->isHtmxRequest()) {
                $this->htmxRedirect('/content/' . $id);

                return;
            }

            // JSON fallback
            $updatedOrder = $this->repository->findOrder($id);
            $this->json([
                'success' => true,
                'data' => $updatedOrder,
            ]);
        } catch (\Exception $e) {
            error_log(sprintf('[API.Content.update] %s: %s', get_class($e), $e->getMessage()));
            $this->sendError($e->getMessage());
        }
    }

    private function sendError(string $message, int $status = 500): void
    {
        if ($this->isHtmxRequest()) {
            $this->htmxError($message);

            return;
        }
        $this->json(['success' => false, 'error' => $message], $status);
    }
}
← Übersicht Graph