CdnCacheControlHeaderSetter::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository;
6
7
use BEAR\Resource\ResourceObject;
8
use Override;
9
10
use function sprintf;
11
12
/**
13
 * @see https://www.ietf.org/archive/id/draft-cdn-control-header-01.html
14
 * @see https://blog.cloudflare.com/cdn-cache-control/
15
 */
16
final class CdnCacheControlHeaderSetter implements CdnCacheControlHeaderSetterInterface
17
{
18
    private const CDN_CACHE_CONTROL_HEADER = Header::CDN_CACHE_CONTROL;
19
20
    #[Override]
21
    public function __invoke(ResourceObject $ro, int|null $sMaxAge): void
22
    {
23
        $sMaxAge ??= 10;
24
        if (! isset($ro->headers[self::CDN_CACHE_CONTROL_HEADER])) {
25
            $ro->headers[self::CDN_CACHE_CONTROL_HEADER] = sprintf('max-age=%s stale-while-revalidate=10', (string) $sMaxAge);
26
        }
27
    }
28
}
29