Skip to content

Commit

Permalink
Merge pull request #7 from RicLeP/master
Browse files Browse the repository at this point in the history
Added new method getStoryByUuid
  • Loading branch information
onefriendaday authored Mar 28, 2019
2 parents 1d032d7 + ddac1e2 commit 349a460
Showing 1 changed file with 73 additions and 42 deletions.
115 changes: 73 additions & 42 deletions src/Storyblok/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public function setCacheVersion()

/**
* Gets cache version from cache or as timestamp
*
*
* @return Integer
*/
function getCacheVersion()
Expand All @@ -348,50 +348,81 @@ function getCacheVersion()
*/
public function getStoryBySlug($slug)
{
$version = 'published';

if ($this->editModeEnabled) {
$version = 'draft';
}

$key = 'stories/' . $slug;
$cachekey = $this->_getCacheKey($key);

$this->reCacheOnPublish($key);

if ($version == 'published' && $this->cache && $cachedItem = $this->cache->load($cachekey)) {
if ($this->cacheNotFound && $cachedItem->httpResponseCode == 404) {
throw new \Exception(self::EXCEPTION_GENERIC_HTTP_ERROR, 404);
}

$this->_assignState($cachedItem);
} else {
$options = array(
'token' => $this->apiKey,
'version' => $version,
'cache_version' => $this->getCacheVersion()
);

try {
$response = $this->get($key, $options);
$this->_save($response, $cachekey, $version);
} catch (\Exception $e) {
if ($this->cacheNotFound && $e->getCode() === 404) {
$result = new \stdClass();
$result->httpResponseBody = [];
$result->httpResponseCode = 404;
$result->httpResponseHeaders = [];

$this->cache->save($result, $cachekey);
}

throw new \Exception(self::EXCEPTION_GENERIC_HTTP_ERROR . ' - ' . $e->getMessage(), $e->getCode());
}
}
return $this->getStory($slug);
}

return $this;
/**
* Gets a story by it’s UUID
*
* @param string $uuid UUID
*
* @return Client
* @throws \Exception
*/
public function getStoryByUuid($uuid)
{
return $this->getStory($uuid, true);
}

/**
* Gets a story
*
* @param string $slug Slug
* @param bool $byUuid
*
* @return Client
* @throws \Exception
*/
private function getStory($slug, $byUuid = false)
{
$version = 'published';

if ($this->editModeEnabled) {
$version = 'draft';
}

$key = 'stories/' . $slug;
$cachekey = $this->_getCacheKey($key);

$this->reCacheOnPublish($key);

if ($version == 'published' && $this->cache && $cachedItem = $this->cache->load($cachekey)) {
if ($this->cacheNotFound && $cachedItem->httpResponseCode == 404) {
throw new \Exception(self::EXCEPTION_GENERIC_HTTP_ERROR, 404);
}

$this->_assignState($cachedItem);
} else {
$options = array(
'token' => $this->apiKey,
'version' => $version,
'cache_version' => $this->getCacheVersion()
);

if ($byUuid) {
$options['find_by'] = 'uuid';
}

try {
$response = $this->get($key, $options);
$this->_save($response, $cachekey, $version);
} catch (\Exception $e) {
if ($this->cacheNotFound && $e->getCode() === 404) {
$result = new \stdClass();
$result->httpResponseBody = [];
$result->httpResponseCode = 404;
$result->httpResponseHeaders = [];

$this->cache->save($result, $cachekey);
}

throw new \Exception(self::EXCEPTION_GENERIC_HTTP_ERROR . ' - ' . $e->getMessage(), $e->getCode());
}
}

return $this;
}

/**
* Gets a list of stories
*
Expand Down

0 comments on commit 349a460

Please sign in to comment.