| 1 |
<?php |
| 2 |
|
| 3 |
namespace Http\Client\Promise; |
| 4 |
|
| 5 |
use Http\Client\Exception; |
| 6 |
use Http\Promise\Promise; |
| 7 |
use Psr\Http\Message\ResponseInterface; |
| 8 |
|
| 9 |
final class HttpFulfilledPromise implements Promise |
| 10 |
{ |
| 11 |
/** |
| 12 |
* @var ResponseInterface |
| 13 |
*/ |
| 14 |
private $response; |
| 15 |
|
| 16 |
public function __construct(ResponseInterface $response) |
| 17 |
{ |
| 18 |
$this->response = $response; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* {@inheritdoc} |
| 23 |
*/ |
| 24 |
public function then(callable $onFulfilled = null, callable $onRejected = null) |
| 25 |
{ |
| 26 |
if (null === $onFulfilled) { |
| 27 |
return $this; |
| 28 |
} |
| 29 |
|
| 30 |
try { |
| 31 |
return new self($onFulfilled($this->response)); |
| 32 |
} catch (Exception $e) { |
| 33 |
return new HttpRejectedPromise($e); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* {@inheritdoc} |
| 39 |
*/ |
| 40 |
public function getState() |
| 41 |
{ |
| 42 |
return Promise::FULFILLED; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* {@inheritdoc} |
| 47 |
*/ |
| 48 |
public function wait($unwrap = true) |
| 49 |
{ |
| 50 |
if ($unwrap) { |
| 51 |
return $this->response; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|