Lines
100.00%
12 / 12
Functions and Methods
100.00%
6 / 6
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| PendingRequest | 100.00% | 12 / 12 | 100.00% | 6 / 6 | 7 | 100.00% | 1 / 1 | |||
| __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 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Worker; | |
| 6 | ||
| 7 | use Amp\DeferredFuture; | |
| 8 | use Amp\Future; | |
| 9 | use Closure; | |
| 10 | use Stewart\Contracts\Exception\ExceptionReason; | |
| 11 | use Stewart\Contracts\Exception\StewartException; | |
| 12 | use Stewart\Runtime\Ipc\Message\RequestFailed; | |
| 13 | use Stewart\Runtime\Model\CorrelationId; | |
| 14 | use Throwable; | |
| 15 | ||
| 16 | /** @template TResult of object */ | |
| 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 | } |