ApiHealth.php 1015 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\ApiClient;
  4. use DateTimeImmutable;
  5. /**
  6. * Tracks the most recent ApiClient call's success/failure for the
  7. * UI's `/healthz` payload.
  8. *
  9. * SPEC §M08.8: a background ticker is overkill — just remember the
  10. * last call result. Healthz returns 200 even when the api is down so
  11. * orchestrators don't kill the UI just because the api is briefly
  12. * unreachable; the body carries `api_reachable` and `last_api_check_at`.
  13. */
  14. final class ApiHealth
  15. {
  16. private ?bool $reachable = null;
  17. private ?DateTimeImmutable $lastSuccessAt = null;
  18. public function recordSuccess(DateTimeImmutable $at): void
  19. {
  20. $this->reachable = true;
  21. $this->lastSuccessAt = $at;
  22. }
  23. public function recordFailure(): void
  24. {
  25. $this->reachable = false;
  26. }
  27. public function isReachable(): ?bool
  28. {
  29. return $this->reachable;
  30. }
  31. public function lastSuccessAt(): ?DateTimeImmutable
  32. {
  33. return $this->lastSuccessAt;
  34. }
  35. }