Kevinrob\GuzzleCache\CacheMiddleware::addReValidationRequest PHP Method

addReValidationRequest() protected method

protected addReValidationRequest ( Psr\Http\Message\RequestInterface $request, Kevinrob\GuzzleCache\Strategy\CacheStrategyInterface &$cacheStorage, CacheEntry $cacheEntry ) : boolean
$request Psr\Http\Message\RequestInterface
$cacheStorage Kevinrob\GuzzleCache\Strategy\CacheStrategyInterface
$cacheEntry CacheEntry
return boolean if added
    protected function addReValidationRequest(RequestInterface $request, CacheStrategyInterface &$cacheStorage, CacheEntry $cacheEntry)
    {
        // Add the promise for revalidate
        if ($this->client !== null) {
            /** @var RequestInterface $request */
            $request = $request->withHeader(self::HEADER_RE_VALIDATION, '1');
            $this->waitingRevalidate[] = $this->client->sendAsync($request)->then(function (ResponseInterface $response) use($request, &$cacheStorage, $cacheEntry) {
                $update = false;
                if ($response->getStatusCode() == 304) {
                    // Not modified => cache entry is re-validate
                    /** @var ResponseInterface $response */
                    $response = $response->withStatus($cacheEntry->getResponse()->getStatusCode());
                    $response = $response->withBody($cacheEntry->getResponse()->getBody());
                    // Merge headers of the "304 Not Modified" and the cache entry
                    foreach ($cacheEntry->getResponse()->getHeaders() as $headerName => $headerValue) {
                        if (!$response->hasHeader($headerName)) {
                            $response = $response->withHeader($headerName, $headerValue);
                        }
                    }
                    $update = true;
                }
                self::addToCache($cacheStorage, $request, $response, $update);
            });
            return true;
        }
        return false;
    }