Open Journal Systems  3.3.0
php-http/guzzle6-adapter/src/Promise.php
1 <?php
2 
3 namespace Http\Adapter\Guzzle6;
4 
9 use Http\Promise\Promise as HttpPromise;
12 
18 final class Promise implements HttpPromise
19 {
23  private $promise;
24 
28  private $state;
29 
33  private $response;
34 
38  private $exception;
39 
43  private $request;
44 
49  public function __construct(PromiseInterface $promise, RequestInterface $request)
50  {
51  $this->request = $request;
52  $this->state = self::PENDING;
53  $this->promise = $promise->then(function ($response) {
54  $this->response = $response;
55  $this->state = self::FULFILLED;
56 
57  return $response;
58  }, function ($reason) use ($request) {
59  $this->state = self::REJECTED;
60 
61  if ($reason instanceof HttplugException) {
62  $this->exception = $reason;
63  } elseif ($reason instanceof GuzzleExceptions\GuzzleException) {
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);
67  } else {
68  $this->exception = new UnexpectedValueException('Reason returned from Guzzle6 must be an Exception');
69  }
70 
71  throw $this->exception;
72  });
73  }
74 
78  public function then(callable $onFulfilled = null, callable $onRejected = null)
79  {
80  return new static($this->promise->then($onFulfilled, $onRejected), $this->request);
81  }
82 
86  public function getState()
87  {
88  return $this->state;
89  }
90 
94  public function wait($unwrap = true)
95  {
96  $this->promise->wait(false);
97 
98  if ($unwrap) {
99  if (self::REJECTED == $this->getState()) {
100  throw $this->exception;
101  }
102 
103  return $this->response;
104  }
105  }
106 
115  private function handleException(GuzzleExceptions\GuzzleException $exception, RequestInterface $request)
116  {
117  if ($exception instanceof GuzzleExceptions\SeekException) {
118  return new HttplugException\RequestException($exception->getMessage(), $request, $exception);
119  }
120 
121  if ($exception instanceof GuzzleExceptions\ConnectException) {
122  return new HttplugException\NetworkException($exception->getMessage(), $exception->getRequest(), $exception);
123  }
124 
125  if ($exception instanceof GuzzleExceptions\RequestException) {
126  // Make sure we have a response for the HttpException
127  if ($exception->hasResponse()) {
128  return new HttplugException\HttpException(
129  $exception->getMessage(),
130  $exception->getRequest(),
131  $exception->getResponse(),
132  $exception
133  );
134  }
135 
136  return new HttplugException\RequestException($exception->getMessage(), $exception->getRequest(), $exception);
137  }
138 
139  return new HttplugException\TransferException($exception->getMessage(), 0, $exception);
140  }
141 }
Http\Adapter\Guzzle6\Promise\wait
wait($unwrap=true)
Definition: php-http/guzzle6-adapter/src/Promise.php:109
GuzzleHttp\Promise\PromiseInterface\then
then(callable $onFulfilled=null, callable $onRejected=null)
GuzzleHttp\Promise\PromiseInterface
Definition: PromiseInterface.php:13
Psr\Http\Message\RequestInterface
Definition: vendor/psr/http-message/src/RequestInterface.php:24
Http\Promise\Promise\PENDING
const PENDING
Definition: php-http/promise/src/Promise.php:21
Http\Promise\Promise
Definition: php-http/promise/src/Promise.php:16
Http\Promise\Promise\FULFILLED
const FULFILLED
Definition: php-http/promise/src/Promise.php:26
Http\Adapter\Guzzle6\Exception\UnexpectedValueException
Definition: vendor/php-http/guzzle6-adapter/src/Exception/UnexpectedValueException.php:7
GuzzleHttp\Exception
Definition: vendor/guzzlehttp/guzzle/src/Exception/BadResponseException.php:2
Http\Promise\Promise\REJECTED
const REJECTED
Definition: php-http/promise/src/Promise.php:31
Http\Adapter\Guzzle6
Definition: vendor/php-http/guzzle6-adapter/src/Client.php:5
Psr\Http\Message\ResponseInterface
Definition: vendor/psr/http-message/src/ResponseInterface.php:20
Http\Client\Exception
Definition: vendor/php-http/httplug/src/Exception/HttpException.php:3
Http\Adapter\Guzzle6\Promise\__construct
__construct(PromiseInterface $promise, RequestInterface $request)
Definition: php-http/guzzle6-adapter/src/Promise.php:64
Http\Adapter\Guzzle6\Promise\then
then(callable $onFulfilled=null, callable $onRejected=null)
Definition: php-http/guzzle6-adapter/src/Promise.php:93
Http\Adapter\Guzzle6\Promise
Definition: php-http/guzzle6-adapter/src/Promise.php:18
Http\Adapter\Guzzle6\Promise\getState
getState()
Definition: php-http/guzzle6-adapter/src/Promise.php:101