Skip to content

Commit 0e94e32

Browse files
committed
Fix CachedResponseAdapter
1 parent 62b0450 commit 0e94e32

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

tests/Geocoder/Tests/CachedResponseAdapter.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ class CachedResponseAdapter implements HttpAdapterInterface
88
{
99
private $adapter;
1010

11+
private $useCache;
12+
1113
private $cacheDir;
1214

13-
public function __construct(HttpAdapterInterface $adapter, $cacheDir = '.cached_responses')
15+
public function __construct(HttpAdapterInterface $adapter, $useCache = false, $cacheDir = '.cached_responses')
1416
{
1517
$this->adapter = $adapter;
18+
$this->useCache = $useCache;
1619
$this->cacheDir = $cacheDir;
1720
}
1821

@@ -21,10 +24,9 @@ public function __construct(HttpAdapterInterface $adapter, $cacheDir = '.cached_
2124
*/
2225
public function getContent($url)
2326
{
24-
$useCache = isset($_SERVER['USE_CACHED_RESPONSES']) && true === $_SERVER['USE_CACHED_RESPONSES'];
25-
$file = sprintf('%s/%s/%s', realpath(__DIR__ . '/../../'), $this->cacheDir, sha1($url));
27+
$file = sprintf('%s/%s/%s', realpath(__DIR__ . '/../../'), $this->cacheDir, sha1($url));
2628

27-
if ($useCache && is_file($file) && is_readable($file)) {
29+
if ($this->useCache && is_file($file) && is_readable($file)) {
2830
$response = unserialize(file_get_contents($file));
2931

3032
if (!empty($response)) {
@@ -34,7 +36,7 @@ public function getContent($url)
3436

3537
$response = $this->adapter->getContent($url);
3638

37-
if ($useCache) {
39+
if ($this->useCache) {
3840
file_put_contents($file, serialize($response));
3941
}
4042

tests/Geocoder/Tests/TestCase.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ protected function getMockAdapterReturns($returnValue)
4949
*/
5050
protected function getAdapter()
5151
{
52-
return new CachedResponseAdapter(
53-
new CurlHttpAdapter()
54-
);
52+
return new CachedResponseAdapter(new CurlHttpAdapter(), $this->useCache());
53+
}
54+
55+
/**
56+
* @return boolean
57+
*/
58+
protected function useCache()
59+
{
60+
return isset($_SERVER['USE_CACHED_RESPONSES']) && true === $_SERVER['USE_CACHED_RESPONSES'];
5561
}
5662
}

0 commit comments

Comments
 (0)