-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCachedResponse.php
More file actions
62 lines (53 loc) · 1.33 KB
/
CachedResponse.php
File metadata and controls
62 lines (53 loc) · 1.33 KB
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
58
59
60
61
62
<?php
declare(strict_types=1);
namespace Saloon\CachePlugin\Data;
use DateInterval;
use DateTimeImmutable;
use Saloon\Data\RecordedResponse;
use Saloon\Http\Faking\FakeResponse;
use Saloon\Contracts\FakeResponse as FakeResponseContract;
class CachedResponse
{
/**
* Constructor
*/
public function __construct(
public readonly RecordedResponse $recordedResponse,
public readonly DateTimeImmutable $expiresAt,
) {
//
}
/**
* Check if the response has expired.
*/
public function hasExpired(): bool
{
return $this->expiresAt->getTimestamp() <= (new DateTimeImmutable)->getTimestamp();
}
/**
* Check if the response has not expired.
*/
public function hasNotExpired(): bool
{
return ! $this->hasExpired();
}
/**
* Get the cache TTL as an interval based on the expiry date.
*/
public function getTtl(): DateInterval
{
return (new DateTimeImmutable())->diff($this->expiresAt);
}
/**
* Create a fake response
*/
public function getFakeResponse(): FakeResponseContract
{
$response = $this->recordedResponse;
return new FakeResponse(
body: $response->data,
status: $response->statusCode,
headers: $response->headers
);
}
}