1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
<?php
namespace Gedcomx\Rs\Client\Options;
use Gedcomx\Rs\Client\GedcomxApplicationState;
use GuzzleHttp\Psr7\Request;
/**
* A REST API request options helper providing cache control features. This is similar to the Preconditions class,
* but applies inverse logic.
*
* Class CacheDirectives
* @package Gedcomx\Rs\Client\Options
*/
class CacheDirectives implements StateTransitionOption
{
private $etag;
private $lastModified;
/**
* Constructs a cache directives class using the specified state. If the ETag (entity tag) specified here does
* not match the server's ETag for a resource, the resource will be returned; otherwise, a not-modified status
* is returned. The same applies to last modified. If the server's last modified date for a resource is greater
* than the last modified specified here, the resource will be returned; otherwise, a not-modified status is
* returned.
*
* @param GedcomxApplicationState $state
*/
public function __construct(GedcomxApplicationState $state)
{
$this->etag = $state->getETag();
$this->lastModified = $state->getLastModified();
}
/**
* Applies the ETag or last modified cache control headers to the specified REST API request. If the ETag
* (entity tag) specified here does not match the server's ETag for a resource, the resource will be returned;
* otherwise, a not-modified status is returned. The same applies to last modified. If the server's last
* modified date for a resource is greater than the last modified specified here, the resource will be returned;
* otherwise, a not-modified status is returned.
*
* @param Request $request
* @param Request $request
*/
public function apply(Request $request)
{
$newRequest = $request;
if ($this->etag !== null) {
$newRequest = $request->withHeader(HeaderParameter::IF_MATCH, $this->etag);
}
if ($this->lastModified !== null) {
$newRequest = $request->withHeader(HeaderParameter::IF_UNMODIFIED_SINCE, $this->lastModified);
}
return $newRequest;
}
}