|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Http\Client\Common; |
| 4 | + |
| 5 | +use Http\Client\Exception; |
| 6 | +use Http\Promise\Promise; |
| 7 | +use Psr\Http\Message\ResponseInterface; |
| 8 | + |
| 9 | +/** |
| 10 | + * A deferred allow to return a promise which has not been resolved yet. |
| 11 | + */ |
| 12 | +class Deferred implements Promise |
| 13 | +{ |
| 14 | + private $value; |
| 15 | + |
| 16 | + private $failure; |
| 17 | + |
| 18 | + private $state; |
| 19 | + |
| 20 | + private $waitCallback; |
| 21 | + |
| 22 | + private $onFulfilledCallbacks; |
| 23 | + |
| 24 | + private $onRejectedCallbacks; |
| 25 | + |
| 26 | + public function __construct(callable $waitCallback) |
| 27 | + { |
| 28 | + $this->waitCallback = $waitCallback; |
| 29 | + $this->state = Promise::PENDING; |
| 30 | + $this->onFulfilledCallbacks = []; |
| 31 | + $this->onRejectedCallbacks = []; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * {@inheritdoc} |
| 36 | + */ |
| 37 | + public function then(callable $onFulfilled = null, callable $onRejected = null) |
| 38 | + { |
| 39 | + $deferred = new self($this->waitCallback); |
| 40 | + |
| 41 | + $this->onFulfilledCallbacks[] = function (ResponseInterface $response) use ($onFulfilled, $deferred) { |
| 42 | + try { |
| 43 | + if (null !== $onFulfilled) { |
| 44 | + $response = $onFulfilled($response); |
| 45 | + } |
| 46 | + $deferred->resolve($response); |
| 47 | + } catch (Exception $exception) { |
| 48 | + $deferred->reject($exception); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + $this->onRejectedCallbacks[] = function (Exception $exception) use ($onRejected, $deferred) { |
| 53 | + try { |
| 54 | + if (null !== $onRejected) { |
| 55 | + $response = $onRejected($exception); |
| 56 | + $deferred->resolve($response); |
| 57 | + |
| 58 | + return; |
| 59 | + } |
| 60 | + $deferred->reject($exception); |
| 61 | + } catch (Exception $newException) { |
| 62 | + $deferred->reject($newException); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + return $deferred; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * {@inheritdoc} |
| 71 | + */ |
| 72 | + public function getState() |
| 73 | + { |
| 74 | + return $this->state; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Resolve this deferred with a Response. |
| 79 | + */ |
| 80 | + public function resolve(ResponseInterface $response) |
| 81 | + { |
| 82 | + if (self::PENDING !== $this->state) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + $this->value = $response; |
| 87 | + $this->state = self::FULFILLED; |
| 88 | + |
| 89 | + foreach ($this->onFulfilledCallbacks as $onFulfilledCallback) { |
| 90 | + $onFulfilledCallback($response); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Reject this deferred with an Exception. |
| 96 | + */ |
| 97 | + public function reject(Exception $exception) |
| 98 | + { |
| 99 | + if (self::PENDING !== $this->state) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + $this->failure = $exception; |
| 104 | + $this->state = self::REJECTED; |
| 105 | + |
| 106 | + foreach ($this->onRejectedCallbacks as $onRejectedCallback) { |
| 107 | + $onRejectedCallback($exception); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * {@inheritdoc} |
| 113 | + */ |
| 114 | + public function wait($unwrap = true) |
| 115 | + { |
| 116 | + if (self::PENDING === $this->state) { |
| 117 | + $callback = $this->waitCallback; |
| 118 | + $callback(); |
| 119 | + } |
| 120 | + |
| 121 | + if (!$unwrap) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + if (self::FULFILLED === $this->state) { |
| 126 | + return $this->value; |
| 127 | + } |
| 128 | + |
| 129 | + throw $this->failure; |
| 130 | + } |
| 131 | +} |
0 commit comments