Lines
100.00%
12 / 12
Functions and Methods
100.00%
2 / 2
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| StoreHealthBuilder | 100.00% | 12 / 12 | 100.00% | 2 / 2 | 9 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| buildStoreHealth | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 8 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Control\Assembler; | |
| 6 | ||
| 7 | use Stewart\Runtime\Broker\WorkerSlotRegistry; | |
| 8 | use Stewart\Runtime\Config\PersistenceConfig; | |
| 9 | use Stewart\Store\StoreHealth; | |
| 10 | ||
| 11 | final readonly class StoreHealthBuilder | |
| 12 | { | |
| 13 | public function __construct( | |
| 14 | private WorkerSlotRegistry $slots, | |
| 15 | private PersistenceConfig $persistenceConfig, | |
| 16 | ) {} | |
| 17 | ||
| 18 | public function buildStoreHealth(): ?StoreHealth | |
| 19 | { | |
| 20 | if (!$this->persistenceConfig->isStoreConfigured()) { | |
| 21 | return null; | |
| 22 | } | |
| 23 | ||
| 24 | $available = true; | |
| 25 | $newest = null; | |
| 26 | ||
| 27 | foreach ($this->slots->listLiveHandles() as $handle) { | |
| 28 | $reported = $handle->lastPong?->store; | |
| 29 | ||
| 30 | if ($reported === null) { | |
| 31 | continue; | |
| 32 | } | |
| 33 | ||
| 34 | $available = $available && $reported->available; | |
| 35 | ||
| 36 | if ($reported->lastFailureAt !== null && ($newest?->lastFailureAt === null || $newest->lastFailureAt->isBefore($reported->lastFailureAt))) { | |
| 37 | $newest = $reported; | |
| 38 | } | |
| 39 | } | |
| 40 | ||
| 41 | return new StoreHealth($available, $newest?->lastFailure, $newest?->lastFailureAt); | |
| 42 | } | |
| 43 | } |