Lines
60.00%
9 / 15
Methods
60.00%
3 / 5
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| nextId | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| expect | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| resolve | 0.00% | 0 / 5 | 0.00% | 0 / 1 | 6 | ||
| forget | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| failAll | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| 12 | final class MessageCorrelator | |
| 13 | { | |
| 14 | private int $nextId = 1; | |
| 15 | ||
| 16 | /** @var array<int, DeferredFuture<array<string, mixed>>> */ | |
| 17 | private array $pending = []; | |
| 18 | ||
| 19 | public function nextId(): int | |
| 20 | { | |
| 21 | return $this->nextId++; | |
| 22 | } | |
| 23 | ||
| 24 | /** @return Future<array<string, mixed>> */ | |
| 25 | public function expect(int $id): Future | |
| 26 | { | |
| 27 | /** @var DeferredFuture<array<string, mixed>> $deferred */ | |
| 28 | $deferred = new DeferredFuture(); | |
| 29 | $this->pending[$id] = $deferred; | |
| 30 | ||
| 31 | return $deferred->getFuture()->ignore(); | |
| 32 | } | |
| 33 | ||
| 34 | /** @param array<string, mixed> $result */ | |
| 35 | public function resolve(int $id, array $result): void | |
| 36 | { | |
| 37 | $deferred = $this->pending[$id] ?? null; | |
| 38 | ||
| 39 | if ($deferred === null) { | |
| 40 | return; | |
| 41 | } | |
| 42 | ||
| 43 | unset($this->pending[$id]); | |
| 44 | $deferred->complete($result); | |
| 45 | } | |
| 46 | ||
| 47 | public function forget(int $id): void | |
| 48 | { | |
| 49 | unset($this->pending[$id]); | |
| 50 | } | |
| 51 | ||
| 52 | public function failAll(Throwable $error): int | |
| 53 | { | |
| 54 | $pending = $this->pending; | |
| 55 | $this->pending = []; | |
| 56 | ||
| 57 | foreach ($pending as $deferred) { | |
| 58 | $deferred->error($error); | |
| 59 | } | |
| 60 | ||
| 61 | return \count($pending); | |
| 62 | } | |
| 63 | } |