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
47 changes: 30 additions & 17 deletions src/Contentful.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,18 @@ public function getContentType($id, $space = null, array $options = [])
}

//fetch them all and pick one out, as it is likely we'll want to access others
$contentTypes = $this->getContentTypes([], $space, $options);
foreach ($contentTypes as $contentType) {
$this->envelope->insertContentType($contentType);
}
$contentTypesPromise = promise_for($this->getContentTypes([], $space, $options))
->then(
function ($contentTypes) use ($id) {
foreach ($contentTypes as $contentType) {
$this->envelope->insertContentType($contentType);
}

return promise_for($this->envelope->findContentType($id));
}
);

return $this->envelope->findContentType($id);
return (isset($options['async']) && $options['async']) ? $contentTypesPromise : $contentTypesPromise->wait();
}

/**
Expand Down Expand Up @@ -262,20 +268,27 @@ public function getContentTypes(array $parameters = [], $space = null, array $op
*/
public function getContentTypeByName($name, $space = null, array $options = [])
{
$contentTypeFromEnvelope = $this->envelope->findContentTypeByName($name);
if ($contentTypeFromEnvelope) {
return $contentTypeFromEnvelope;
}
$contentTypes = $this->getContentTypes([], $space, $options);
$foundContentType = null;
foreach ($contentTypes as $contentType) {
if ($contentType->getName() === $name) {
$foundContentType = $contentType;
$promise = coroutine(
function () use ($name, $space, $options) {
$contentTypeFromEnvelope = $this->envelope->findContentTypeByName($name);
if ($contentTypeFromEnvelope) {
yield promise_for($contentTypeFromEnvelope);
return;
}
$contentTypes = (yield $this->getContentTypes([], $space, array_merge($options, ['async' => true])));
$foundContentType = null;
foreach ($contentTypes as $contentType) {
if ($contentType->getName() === $name) {
$foundContentType = $contentType;
}
$this->envelope->insertContentType($contentType);
}

yield $foundContentType;
}
$this->envelope->insertContentType($contentType);
}
);

return $foundContentType;
return (isset($options['async']) && $options['async']) ? $promise : $promise->wait();
}

/**
Expand Down