<?php
namespace App\Controller\Api;
use App\Entity\Elevator;
use App\Entity\Pump;
use App\Entity\Sensor;
use App\Entity\Ticket;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
class HealthController extends AbstractController
{
/**
* @Route("/health", name="api_health", methods={"GET"})
*/
public function healthAction(EntityManagerInterface $em): JsonResponse
{
// Leichte Read-Query je Kern-Entity: findOneBy([]) lädt eine volle Zeile mit ALLEN
// gemappten Spalten (LIMIT 1). Fehlt eine Spalte in der DB (Schema-Drift / fehlende
// Migration), wirft die Query hier — so wird genau die "Unknown column"-500er-Klasse
// (z.B. sensor.pending_command, pump.current) beim Deploy-Health-Check sichtbar,
// statt still im Betrieb aufzuschlagen.
$entities = [
'sensor' => Sensor::class,
'pump' => Pump::class,
'elevator' => Elevator::class,
'ticket' => Ticket::class,
];
foreach ($entities as $key => $class) {
try {
$em->getRepository($class)->findOneBy([]);
} catch (\Throwable $e) {
return new JsonResponse([
'status' => 'error',
'check' => $key,
'message' => $e->getMessage(),
], JsonResponse::HTTP_SERVICE_UNAVAILABLE);
}
}
return new JsonResponse(['status' => 'ok']);
}
}