Skip to content

Commit 1344108

Browse files
committed
Refactor executeSyncRequestWithCache method to improve caching support and error handling
1 parent d54150e commit 1344108

File tree

1 file changed

+47
-47
lines changed

1 file changed

+47
-47
lines changed

src/Fetch/Concerns/PerformsHttpRequests.php

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -210,53 +210,6 @@ public function sendRequest(
210210
}
211211
}
212212

213-
/**
214-
* Execute a synchronous request with caching support.
215-
*
216-
* @param string $method The HTTP method
217-
* @param string $uri The full URI
218-
* @param array<string, mixed> $options The Guzzle options
219-
* @param float $startTime The request start time
220-
* @param array<string, mixed>|null $cachedResult The cached result data
221-
* @param mixed $handler The cloned handler instance with request-specific state
222-
* @return ResponseInterface The response
223-
*/
224-
protected function executeSyncRequestWithCache(
225-
string $method,
226-
string $uri,
227-
array $options,
228-
float $startTime,
229-
?array $cachedResult,
230-
mixed $handler
231-
): ResponseInterface {
232-
try {
233-
$response = $handler->executeSyncRequest($method, $uri, $options, $startTime);
234-
235-
// Handle 304 Not Modified response
236-
if ($response->getStatusCode() === 304 && $cachedResult !== null && isset($cachedResult['cached']) && method_exists($handler, 'handleNotModified')) {
237-
$response = $handler->handleNotModified($cachedResult['cached'], $response);
238-
}
239-
240-
// Cache the response if caching is enabled
241-
// Use handler options which includes cache config
242-
if (method_exists($handler, 'cacheResponse') && method_exists($handler, 'isCacheEnabled') && $handler->isCacheEnabled()) {
243-
$handler->cacheResponse($method, $uri, $response, $handler->options);
244-
}
245-
246-
return $response;
247-
} catch (\Throwable $e) {
248-
// Handle stale-if-error: serve stale response on error
249-
if ($cachedResult !== null && isset($cachedResult['cached']) && method_exists($handler, 'handleStaleIfError')) {
250-
$staleResponse = $handler->handleStaleIfError($cachedResult['cached']);
251-
if ($staleResponse !== null) {
252-
return $staleResponse;
253-
}
254-
}
255-
256-
throw $e;
257-
}
258-
}
259-
260213
/**
261214
* Sends an HTTP request with the specified parameters.
262215
*
@@ -312,6 +265,53 @@ public function getEffectiveTimeout(): int
312265
return self::DEFAULT_TIMEOUT;
313266
}
314267

268+
/**
269+
* Execute a synchronous request with caching support.
270+
*
271+
* @param string $method The HTTP method
272+
* @param string $uri The full URI
273+
* @param array<string, mixed> $options The Guzzle options
274+
* @param float $startTime The request start time
275+
* @param array<string, mixed>|null $cachedResult The cached result data
276+
* @param mixed $handler The cloned handler instance with request-specific state
277+
* @return ResponseInterface The response
278+
*/
279+
protected function executeSyncRequestWithCache(
280+
string $method,
281+
string $uri,
282+
array $options,
283+
float $startTime,
284+
?array $cachedResult,
285+
mixed $handler
286+
): ResponseInterface {
287+
try {
288+
$response = $handler->executeSyncRequest($method, $uri, $options, $startTime);
289+
290+
// Handle 304 Not Modified response
291+
if ($response->getStatusCode() === 304 && $cachedResult !== null && isset($cachedResult['cached']) && method_exists($handler, 'handleNotModified')) {
292+
$response = $handler->handleNotModified($cachedResult['cached'], $response);
293+
}
294+
295+
// Cache the response if caching is enabled
296+
// Use handler options which includes cache config
297+
if (method_exists($handler, 'cacheResponse') && method_exists($handler, 'isCacheEnabled') && $handler->isCacheEnabled()) {
298+
$handler->cacheResponse($method, $uri, $response, $handler->options);
299+
}
300+
301+
return $response;
302+
} catch (\Throwable $e) {
303+
// Handle stale-if-error: serve stale response on error
304+
if ($cachedResult !== null && isset($cachedResult['cached']) && method_exists($handler, 'handleStaleIfError')) {
305+
$staleResponse = $handler->handleStaleIfError($cachedResult['cached']);
306+
if ($staleResponse !== null) {
307+
return $staleResponse;
308+
}
309+
}
310+
311+
throw $e;
312+
}
313+
}
314+
315315
/**
316316
* Send an HTTP request with a body.
317317
*

0 commit comments

Comments
 (0)