-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathHeaderSetter.php
More file actions
50 lines (39 loc) · 1.34 KB
/
Copy pathHeaderSetter.php
File metadata and controls
50 lines (39 loc) · 1.34 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
<?php
declare(strict_types=1);
namespace BEAR\QueryRepository;
use BEAR\RepositoryModule\Annotation\HttpCache;
use BEAR\Resource\ResourceObject;
use function is_int;
use function sprintf;
use function str_contains;
final readonly class HeaderSetter
{
public function __construct(
private EtagSetterInterface $etagSetter,
) {
}
public function __invoke(ResourceObject $ro, int|null $concheControlMaxAge, HttpCache|null $httpCache): void
{
($this->etagSetter)($ro, null, $httpCache);
if (is_int($concheControlMaxAge) && $concheControlMaxAge > 0) {
$this->setCacheControlMaxAge($ro, $concheControlMaxAge);
}
}
private function setCacheControlMaxAge(ResourceObject $ro, int $age): void
{
$setMaxAge = sprintf('max-age=%d', $age);
$hasNoCacheControleHeader = ! isset($ro->headers[Header::CACHE_CONTROL]);
$headers = $ro->headers;
if ($hasNoCacheControleHeader) {
$ro->headers[Header::CACHE_CONTROL] = $setMaxAge;
return;
}
$isMaxAgeAlreadyDefined = str_contains($headers[Header::CACHE_CONTROL], 'max-age');
if ($isMaxAgeAlreadyDefined) {
return;
}
if (isset($ro->headers[Header::CACHE_CONTROL])) {
$ro->headers[Header::CACHE_CONTROL] .= ', ' . $setMaxAge;
}
}
}