/** * Determine if resource was NOT modified * since requester's last check * * For Concurrency Control (Lost Update Problem) * Uses `If-Match` and/or `If-Unmodified-Since` */ public function wasNotModified(ResourceInterface $resource) { /* * Unlike If-None-Match, the spec on If-Match * does NOT say if If-Unmodified-Since * should be ignored if an ETag was given via * If-Match. * * We'll assume the same behaviour anyway. */ $usesEtags = false; // First, ETag Validation $etag = str_replace('"', '', $this->getHeader('if-match')); if ($etag) { $usesEtags = true; if ($etag !== $resource->getEtag()) { return false; } } // Second, Modification Date Validation $ifUnmodifiedSince = $this->getHeader('if-unmodified-since'); if ($ifUnmodifiedSince && $usesEtags === false) { if (strtotime($ifUnmodifiedSince) < $resource->getLastUpdated()->getTimestamp()) { return false; } } return true; }
/** * Set cache settings (simple for now) * * @param Fideloper\ResourceCache\Resource\ResourceInterface * @param Symfony\Component\HttpFoundation\Response * @return Symfony\Component\HttpFoundation\Response */ protected function setCache(ResourceInterface $resource, HttpResponse $response) { $response->setCache(array('etag' => $resource->getEtag(), 'last_modified' => $resource->getLastUpdated())); return $response; }