Completed
Push — 1.x ( 5ef5a6...a03999 )
by Akihito
12s
created

QueryRepository::put()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 15
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 21
ccs 6
cts 6
cp 1
crap 4
rs 9.7666
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 29
        $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 29
        if (isset($ro->headers[Header::ETAG])) {
44 29
            $etag = $ro->headers[Header::ETAG];
45 29
            $surrogateKeys = $ro->headers[Header::SURROGATE_KEY] ?? '';
46 29
            $this->storage->saveEtag($ro->uri, $etag, $surrogateKeys, $ttl);
47 29
        }
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 27
    }
55
56 27
    /**
57 27
     * {@inheritDoc}
58 27
     */
59 27
    #[Override]
60 27
    public function get(AbstractUri $uri): ResourceState|null
61 26
    {
62 26
        $state = $this->storage->get($uri);
63
64 26
        if (! $state instanceof ResourceState) {
65 26
            return null;
66 2
        }
67
68
        $state->headers[Header::AGE] = (string) (time() - (int) strtotime($state->headers[Header::LAST_MODIFIED]));
69 24
70
        return $state;
71
    }
72
73
    /**
74
     * {@inheritDoc}
75 25
     */
76
    #[Override]
77 25
    public function purge(AbstractUri $uri)
78 25
    {
79 23
        $this->logger->log('purge-query-repository uri:%s', $uri);
80
81 15
        return $this->storage->deleteEtag($uri);
82 15
    }
83
84 15
    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 12
91
    private function getCacheableAnnotation(ResourceObject $ro): Cacheable|null
92 12
    {
93
        $attributes = (new ReflectionClass($ro))->getAttributes(Cacheable::class);
94 12
95
        return isset($attributes[0]) ? $attributes[0]->newInstance() : null;
96
    }
97
98
    private function getExpiryTime(ResourceObject $ro, Cacheable|null $cacheable = null): int
99
    {
100 27
        if ($cacheable === null) {
101
            return 0;
102 27
        }
103 27
104 27
        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 27
116
            throw new ExpireAtKeyNotExists($msg);
117 27
        }
118 27
119 27
        /** @var string $expiryAt */
120
        $expiryAt = $ro->body[$cacheable->expiryAt];
121
122
        return (int) strtotime($expiryAt) - time();
123
    }
124
}
125