Skip to content
Merged
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
40 changes: 18 additions & 22 deletions src/Contentful.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GuzzleHttp\Exception\RequestException;
use function GuzzleHttp\Promise\coroutine;
use GuzzleHttp\Promise\FulfilledPromise;
use function GuzzleHttp\Promise\promise_for;
use GuzzleHttp\Promise\PromiseInterface;
use Markup\Contentful\Cache\NullCacheItemPool;
use Markup\Contentful\Decorator\AssetDecoratorInterface;
Expand Down Expand Up @@ -385,7 +386,6 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
return $pool->getItem($cacheKey);
};
$assetDecorator = $this->ensureAssetDecorator($spaceData['asset_decorator']);
$finalPromise = null;
/**
* Returns a built response if it passes test, or null if it doesn't.
*
Expand Down Expand Up @@ -430,8 +430,8 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,

$builtResponse = $buildResponseFromJson($cacheItemJson);
if ($builtResponse) {
$finalPromise = new FulfilledPromise($builtResponse);
goto finalYield;
yield promise_for($builtResponse);
return;
}
}
if ($this->cacheFailResponses) {
Expand All @@ -449,8 +449,8 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
);
$builtResponse = $buildResponseFromJson($fallbackJson);
if ($builtResponse) {
$finalPromise = new FulfilledPromise($builtResponse);
goto finalYield;
yield promise_for($builtResponse);
return;
}
}
}
Expand Down Expand Up @@ -499,22 +499,20 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
$writeCacheItem->set($fallbackJson);
$writeCache->save($writeCacheItem);

$finalPromise = new FulfilledPromise(
$this->buildResponseFromRaw(
json_decode($fallbackJson, true),
$spaceData['name'],
$assetDecorator,
$shouldBuildTypedResources
)
);
goto finalYield;
yield promise_for($this->buildResponseFromRaw(
json_decode($fallbackJson, true),
$spaceData['name'],
$assetDecorator,
$shouldBuildTypedResources
));
return;
}
}
//if there is a rate limit error, wait (if applicable)
if ($e->hasResponse() && $e->getResponse()->getStatusCode() === 429 && $spaceData['retry_time_after_rate_limit_in_ms']) {
usleep(intval($spaceData['retry_time_after_rate_limit_in_ms']));

$finalPromise = (yield $this->doRequest(
yield $this->doRequest(
$spaceData,
$spaceName,
$endpointUrl,
Expand All @@ -524,8 +522,8 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
$cacheDisambiguator,
$parameters,
array_merge($options, ['async' => true])
));
goto finalYield;
);
return;
}
$unavailableException = new ResourceUnavailableException($e->getResponse(), $exceptionMessage, 0, $e);
}
Expand Down Expand Up @@ -594,8 +592,8 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
);
$builtResponse = $buildResponseFromJson($fallbackJson);
if ($builtResponse) {
$finalPromise = new FulfilledPromise($builtResponse);
goto finalYield;
yield promise_for($builtResponse);
return;
}
}
}
Expand All @@ -613,9 +611,7 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
LogInterface::TYPE_RESPONSE
);

finalYield:

yield $finalPromise ?: new FulfilledPromise($builtResponse);
yield promise_for($builtResponse);
}
);

Expand Down