Skip to content

Minor cleanup #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions src/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,43 @@

namespace Http\Client\Common;

use Http\Client\Exception;
use Http\Promise\Promise;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\ResponseInterface;

/**
* A deferred allow to return a promise which has not been resolved yet.
*/
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)
Expand All @@ -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);
Expand All @@ -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);
}
};
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/HttpClientPool/HttpClientPoolItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down