QueryRepository   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 36
c 3
b 0
f 0
dl 0
loc 100
rs 10
ccs 29
cts 29
cp 1
wmc 17

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getExpiryTime() 0 11 4
A put() 0 21 4
A __construct() 0 7 1
A purge() 0 6 1
A get() 0 12 2
A getCacheableAnnotation() 0 3 1
A getExpiryAtSec() 0 12 3
A getHttpCacheAnnotation() 0 3 1
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, BEAR\QueryRepository\HttpCache. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use BEAR\Resource\AbstractUri;
11
use BEAR\Resource\ResourceObject;
12
use Doctrine\Common\Annotations\Reader;
13
use Override;
14
use ReflectionClass;
15
16
use function is_array;
17
use function sprintf;
18
use function strtotime;
19
use function time;
20
21
final class QueryRepository implements QueryRepositoryInterface
22
{
23
    public function __construct(
24
        private readonly RepositoryLoggerInterface $logger,
25
        private readonly HeaderSetter $headerSetter,
26
        private readonly ResourceStorageInterface $storage,
27
        private readonly Reader $reader,
28
        private readonly Expiry $expiry,
0 ignored issues
show
Bug introduced by
The type BEAR\QueryRepository\Expiry was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
    ) {
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
    #[Override]
36
    public function put(ResourceObject $ro)
37
    {
38
        $this->logger->log('put-query-repository uri:%s', $ro->uri);
39
        $this->storage->deleteEtag($ro->uri);
40
        $ro->toString();
41
        $cacheable = $this->getCacheableAnnotation($ro);
42
        $httpCache = $this->getHttpCacheAnnotation($ro);
43
        $ttl = $this->getExpiryTime($ro, $cacheable);
44
        ($this->headerSetter)($ro, $ttl, $httpCache);
45
        if (isset($ro->headers[Header::ETAG])) {
46
            $etag = $ro->headers[Header::ETAG];
47
            $surrogateKeys = $ro->headers[Header::SURROGATE_KEY] ?? '';
48
            $this->storage->saveEtag($ro->uri, $etag, $surrogateKeys, $ttl);
49
        }
50
51 16
        if ($cacheable instanceof Cacheable && $cacheable->type === 'view') {
52
            return $this->storage->saveView($ro, $ttl);
53
        }
54
55
        return $this->storage->saveValue($ro, $ttl);
56
    }
57 16
58 16
    /**
59 16
     * {@inheritDoc}
60 16
     */
61 16
    #[Override]
62
    public function get(AbstractUri $uri): ResourceState|null
63
    {
64
        $state = $this->storage->get($uri);
65
66 14
        if (! $state instanceof ResourceState) {
67
            return null;
68 14
        }
69 14
70 14
        $state->headers[Header::AGE] = (string) (time() - (int) strtotime($state->headers[Header::LAST_MODIFIED]));
71
72
        return $state;
73 14
    }
74 14
75 14
    /**
76 1
     * {@inheritDoc}
77
     */
78 1
    #[Override]
79
    public function purge(AbstractUri $uri)
80
    {
81 1
        $this->logger->log('purge-query-repository uri:%s', $uri);
82
83
        return $this->storage->deleteEtag($uri);
84 13
    }
85
86
    private function getHttpCacheAnnotation(ResourceObject $ro): HttpCache|null
87
    {
88
        return $this->reader->getClassAnnotation(new ReflectionClass($ro), HttpCache::class);
89
    }
90 14
91
    private function getCacheableAnnotation(ResourceObject $ro): Cacheable|null
92 14
    {
93 14
        return $this->reader->getClassAnnotation(new ReflectionClass($ro), Cacheable::class);
94 13
    }
95
96
    private function getExpiryTime(ResourceObject $ro, Cacheable|null $cacheable = null): int
97 10
    {
98
        if ($cacheable === null) {
99
            return 0;
100
        }
101
102
        if ($cacheable->expiryAt !== '') {
103 9
            return $this->getExpiryAtSec($ro, $cacheable);
104
        }
105 9
106
        return $cacheable->expirySecond ?: $this->expiry->getTime($cacheable->expiry);
107 9
    }
108
109
    private function getExpiryAtSec(ResourceObject $ro, Cacheable $cacheable): int
110
    {
111
        if (! is_array($ro->body) || ! isset($ro->body[$cacheable->expiryAt])) {
112
            $msg = sprintf('%s::%s', $ro::class, $cacheable->expiryAt);
113
114
            throw new ExpireAtKeyNotExists($msg);
115 9
        }
116
117 9
        /** @var string $expiryAt */
118 9
        $expiryAt = $ro->body[$cacheable->expiryAt];
119
120 9
        return (int) strtotime($expiryAt) - time();
121 9
    }
122
}
123