Skip to content

Commit abaedea

Browse files
committed
add a getLastResponse method to CachedHttpClient that return the cached version of the response by default
1 parent 9b57f84 commit abaedea

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

lib/Github/HttpClient/CachedHttpClient.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ class CachedHttpClient extends HttpClient
1818
* @var CacheInterface
1919
*/
2020
protected $cache;
21+
22+
/**
23+
* contains the lastResponse fetched from cache
24+
*
25+
* @var Guzzle\Http\Message\Response
26+
*/
27+
private $lastCachedResponse;
2128

2229
/**
2330
* @return CacheInterface
@@ -45,9 +52,12 @@ public function setCache(CacheInterface $cache)
4552
public function request($path, $body = null, $httpMethod = 'GET', array $headers = array(), array $options = array())
4653
{
4754
$response = parent::request($path, $body, $httpMethod, $headers, $options);
48-
55+
4956
if (304 == $response->getStatusCode()) {
50-
return $this->getCache()->get($path);
57+
$cacheResponse = $this->getCache()->get($path);
58+
$this->lastCachedResponse = $cacheResponse;
59+
60+
return $cacheResponse;
5161
}
5262

5363
$this->getCache()->set($path, $response);
@@ -82,4 +92,18 @@ protected function createRequest($httpMethod, $path, $body = null, array $header
8292

8393
return $request;
8494
}
95+
96+
/**
97+
* @return Guzzle\Http\Message\Response
98+
*/
99+
public function getLastResponse($force = false)
100+
{
101+
102+
$lastResponse = parent::getLastResponse();
103+
if (304 != $lastResponse->getStatusCode()) {
104+
$force = true;
105+
}
106+
107+
return ($force) ? $lastResponse : $this->lastCachedResponse;
108+
}
85109
}

0 commit comments

Comments
 (0)