|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Http\Client; |
| 4 | + |
| 5 | +use GuzzleHttp\Promise\PromiseInterface; |
| 6 | +use Illuminate\Support\Traits\ForwardsCalls; |
| 7 | + |
| 8 | +/** |
| 9 | + * A decorated Promise which allows for chaining callbacks. |
| 10 | + */ |
| 11 | +class FluentPromise implements PromiseInterface |
| 12 | +{ |
| 13 | + use ForwardsCalls; |
| 14 | + |
| 15 | + /** |
| 16 | + * Create a new fluent promise instance. |
| 17 | + * |
| 18 | + * @param \GuzzleHttp\Promise\PromiseInterface $guzzlePromise |
| 19 | + */ |
| 20 | + public function __construct(protected PromiseInterface $guzzlePromise) |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + #[\Override] |
| 25 | + public function then(?callable $onFulfilled = null, ?callable $onRejected = null): PromiseInterface |
| 26 | + { |
| 27 | + return $this->__call('then', [$onFulfilled, $onRejected]); |
| 28 | + } |
| 29 | + |
| 30 | + #[\Override] |
| 31 | + public function otherwise(callable $onRejected): PromiseInterface |
| 32 | + { |
| 33 | + return $this->__call('otherwise', [$onRejected]); |
| 34 | + } |
| 35 | + |
| 36 | + #[\Override] |
| 37 | + public function resolve($value): void |
| 38 | + { |
| 39 | + $this->guzzlePromise->resolve($value); |
| 40 | + } |
| 41 | + |
| 42 | + #[\Override] |
| 43 | + public function reject($reason): void |
| 44 | + { |
| 45 | + $this->guzzlePromise->reject($reason); |
| 46 | + } |
| 47 | + |
| 48 | + #[\Override] |
| 49 | + public function cancel(): void |
| 50 | + { |
| 51 | + $this->guzzlePromise->cancel(); |
| 52 | + } |
| 53 | + |
| 54 | + #[\Override] |
| 55 | + public function wait(bool $unwrap = true) |
| 56 | + { |
| 57 | + return $this->__call('wait', [$unwrap]); |
| 58 | + } |
| 59 | + |
| 60 | + #[\Override] |
| 61 | + public function getState(): string |
| 62 | + { |
| 63 | + return $this->guzzlePromise->getState(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get the underlying Guzzle promise. |
| 68 | + * |
| 69 | + * @return \GuzzleHttp\Promise\PromiseInterface |
| 70 | + */ |
| 71 | + public function getGuzzlePromise(): PromiseInterface |
| 72 | + { |
| 73 | + return $this->guzzlePromise; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Proxy requests to the underlying promise interface and update the local promise. |
| 78 | + * |
| 79 | + * @param string $method |
| 80 | + * @param array $parameters |
| 81 | + * @return mixed |
| 82 | + */ |
| 83 | + public function __call($method, $parameters) |
| 84 | + { |
| 85 | + $result = $this->forwardCallTo($this->guzzlePromise, $method, $parameters); |
| 86 | + |
| 87 | + if (! $result instanceof PromiseInterface) { |
| 88 | + return $result; |
| 89 | + } |
| 90 | + |
| 91 | + $this->guzzlePromise = $result; |
| 92 | + |
| 93 | + return $this; |
| 94 | + } |
| 95 | +} |
0 commit comments