Passed
Push — php82-readonly-optimization ( 7445f1 )
by Akihito
03:31
created

QueryRepository   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 38
dl 0
loc 103
ccs 36
cts 38
cp 0.9474
rs 10
c 2
b 0
f 0
wmc 19
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository;
6
7
use BEAR\QueryRepository\Exception\ExpireAtKeyNotExists;
8
use BEAR\RepositoryModule\Annotation\Cacheable;
9
use BEAR\RepositoryModule\Annotation\HttpCache;
10
use BEAR\Resource\AbstractUri;
11
use BEAR\Resource\ResourceObject;
12
use Override;
13
use ReflectionClass;
14
15
use function is_array;
16
use function sprintf;
17
use function strtotime;
18
use function time;
19
20
final readonly class QueryRepository implements QueryRepositoryInterface
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 20 at column 6
Loading history...
21
{
22
    public function __construct(
23
        private RepositoryLoggerInterface $logger,
24
        private HeaderSetter $headerSetter,
25
        private ResourceStorageInterface $storage,
26
        private Expiry $expiry,
27
    ) {
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    #[Override]
34
    public function put(ResourceObject $ro)
35
    {
36
        $this->logger->log('put-query-repository uri:%s', $ro->uri);
37
        $this->storage->deleteEtag($ro->uri);
38
        $ro->toString();
39
        $cacheable = $this->getCacheableAnnotation($ro);
40
        $httpCache = $this->getHttpCacheAnnotation($ro);
41
        $ttl = $this->getExpiryTime($ro, $cacheable);
42
        ($this->headerSetter)($ro, $ttl, $httpCache);
43
        if (isset($ro->headers[Header::ETAG])) {
44
            $etag = $ro->headers[Header::ETAG];
45
            $surrogateKeys = $ro->headers[Header::SURROGATE_KEY] ?? '';
46
            $this->storage->saveEtag($ro->uri, $etag, $surrogateKeys, $ttl);
47
        }
48
49
        if ($cacheable instanceof Cacheable && $cacheable->type === 'view') {
50
            return $this->storage->saveView($ro, $ttl);
51
        }
52
53
        return $this->storage->saveValue($ro, $ttl);
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    #[Override]
60
    public function get(AbstractUri $uri): ResourceState|null
61
    {
62
        $state = $this->storage->get($uri);
63
64
        if (! $state instanceof ResourceState) {
65
            return null;
66
        }
67
68
        $state->headers[Header::AGE] = (string) (time() - (int) strtotime($state->headers[Header::LAST_MODIFIED]));
69
70
        return $state;
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    #[Override]
77
    public function purge(AbstractUri $uri)
78
    {
79
        $this->logger->log('purge-query-repository uri:%s', $uri);
80
81
        return $this->storage->deleteEtag($uri);
82
    }
83
84
    private function getHttpCacheAnnotation(ResourceObject $ro): HttpCache|null
85
    {
86
        $attributes = (new ReflectionClass($ro))->getAttributes(HttpCache::class);
87
88
        return isset($attributes[0]) ? $attributes[0]->newInstance() : null;
89
    }
90
91
    private function getCacheableAnnotation(ResourceObject $ro): Cacheable|null
92
    {
93
        $attributes = (new ReflectionClass($ro))->getAttributes(Cacheable::class);
94
95
        return isset($attributes[0]) ? $attributes[0]->newInstance() : null;
96
    }
97
98
    private function getExpiryTime(ResourceObject $ro, Cacheable|null $cacheable = null): int
99
    {
100
        if ($cacheable === null) {
101
            return 0;
102
        }
103
104
        if ($cacheable->expiryAt !== '') {
105
            return $this->getExpiryAtSec($ro, $cacheable);
106
        }
107
108
        return $cacheable->expirySecond ?: $this->expiry->getTime($cacheable->expiry);
109
    }
110
111
    private function getExpiryAtSec(ResourceObject $ro, Cacheable $cacheable): int
112
    {
113
        if (! is_array($ro->body) || ! isset($ro->body[$cacheable->expiryAt])) {
114
            $msg = sprintf('%s::%s', $ro::class, $cacheable->expiryAt);
115
116
            throw new ExpireAtKeyNotExists($msg);
117
        }
118
119
        /** @var string $expiryAt */
120
        $expiryAt = $ro->body[$cacheable->expiryAt];
121
122
        return (int) strtotime($expiryAt) - time();
123
    }
124
}
125