Skip to content

Commit ec371af

Browse files
committed
Update CachedHttpClient.php
Some cleaning according the chat with @stof + format the identifier (`$id`) with encoded query parameters.
1 parent 2bcaeb9 commit ec371af

File tree

1 file changed

+13
-35
lines changed

1 file changed

+13
-35
lines changed

lib/Github/HttpClient/CachedHttpClient.php

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ class CachedHttpClient extends HttpClient
2727
private $lastCachedResponse;
2828

2929
/**
30-
* $path + query parameter(s) if they exist.
30+
* Identifier used for the cache file(s).
31+
* $path + encoded query parameter(s) if they exist.
3132
*
3233
* @var string
3334
*/
34-
private $path;
35+
private $id;
3536

3637
/**
3738
* @return CacheInterface
@@ -58,18 +59,16 @@ public function setCache(CacheInterface $cache)
5859
*/
5960
public function request($path, $body = null, $httpMethod = 'GET', array $headers = array(), array $options = array())
6061
{
61-
$this->formatPath($path, $options);
62-
6362
$response = parent::request($path, $body, $httpMethod, $headers, $options);
6463

6564
if (304 == $response->getStatusCode()) {
66-
$cacheResponse = $this->getCache()->get($this->path);
65+
$cacheResponse = $this->getCache()->get($this->id);
6766
$this->lastCachedResponse = $cacheResponse;
6867

6968
return $cacheResponse;
7069
}
7170

72-
$this->getCache()->set($this->path, $response);
71+
$this->getCache()->set($this->id, $response);
7372

7473
return $response;
7574
}
@@ -82,8 +81,14 @@ public function request($path, $body = null, $httpMethod = 'GET', array $headers
8281
protected function createRequest($httpMethod, $path, $body = null, array $headers = array(), array $options = array())
8382
{
8483
$request = parent::createRequest($httpMethod, $path, $body, $headers, $options);
84+
85+
$this->id = $path;
86+
87+
if (array_key_exists('query', $options) && !empty($options['query'])) {
88+
$this->id .= '?' . $request->getQuery();
89+
}
8590

86-
if ($modifiedAt = $this->getCache()->getModifiedSince($this->path)) {
91+
if ($modifiedAt = $this->getCache()->getModifiedSince($this->id)) {
8792
$modifiedAt = new \DateTime('@'.$modifiedAt);
8893
$modifiedAt->setTimezone(new \DateTimeZone('GMT'));
8994

@@ -92,7 +97,7 @@ protected function createRequest($httpMethod, $path, $body = null, array $header
9297
sprintf('%s GMT', $modifiedAt->format('l, d-M-y H:i:s'))
9398
);
9499
}
95-
if ($etag = $this->getCache()->getETag($this->path)) {
100+
if ($etag = $this->getCache()->getETag($this->id)) {
96101
$request->addHeader(
97102
'If-None-Match',
98103
$etag
@@ -114,31 +119,4 @@ public function getLastResponse($force = false)
114119

115120
return ($force) ? $lastResponse : $this->lastCachedResponse;
116121
}
117-
118-
/**
119-
* Format the path and add query parameters if they exist.
120-
*
121-
* @param string $path
122-
* @param array $options
123-
* @return void
124-
*/
125-
private function formatPath($path, array $options)
126-
{
127-
$this->path = $path;
128-
129-
if (array_key_exists('query', $options) && !empty($options['query'])) {
130-
$this->path .= '?';
131-
132-
$i = 0;
133-
foreach ($options['query'] as $key => $value) {
134-
if ($i > 0) {
135-
$this->path .= '&';
136-
}
137-
138-
$this->path .= $key . '=' . $value;
139-
140-
$i++;
141-
}
142-
}
143-
}
144122
}

0 commit comments

Comments
 (0)