22
33namespace Github \HttpClient ;
44
5- use Buzz \Client \ClientInterface ;
6-
75use Github \HttpClient \Cache \CacheInterface ;
86use Github \HttpClient \Cache \FilesystemCache ;
97
@@ -22,15 +20,23 @@ class CachedHttpClient extends HttpClient
2220 protected $ cache ;
2321
2422 /**
25- * @param array $options
26- * @param null|ClientInterface $client
27- * @param null|CacheInterface $cache
23+ * @return CacheInterface
2824 */
29- public function __construct ( array $ options = array (), ClientInterface $ client = null , CacheInterface $ cache = null )
25+ public function getCache ( )
3026 {
31- parent ::__construct ($ options , $ client );
27+ if (null === $ this ->cache ) {
28+ $ this ->cache = new FilesystemCache ($ this ->options ['cache_dir ' ] ?: sys_get_temp_dir ().DIRECTORY_SEPARATOR .'php-github-api-cache ' );
29+ }
30+
31+ return $ this ->cache ;
32+ }
3233
33- $ this ->cache = $ cache ?: new FilesystemCache ($ this ->options ['cache_dir ' ] ?: sys_get_temp_dir ().DIRECTORY_SEPARATOR .'php-github-api-cache ' );
34+ /**
35+ * @param $cache CacheInterface
36+ */
37+ public function setCache (CacheInterface $ cache )
38+ {
39+ $ this ->cache = $ cache ;
3440 }
3541
3642 /**
@@ -42,10 +48,10 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
4248
4349 $ key = trim ($ this ->options ['base_url ' ].$ path , '/ ' );
4450 if ($ response ->isNotModified ()) {
45- return $ this ->cache ->get ($ key );
51+ return $ this ->getCache () ->get ($ key );
4652 }
4753
48- $ this ->cache ->set ($ key , $ response );
54+ $ this ->getCache () ->set ($ key , $ response );
4955
5056 return $ response ;
5157 }
@@ -58,7 +64,7 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
5864 protected function createRequest ($ httpMethod , $ url )
5965 {
6066 $ request = parent ::createRequest ($ httpMethod , $ url );
61- $ request ->addHeader (sprintf ('If-Modified-Since: %s ' , date ('r ' , $ this ->cache ->getModifiedSince ($ url ))));
67+ $ request ->addHeader (sprintf ('If-Modified-Since: %s ' , date ('r ' , $ this ->getCache () ->getModifiedSince ($ url ))));
6268
6369 return $ request ;
6470 }
0 commit comments