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