-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCachedResponse.php
More file actions
54 lines (46 loc) · 1.15 KB
/
CachedResponse.php
File metadata and controls
54 lines (46 loc) · 1.15 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
<?php
declare(strict_types=1);
namespace Saloon\CachePlugin\Data;
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,
public readonly int $ttl,
) {
//
}
/**
* 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();
}
/**
* Create a fake response
*/
public function getFakeResponse(): FakeResponseContract
{
$response = $this->recordedResponse;
return new FakeResponse(
body: $response->data,
status: $response->statusCode,
headers: $response->headers
);
}
}