From abaedeaef7a5fd4d9fbd264c9ff13bb91da8c785 Mon Sep 17 00:00:00 2001 From: Beleneglorion Date: Sun, 27 Jul 2014 14:40:34 +0200 Subject: [PATCH] add a getLastResponse method to CachedHttpClient that return the cached version of the response by default --- lib/Github/HttpClient/CachedHttpClient.php | 28 ++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/Github/HttpClient/CachedHttpClient.php b/lib/Github/HttpClient/CachedHttpClient.php index dfbe6630f31..95e6913b373 100644 --- a/lib/Github/HttpClient/CachedHttpClient.php +++ b/lib/Github/HttpClient/CachedHttpClient.php @@ -18,6 +18,13 @@ class CachedHttpClient extends HttpClient * @var CacheInterface */ protected $cache; + + /** + * contains the lastResponse fetched from cache + * + * @var Guzzle\Http\Message\Response + */ + private $lastCachedResponse; /** * @return CacheInterface @@ -45,9 +52,12 @@ public function setCache(CacheInterface $cache) public function request($path, $body = null, $httpMethod = 'GET', array $headers = array(), array $options = array()) { $response = parent::request($path, $body, $httpMethod, $headers, $options); - + if (304 == $response->getStatusCode()) { - return $this->getCache()->get($path); + $cacheResponse = $this->getCache()->get($path); + $this->lastCachedResponse = $cacheResponse; + + return $cacheResponse; } $this->getCache()->set($path, $response); @@ -82,4 +92,18 @@ protected function createRequest($httpMethod, $path, $body = null, array $header return $request; } + + /** + * @return Guzzle\Http\Message\Response + */ + public function getLastResponse($force = false) + { + + $lastResponse = parent::getLastResponse(); + if (304 != $lastResponse->getStatusCode()) { + $force = true; + } + + return ($force) ? $lastResponse : $this->lastCachedResponse; + } }