| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient\Response; |
| 13 |
|
| 14 |
use GuzzleHttp\Promise\Create; |
| 15 |
use GuzzleHttp\Promise\PromiseInterface as GuzzlePromiseInterface; |
| 16 |
use Http\Promise\Promise as HttplugPromiseInterface; |
| 17 |
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
final class HttplugPromise implements HttplugPromiseInterface |
| 25 |
{ |
| 26 |
public function __construct( |
| 27 |
private GuzzlePromiseInterface $promise, |
| 28 |
) { |
| 29 |
} |
| 30 |
|
| 31 |
public function then(?callable $onFulfilled = null, ?callable $onRejected = null): self |
| 32 |
{ |
| 33 |
return new self($this->promise->then( |
| 34 |
$this->wrapThenCallback($onFulfilled), |
| 35 |
$this->wrapThenCallback($onRejected) |
| 36 |
)); |
| 37 |
} |
| 38 |
|
| 39 |
public function cancel(): void |
| 40 |
{ |
| 41 |
$this->promise->cancel(); |
| 42 |
} |
| 43 |
|
| 44 |
public function getState(): string |
| 45 |
{ |
| 46 |
return $this->promise->getState(); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function wait($unwrap = true): mixed |
| 53 |
{ |
| 54 |
$result = $this->promise->wait($unwrap); |
| 55 |
|
| 56 |
while ($result instanceof HttplugPromiseInterface || $result instanceof GuzzlePromiseInterface) { |
| 57 |
$result = $result->wait($unwrap); |
| 58 |
} |
| 59 |
|
| 60 |
return $result; |
| 61 |
} |
| 62 |
|
| 63 |
private function wrapThenCallback(?callable $callback): ?callable |
| 64 |
{ |
| 65 |
if (null === $callback) { |
| 66 |
return null; |
| 67 |
} |
| 68 |
|
| 69 |
return static fn ($value) => Create::promiseFor($callback($value)); |
| 70 |
} |
| 71 |
} |