| Total Complexity | 9 |
| Total Lines | 64 |
| Duplicated Lines | 0 % |
| Coverage | 91.67% |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 21 | final class CacheLoadAverage implements Server |
||
| 22 | { |
||
| 23 | private Server $server; |
||
| 24 | private Clock $clock; |
||
| 25 | private ElapsedPeriod $threshold; |
||
| 26 | private ?PointInTime $cachedAt = null; |
||
| 27 | private ?LoadAverage $data = null; |
||
| 28 | 18 | ||
| 29 | public function __construct( |
||
| 30 | Server $server, |
||
| 31 | Clock $clock, |
||
| 32 | ElapsedPeriod $threshold |
||
| 33 | 18 | ) { |
|
| 34 | 18 | $this->server = $server; |
|
| 35 | 18 | $this->clock = $clock; |
|
| 36 | 18 | $this->threshold = $threshold; |
|
| 37 | } |
||
| 38 | 3 | ||
| 39 | public function cpu(): Cpu |
||
| 40 | 3 | { |
|
| 41 | return $this->server->cpu(); |
||
| 42 | } |
||
| 43 | 3 | ||
| 44 | public function memory(): Memory |
||
| 45 | 3 | { |
|
| 46 | return $this->server->memory(); |
||
| 47 | } |
||
| 48 | 3 | ||
| 49 | public function processes(): Processes |
||
| 50 | 3 | { |
|
| 51 | return $this->server->processes(); |
||
| 52 | } |
||
| 53 | 3 | ||
| 54 | /** |
||
| 55 | 3 | * @psalm-suppress InvalidNullableReturnType |
|
| 56 | */ |
||
| 57 | public function loadAverage(): LoadAverage |
||
| 58 | 3 | { |
|
| 59 | 3 | $now = $this->clock->now(); |
|
| 60 | 3 | ||
| 61 | if ( |
||
| 62 | $this->cachedAt && |
||
| 63 | 3 | $this->threshold->longerThan( |
|
| 64 | $now->elapsedSince($this->cachedAt) |
||
| 65 | ) |
||
| 66 | 3 | ) { |
|
| 67 | 3 | /** @psalm-suppress NullableReturnStatement */ |
|
| 68 | return $this->data; |
||
|
|
|||
| 69 | 3 | } |
|
| 70 | |||
| 71 | $this->data = $this->server->loadAverage(); |
||
| 72 | 3 | $this->cachedAt = $now; |
|
| 73 | |||
| 74 | 3 | return $this->data; |
|
| 75 | } |
||
| 76 | |||
| 77 | public function disk(): Disk |
||
| 78 | { |
||
| 79 | return $this->server->disk(); |
||
| 80 | } |
||
| 81 | |||
| 82 | public function tmp(): Path |
||
| 85 | } |
||
| 86 | } |
||
| 87 |