diff --git a/src/Deferred.php b/src/Deferred.php index 4c50721..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; /** @@ -13,16 +13,34 @@ */ final class Deferred implements Promise { + /** + * @var ResponseInterface|null + */ private $value; + /** + * @var ClientExceptionInterface|null + */ private $failure; + /** + * @var string + */ private $state; + /** + * @var callable + */ private $waitCallback; + /** + * @var callable[] + */ private $onFulfilledCallbacks; + /** + * @var callable[] + */ private $onRejectedCallbacks; public function __construct(callable $waitCallback) @@ -46,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); @@ -60,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); } }; @@ -81,12 +99,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); @@ -96,14 +114,14 @@ 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 (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 +133,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;