| 1 |
<?php |
| 2 |
|
| 3 |
namespace Http\Client\Promise; |
| 4 |
|
| 5 |
use Http\Client\Exception; |
| 6 |
use Http\Promise\Promise; |
| 7 |
|
| 8 |
final class HttpRejectedPromise implements Promise |
| 9 |
{ |
| 10 |
/** |
| 11 |
* @var Exception |
| 12 |
*/ |
| 13 |
private $exception; |
| 14 |
|
| 15 |
public function __construct(Exception $exception) |
| 16 |
{ |
| 17 |
$this->exception = $exception; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* {@inheritdoc} |
| 22 |
*/ |
| 23 |
public function then(callable $onFulfilled = null, callable $onRejected = null) |
| 24 |
{ |
| 25 |
if (null === $onRejected) { |
| 26 |
return $this; |
| 27 |
} |
| 28 |
|
| 29 |
try { |
| 30 |
$result = $onRejected($this->exception); |
| 31 |
if ($result instanceof Promise) { |
| 32 |
return $result; |
| 33 |
} |
| 34 |
|
| 35 |
return new HttpFulfilledPromise($result); |
| 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 |
|