| @@ 12-60 (lines=49) @@ | ||
| 9 | * |
|
| 10 | * @author Joel Wurtz <[email protected]> |
|
| 11 | */ |
|
| 12 | final class FulfilledPromise implements Promise |
|
| 13 | { |
|
| 14 | /** |
|
| 15 | * @var ResponseInterface |
|
| 16 | */ |
|
| 17 | private $response; |
|
| 18 | ||
| 19 | /** |
|
| 20 | * @param ResponseInterface $response |
|
| 21 | */ |
|
| 22 | public function __construct(ResponseInterface $response) |
|
| 23 | { |
|
| 24 | $this->response = $response; |
|
| 25 | } |
|
| 26 | ||
| 27 | /** |
|
| 28 | * {@inheritdoc} |
|
| 29 | */ |
|
| 30 | public function then(callable $onFulfilled = null, callable $onRejected = null) |
|
| 31 | { |
|
| 32 | if (null === $onFulfilled) { |
|
| 33 | return $this; |
|
| 34 | } |
|
| 35 | ||
| 36 | try { |
|
| 37 | return new self($onFulfilled($this->response)); |
|
| 38 | } catch (\Exception $e) { |
|
| 39 | return new RejectedPromise($e); |
|
| 40 | } |
|
| 41 | } |
|
| 42 | ||
| 43 | /** |
|
| 44 | * {@inheritdoc} |
|
| 45 | */ |
|
| 46 | public function getState() |
|
| 47 | { |
|
| 48 | return Promise::FULFILLED; |
|
| 49 | } |
|
| 50 | ||
| 51 | /** |
|
| 52 | * {@inheritdoc} |
|
| 53 | */ |
|
| 54 | public function wait($unwrap = true) |
|
| 55 | { |
|
| 56 | if ($unwrap) { |
|
| 57 | return $this->response; |
|
| 58 | } |
|
| 59 | } |
|
| 60 | } |
|
| 61 | ||
| @@ 10-58 (lines=49) @@ | ||
| 7 | * |
|
| 8 | * @author Joel Wurtz <[email protected]> |
|
| 9 | */ |
|
| 10 | final class RejectedPromise implements Promise |
|
| 11 | { |
|
| 12 | /** |
|
| 13 | * @var \Exception |
|
| 14 | */ |
|
| 15 | private $exception; |
|
| 16 | ||
| 17 | /** |
|
| 18 | * @param \Exception $exception |
|
| 19 | */ |
|
| 20 | public function __construct(\Exception $exception) |
|
| 21 | { |
|
| 22 | $this->exception = $exception; |
|
| 23 | } |
|
| 24 | ||
| 25 | /** |
|
| 26 | * {@inheritdoc} |
|
| 27 | */ |
|
| 28 | public function then(callable $onFulfilled = null, callable $onRejected = null) |
|
| 29 | { |
|
| 30 | if (null === $onRejected) { |
|
| 31 | return $this; |
|
| 32 | } |
|
| 33 | ||
| 34 | try { |
|
| 35 | return new FulfilledPromise($onRejected($this->exception)); |
|
| 36 | } catch (\Exception $e) { |
|
| 37 | return new self($e); |
|
| 38 | } |
|
| 39 | } |
|
| 40 | ||
| 41 | /** |
|
| 42 | * {@inheritdoc} |
|
| 43 | */ |
|
| 44 | public function getState() |
|
| 45 | { |
|
| 46 | return Promise::REJECTED; |
|
| 47 | } |
|
| 48 | ||
| 49 | /** |
|
| 50 | * {@inheritdoc} |
|
| 51 | */ |
|
| 52 | public function wait($unwrap = true) |
|
| 53 | { |
|
| 54 | if ($unwrap) { |
|
| 55 | throw $this->exception; |
|
| 56 | } |
|
| 57 | } |
|
| 58 | } |
|
| 59 | ||