Lines
90.90%
10 / 11
Methods
80.00%
4 / 5
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| cancel | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| isPending | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| fire | 75.00% | 3 / 4 | 0.00% | 0 / 1 | 2.06 | ||
| release | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| 11 | final class ManualTimer implements TimerHandle | |
| 12 | { | |
| 13 | private bool $pending = true; | |
| 14 | ||
| 15 | /** | |
| 16 | * @param Closure(): void $callback | |
| 17 | * @param Closure(self): void $onRelease | |
| 18 | */ | |
| 19 | public function __construct( | |
| 20 | public readonly MonotonicTime $dueAt, | |
| 21 | private readonly Closure $callback, | |
| 22 | private readonly Closure $onRelease, | |
| 23 | ) {} | |
| 24 | ||
| 25 | public function cancel(): void | |
| 26 | { | |
| 27 | $this->release(); | |
| 28 | } | |
| 29 | ||
| 30 | public function isPending(): bool | |
| 31 | { | |
| 32 | return $this->pending; | |
| 33 | } | |
| 34 | ||
| 35 | public function fire(): void | |
| 36 | { | |
| 37 | if (!$this->pending) { | |
| 38 | return; | |
| 39 | } | |
| 40 | ||
| 41 | $this->release(); | |
| 42 | ($this->callback)(); | |
| 43 | } | |
| 44 | ||
| 45 | private function release(): void | |
| 46 | { | |
| 47 | if (!$this->pending) { | |
| 48 | return; | |
| 49 | } | |
| 50 | ||
| 51 | $this->pending = false; | |
| 52 | ($this->onRelease)($this); | |
| 53 | } | |
| 54 | } |