← Code-Übersicht

HealthAction.php

Pfad: src/Infrastructure/Http/HealthAction.php

Ext: php

Größe: 1077 Bytes

Geändert: 2026-07-15 16:13:15+02

<?php
declare(strict_types=1);

namespace Demo\Infrastructure\Http;

use Demo\Application\Health\GetHealth;

/**
 * Treibender Adapter: liefert den Systemzustand als JSON.
 */
final class HealthAction
{
    /** @var GetHealth Injizierter Anwendungsfall. */
    private GetHealth $getHealth;

    /**
     * Nimmt den Anwendungsfall entgegen.
     *
     * @param GetHealth $getHealth Anwendungsfall zur Zustandsermittlung.
     */
    public function __construct(GetHealth $getHealth)
    {
        $this->getHealth = $getHealth;
    }

    /**
     * Ermittelt den Zustand und kodiert ihn als JSON-Antwort.
     *
     * @return Response JSON-Antwort: 200 bei gesund, 503 bei Stoerung.
     */
    public function __invoke(): Response
    {
        $report = $this->getHealth->execute();
        $payload = json_encode(
            ['healthy' => $report->healthy, 'detail' => $report->detail],
            JSON_THROW_ON_ERROR
        );
        $status = $report->healthy ? 200 : 503;
        return new Response($status, 'application/json; charset=UTF-8', $payload);
    }
}