Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 25 additions & 7 deletions src/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,34 @@
*/
final class Deferred implements Promise
{
/**
* @var ResponseInterface|null
*/
private $value;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imho it would make sense to have phpdoc for the types of all fields here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

/**
* @var Exception|null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be Psr\Http\Client\ClientExceptionInterface ? (i see that for now the type declaration in the method is the httplug exception interface, but psr exception would be future proof...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. Thanks

*/
private $failure;

/**
* @var string
*/
private $state;

/**
* @var callable
*/
private $waitCallback;

/**
* @var callable[]
*/
private $onFulfilledCallbacks;

/**
* @var callable[]
*/
private $onRejectedCallbacks;

public function __construct(callable $waitCallback)
Expand Down Expand Up @@ -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 @@ -98,12 +116,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);
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