Completed
Pull Request — 1.x (#81)
by Akihito
22:36
created

QueryRepository::evaluateBody()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.5923

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 4
cts 6
cp 0.6667
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4.5923
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.

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 LogicException;
14
use ReflectionClass;
15
use ReflectionException;
16
17
use function assert;
18
use function get_class;
19
use function is_array;
20
use function is_string;
21
use function sprintf;
22
use function strpos;
23
use function strtotime;
24
use function time;
25
26
final class QueryRepository implements QueryRepositoryInterface
27
{
28
    /** @var ResourceStorageInterface */
29
    private $storage;
30
31
    /** @var Reader */
32
    private $reader;
33
34
    /** @var Expiry */
35
    private $expiry;
36
37 29
    /** @var EtagSetterInterface */
38
    private $setEtag;
39
40
    public function __construct(
41
        EtagSetterInterface $setEtag,
42
        ResourceStorageInterface $storage,
43 29
        Reader $reader,
44 29
        Expiry $expiry
45 29
    ) {
46 29
        $this->setEtag = $setEtag;
47 29
        $this->reader = $reader;
48
        $this->storage = $storage;
49
        $this->expiry = $expiry;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54 27
     *
55
     * @throws ReflectionException
56 27
     */
57 27
    public function put(ResourceObject $ro)
58 27
    {
59 27
        $ro->toString();
60 27
        $httpCache = $this->getHttpCacheAnnotation($ro);
61 26
        $cacheable = $this->getCacheableAnnotation($ro);
62 26
        ($this->setEtag)($ro, null, $httpCache);
63
        $lifeTime = $this->getExpiryTime($ro, $cacheable);
64 26
        if (isset($ro->headers['ETag'])) {
65 26
            $this->storage->updateEtag($ro, $lifeTime);
66 2
        }
67
68
        $this->setMaxAge($ro, $lifeTime);
69 24
        if ($cacheable instanceof Cacheable && $cacheable->type === 'view') {
0 ignored issues
show
Bug introduced by
The class BEAR\RepositoryModule\Annotation\Cacheable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
70
            return $this->saveViewCache($ro, $lifeTime);
71
        }
72
73
        return $this->storage->saveValue($ro, $lifeTime);
74
    }
75 25
76
    /**
77 25
     * {@inheritdoc}
78 25
     */
79 23
    public function get(AbstractUri $uri)
80
    {
81 15
        $data = $this->storage->get($uri);
82 15
        if ($data === false) {
83
            return false;
84 15
        }
85
86
        $age = time() - strtotime($data[2]['Last-Modified']);
87
        $data[2]['Age'] = $age;
88
89
        return $data;
90 12
    }
91
92 12
    /**
93
     * {@inheritdoc}
94 12
     */
95
    public function purge(AbstractUri $uri)
96
    {
97
        $this->storage->deleteEtag($uri);
98
99
        return $this->storage->delete($uri);
100 27
    }
101
102 27
    /**
103 27
     * @throws ReflectionException
104 27
     */
105
    private function getHttpCacheAnnotation(ResourceObject $ro): ?HttpCache
106
    {
107
        $annotation = $this->reader->getClassAnnotation(new ReflectionClass($ro), HttpCache::class);
108
109
        return $annotation;
110
    }
111
112
    /**
113
     * @return ?Cacheable
0 ignored issues
show
Documentation introduced by
The doc-type ?Cacheable could not be parsed: Unknown type name "?Cacheable" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
114
     *
115 27
     * @throws ReflectionException
116
     */
117 27
    private function getCacheableAnnotation(ResourceObject $ro): ?Cacheable
118 27
    {
119 27
        $annotation = $this->reader->getClassAnnotation(new ReflectionClass($ro), Cacheable::class);
120
121
        return $annotation;
122
    }
123
124
    private function getExpiryTime(ResourceObject $ro, ?Cacheable $cacheable = null): int
125
    {
126
        if ($cacheable === null) {
127
            return 0;
128
        }
129
130
        if ($cacheable->expiryAt) {
131
            return $this->getExpiryAtSec($ro, $cacheable);
132
        }
133
134
        return $cacheable->expirySecond ? $cacheable->expirySecond : (int) $this->expiry[$cacheable->expiry];
135
    }
136
137
    private function getExpiryAtSec(ResourceObject $ro, Cacheable $cacheable): int
138
    {
139 28
        if (! isset($ro->body[$cacheable->expiryAt])) {
140
            $msg = sprintf('%s::%s', get_class($ro), $cacheable->expiryAt);
141 28
142 4
            throw new ExpireAtKeyNotExists($msg);
143
        }
144
145 24
        assert(is_array($ro->body));
146 2
        $expiryAt = (string) $ro->body[$cacheable->expiryAt];
147
148
        return strtotime($expiryAt) - time();
149 22
    }
150
151
    /**
152 2
     * @return void
153
     */
154 2
    private function setMaxAge(ResourceObject $ro, int $age)
155 1
    {
156
        if ($age === 0) {
157 1
            return;
158
        }
159 1
160
        $setMaxAge = sprintf('max-age=%d', $age);
161 1
        $noCacheControleHeader = ! isset($ro->headers['Cache-Control']);
162
        /** @var array<string, string> $headers */
163
        $headers = $ro->headers;
164 26
        if ($noCacheControleHeader) {
165
            $ro->headers['Cache-Control'] = $setMaxAge;
166 26
167 22
            return;
168
        }
169 4
170 4
        $isMaxAgeAlreadyDefined = strpos($headers['Cache-Control'], 'max-age') !== false;
171 4
        if ($isMaxAgeAlreadyDefined) {
172 1
            return;
173
        }
174 1
175
        if (is_string($ro->headers['Cache-Control'])) {
176 3
            $ro->headers['Cache-Control'] .= ', ' . $setMaxAge;
177 3
        }
178 1
    }
179
180 2
    private function saveViewCache(ResourceObject $ro, int $lifeTime): bool
181 2
    {
182
        if (! $ro->view) {
183 2
            $ro->view = $ro->toString();
184
        }
185 2
186
        return $this->storage->saveView($ro, $lifeTime);
187
    }
188
}
189