Lines
100.00%
12 / 12
Methods
100.00%
6 / 6
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| getFuture | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| complete | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| failFromWire | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| failAsUnreachable | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| fail | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| 17 | final readonly class PendingRequest | |
| 18 | { | |
| 19 | /** @var DeferredFuture<TResult> */ | |
| 20 | private DeferredFuture $deferred; | |
| 21 | ||
| 22 | /** | |
| 23 | * @param class-string<TResult> $resultClass | |
| 24 | * @param Closure(string): StewartException<ExceptionReason> $unreachable | |
| 25 | */ | |
| 26 | public function __construct( | |
| 27 | public CorrelationId $correlationId, | |
| 28 | private string $resultClass, | |
| 29 | private Closure $unreachable, | |
| 30 | ) { | |
| 31 | /** @var DeferredFuture<TResult> $deferred */ | |
| 32 | $deferred = new DeferredFuture(); | |
| 33 | $this->deferred = $deferred; | |
| 34 | } | |
| 35 | ||
| 36 | /** @return Future<TResult> */ | |
| 37 | public function getFuture(): Future | |
| 38 | { | |
| 39 | return $this->deferred->getFuture(); | |
| 40 | } | |
| 41 | ||
| 42 | public function complete(object $result): void | |
| 43 | { | |
| 44 | if (!$result instanceof $this->resultClass) { | |
| 45 | $this->failAsUnreachable(\sprintf('the broker answered with %s', get_debug_type($result))); | |
| 46 | ||
| 47 | return; | |
| 48 | } | |
| 49 | ||
| 50 | $this->deferred->complete($result); | |
| 51 | } | |
| 52 | ||
| 53 | public function failFromWire(RequestFailed $failure): void | |
| 54 | { | |
| 55 | $fallback = ($this->unreachable)($failure->message); | |
| 56 | $reason = $fallback->reason::tryFrom($failure->details->reason) ?? $fallback->reason; | |
| 57 | ||
| 58 | $this->fail($fallback::restoreFromWire($reason, $failure->message, $failure->details->context)); | |
| 59 | } | |
| 60 | ||
| 61 | public function failAsUnreachable(string $detail): void | |
| 62 | { | |
| 63 | $this->fail(($this->unreachable)($detail)); | |
| 64 | } | |
| 65 | ||
| 66 | public function fail(Throwable $error): void | |
| 67 | { | |
| 68 | $this->deferred->error($error); | |
| 69 | } | |
| 70 | } |