From 9b3215e6dc1a522acfbb45339f1776eecf1c731c Mon Sep 17 00:00:00 2001 From: Felix Schneider Date: Mon, 2 Jun 2025 11:53:13 +0200 Subject: [PATCH] feat: sort health collection by state --- src/Components/Health/HealthCollection.php | 14 ++++++++++++++ src/Controller/HealthController.php | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/src/Components/Health/HealthCollection.php b/src/Components/Health/HealthCollection.php index 854d4fcf..ca67e5cd 100644 --- a/src/Components/Health/HealthCollection.php +++ b/src/Components/Health/HealthCollection.php @@ -11,6 +11,20 @@ */ class HealthCollection extends Collection { + private const STATE_PRIORITY = [ + SettingsResult::ERROR => 0, + SettingsResult::WARNING => 1, + SettingsResult::INFO => 2, + SettingsResult::GREEN => 3, + ]; + + public function sortByState(): void + { + $this->sort(static function (SettingsResult $a, SettingsResult $b) { + return (self::STATE_PRIORITY[$a->state] ?? -1) <=> (self::STATE_PRIORITY[$b->state] ?? -1); + }); + } + protected function getExpectedClass(): ?string { return SettingsResult::class; diff --git a/src/Controller/HealthController.php b/src/Controller/HealthController.php index b3f641fb..0c0544d5 100644 --- a/src/Controller/HealthController.php +++ b/src/Controller/HealthController.php @@ -38,6 +38,8 @@ public function status(): JsonResponse $checker->collect($collection); } + $collection->sortByState(); + return new JsonResponse($collection); } @@ -49,6 +51,8 @@ public function performanceStatus(): JsonResponse $checker->collect($collection); } + $collection->sortByState(); + return new JsonResponse($collection); }