From 9de3ec8a3d743ce37f2d27e26516df198025daef Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 20 Jan 2019 08:40:10 +0100 Subject: [PATCH 1/3] Minor cleanup --- src/Deferred.php | 19 +++++++------------ src/HttpClientPool/HttpClientPoolItem.php | 2 +- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/Deferred.php b/src/Deferred.php index 4c50721..022213d 100644 --- a/src/Deferred.php +++ b/src/Deferred.php @@ -14,15 +14,10 @@ final class Deferred implements Promise { private $value; - private $failure; - private $state; - private $waitCallback; - private $onFulfilledCallbacks; - private $onRejectedCallbacks; public function __construct(callable $waitCallback) @@ -81,12 +76,12 @@ public function getState(): string */ public function resolve(ResponseInterface $response): void { - if (self::PENDING !== $this->state) { + if (Promise::PENDING !== $this->state) { return; } $this->value = $response; - $this->state = self::FULFILLED; + $this->state = Promise::FULFILLED; foreach ($this->onFulfilledCallbacks as $onFulfilledCallback) { $onFulfilledCallback($response); @@ -98,12 +93,12 @@ public function resolve(ResponseInterface $response): void */ public function reject(Exception $exception): void { - if (self::PENDING !== $this->state) { + if (Promise::PENDING !== $this->state) { return; } $this->failure = $exception; - $this->state = self::REJECTED; + $this->state = Promise::REJECTED; foreach ($this->onRejectedCallbacks as $onRejectedCallback) { $onRejectedCallback($exception); @@ -115,16 +110,16 @@ public function reject(Exception $exception): void */ public function wait($unwrap = true) { - if (self::PENDING === $this->state) { + if (Promise::PENDING === $this->state) { $callback = $this->waitCallback; $callback(); } if (!$unwrap) { - return; + return null; } - if (self::FULFILLED === $this->state) { + if (Promise::FULFILLED === $this->state) { return $this->value; } diff --git a/src/HttpClientPool/HttpClientPoolItem.php b/src/HttpClientPool/HttpClientPoolItem.php index 740262a..2c000ba 100644 --- a/src/HttpClientPool/HttpClientPoolItem.php +++ b/src/HttpClientPool/HttpClientPoolItem.php @@ -57,7 +57,7 @@ class HttpClientPoolItem implements HttpClient, HttpAsyncClient * @param ClientInterface|HttpAsyncClient $client * @param int|null $reenableAfter Number of seconds until this client is enabled again after an error */ - public function __construct($client, $reenableAfter = null) + public function __construct($client, int $reenableAfter = null) { $this->client = new FlexibleHttpClient($client); $this->reenableAfter = $reenableAfter; From f5143fe5a5273f4aca6491bcd5dffd69e36df2ce Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 20 Jan 2019 10:08:21 +0100 Subject: [PATCH 2/3] Added doc blocks --- src/Deferred.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Deferred.php b/src/Deferred.php index 022213d..716c12c 100644 --- a/src/Deferred.php +++ b/src/Deferred.php @@ -13,11 +13,34 @@ */ final class Deferred implements Promise { + /** + * @var ResponseInterface|null + */ private $value; + + /** + * @var Exception|null + */ private $failure; + + /** + * @var string + */ private $state; + + /** + * @var callable + */ private $waitCallback; + + /** + * @var callable[] + */ private $onFulfilledCallbacks; + + /** + * @var callable[] + */ private $onRejectedCallbacks; public function __construct(callable $waitCallback) From 44bf9e394da46b9ab61e5eb1ebcefc33397173d2 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 20 Jan 2019 12:59:13 +0100 Subject: [PATCH 3/3] Make sure we handle PSR-18 exceptions --- src/Deferred.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Deferred.php b/src/Deferred.php index 716c12c..02fcc29 100644 --- a/src/Deferred.php +++ b/src/Deferred.php @@ -4,8 +4,8 @@ namespace Http\Client\Common; -use Http\Client\Exception; use Http\Promise\Promise; +use Psr\Http\Client\ClientExceptionInterface; use Psr\Http\Message\ResponseInterface; /** @@ -19,7 +19,7 @@ final class Deferred implements Promise private $value; /** - * @var Exception|null + * @var ClientExceptionInterface|null */ private $failure; @@ -64,12 +64,12 @@ public function then(callable $onFulfilled = null, callable $onRejected = null): $response = $onFulfilled($response); } $deferred->resolve($response); - } catch (Exception $exception) { + } catch (ClientExceptionInterface $exception) { $deferred->reject($exception); } }; - $this->onRejectedCallbacks[] = function (Exception $exception) use ($onRejected, $deferred) { + $this->onRejectedCallbacks[] = function (ClientExceptionInterface $exception) use ($onRejected, $deferred) { try { if (null !== $onRejected) { $response = $onRejected($exception); @@ -78,7 +78,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null): return; } $deferred->reject($exception); - } catch (Exception $newException) { + } catch (ClientExceptionInterface $newException) { $deferred->reject($newException); } }; @@ -114,7 +114,7 @@ public function resolve(ResponseInterface $response): void /** * Reject this deferred with an Exception. */ - public function reject(Exception $exception): void + public function reject(ClientExceptionInterface $exception): void { if (Promise::PENDING !== $this->state) { return;