18 final class Promise implements HttpPromise
51 $this->request = $request;
53 $this->promise = $promise->
then(
function ($response) {
54 $this->response = $response;
58 },
function ($reason) use ($request) {
62 $this->exception = $reason;
64 $this->exception = $this->handleException($reason, $request);
65 } elseif ($reason instanceof \Throwable) {
66 $this->exception =
new HttplugException\TransferException(
'Invalid exception returned from Guzzle6', 0, $reason);
68 $this->exception =
new UnexpectedValueException(
'Reason returned from Guzzle6 must be an Exception');
71 throw $this->exception;
78 public function then(callable $onFulfilled =
null, callable $onRejected =
null)
80 return new static($this->promise->then($onFulfilled, $onRejected), $this->request);
94 public function wait($unwrap =
true)
96 $this->promise->wait(
false);
99 if (self::REJECTED == $this->
getState()) {
100 throw $this->exception;
103 return $this->response;
115 private function handleException(
GuzzleExceptions\GuzzleException $exception, RequestInterface $request)
118 return new HttplugException\RequestException($exception->getMessage(), $request, $exception);
122 return new HttplugException\NetworkException($exception->getMessage(), $exception->getRequest(), $exception);
127 if ($exception->hasResponse()) {
128 return new HttplugException\HttpException(
129 $exception->getMessage(),
130 $exception->getRequest(),
131 $exception->getResponse(),
136 return new HttplugException\RequestException($exception->getMessage(), $exception->getRequest(), $exception);
139 return new HttplugException\TransferException($exception->getMessage(), 0, $exception);