From ddac1e29e585ca91933d8d35ad2ee25ec15b7e56 Mon Sep 17 00:00:00 2001 From: Richard Le Poidevin Date: Tue, 26 Mar 2019 10:08:20 +0000 Subject: [PATCH] Added new method getStoryByUuid Refactored getStoryBySlug using new getStory which contains common functionality for both calls --- src/Storyblok/Client.php | 115 +++++++++++++++++++++++++-------------- 1 file changed, 73 insertions(+), 42 deletions(-) diff --git a/src/Storyblok/Client.php b/src/Storyblok/Client.php index da56692..919873b 100644 --- a/src/Storyblok/Client.php +++ b/src/Storyblok/Client.php @@ -326,7 +326,7 @@ public function setCacheVersion() /** * Gets cache version from cache or as timestamp - * + * * @return Integer */ function getCacheVersion() @@ -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 *