Skip to content
This repository was archived by the owner on Jan 6, 2024. It is now read-only.

Commit 02c5f75

Browse files
committed
Use chained then, force our implementation so it's called even if then not called
1 parent 3249580 commit 02c5f75

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

src/Guzzle6Promise.php

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,39 @@ class Guzzle6Promise implements Promise
2828
/**
2929
* @var Exception
3030
*/
31-
private $error;
31+
private $exception;
3232

3333
public function __construct(PromiseInterface $promise)
3434
{
35-
$this->promise = $promise;
36-
$this->state = self::PENDING;
37-
}
38-
39-
/**
40-
* {@inheritdoc}
41-
*/
42-
public function then(callable $onFulfilled = null, callable $onRejected = null)
43-
{
44-
$onFulfilledInternal = function ($response) use($onFulfilled) {
35+
$this->promise = $promise->then(function ($response) {
4536
$this->response = $response;
4637
$this->state = self::FULFILLED;
4738

48-
return $onFulfilled($this->response);
49-
};
50-
51-
$onRejectedInternal = function ($reason) use($onRejected) {
39+
return $response;
40+
}, function ($reason) {
5241
if (!($reason instanceof RequestException)) {
53-
throw new \RuntimeException("Invalid reason");
42+
throw new \RuntimeException("Invalid reason");
5443
}
5544

56-
$this->state = self::REJECTED;
57-
$this->error = new Exception\NetworkException($reason->getMessage(), $reason->getRequest(), $reason);
45+
$this->state = self::REJECTED;
46+
$this->exception = new Exception\NetworkException($reason->getMessage(), $reason->getRequest(), $reason);
5847

5948
if ($reason->hasResponse()) {
60-
$this->error = new Exception\HttpException($reason->getMessage(), $reason->getRequest(), $reason->getResponse(), $reason);
49+
$this->exception = new Exception\HttpException($reason->getMessage(), $reason->getRequest(), $reason->getResponse(), $reason);
6150
}
6251

63-
return $onRejected($this->error);
64-
};
52+
throw $this->exception;
53+
});
6554

66-
$this->promise = $this->promise->then($onFulfilledInternal, $onRejectedInternal);
55+
$this->state = self::PENDING;
56+
}
6757

68-
return new static($this->promise);
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function then(callable $onFulfilled = null, callable $onRejected = null)
62+
{
63+
return new static($this->promise->then($onFulfilled, $onRejected));
6964
}
7065

7166
/**
@@ -92,13 +87,13 @@ public function getResponse()
9287
/**
9388
* {@inheritdoc}
9489
*/
95-
public function getError()
90+
public function getException()
9691
{
9792
if (self::REJECTED !== $this->state) {
9893
throw new \LogicException("Error not available for the current state");
9994
}
10095

101-
return $this->error;
96+
return $this->exception;
10297
}
10398

10499
/**

0 commit comments

Comments
 (0)