Completed
Pull Request — 1.x (#53)
by Akihito
04:27
created

QueryRepository::setMaxAge()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * This file is part of the BEAR.QueryRepository package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\QueryRepository;
8
9
use BEAR\QueryRepository\Exception\ExpireAtKeyNotExists;
10
use BEAR\RepositoryModule\Annotation\Cacheable;
11
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...
12
use BEAR\RepositoryModule\Annotation\Storage;
13
use BEAR\Resource\AbstractUri;
14
use BEAR\Resource\RequestInterface;
15
use BEAR\Resource\ResourceObject;
16
use Doctrine\Common\Annotations\Reader;
17
use Doctrine\Common\Cache\Cache;
18
19
class QueryRepository implements QueryRepositoryInterface
20
{
21
    const ETAG_BY_URI = 'etag-by-uri';
22
23
    /**
24
     * @var Cache
25
     */
26
    private $kvs;
27
28
    /**
29
     * @var Reader
30
     */
31
    private $reader;
32
33
    /**
34
     * @var Expiry
35
     */
36
    private $expiry;
37
38
    /**
39
     * @var EtagSetterInterface
40
     */
41
    private $setEtag;
42
43
    /**
44
     * @Storage("kvs")
45
     */
46 25
    public function __construct(
47
        EtagSetterInterface $setEtag,
48
        Cache $kvs,
49
        Reader $reader,
50
        Expiry $expiry
51
    ) {
52 25
        $this->setEtag = $setEtag;
53 25
        $this->reader = $reader;
54 25
        $this->kvs = $kvs;
55 25
        $this->expiry = $expiry;
56 25
    }
57
58
    /**
59
     * {@inheritdoc}
60
     *
61
     * @throws \ReflectionException
62
     */
63 23
    public function put(ResourceObject $ro)
64
    {
65 23
        $ro->toString();
66 23
        $httpCache = $this->getHttpCacheAnnotation($ro);
67 23
        $cacheable = $this->getCacheableAnnotation($ro);
68
        /* @var Cacheable $cacheable|null */
69 23
        ($this->setEtag)($ro, null, $httpCache);
70 23
        if (isset($ro->headers['ETag'])) {
71 23
            $this->updateEtagDatabase($ro);
72
        }
73 23
        $body = $this->evaluateBody($ro->body);
74 23
        $lifeTime = $this->getExpiryTime($ro, $cacheable);
75 22
        $this->setMaxAge($ro, $lifeTime);
76 22
        $id = $this->getVaryUri($ro->uri);
77 22
        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...
78 2
            if (! $ro->view) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ro->view of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
79
                // render
80
                $ro->view = $ro->toString();
81
            }
82
83 2
            return $this->kvs->save($id, [$ro->uri, $ro->code, $ro->headers, $body, $ro->view], $lifeTime);
84
        }
85
        // "value" cache type
86 20
        return $this->kvs->save($id, [$ro->uri, $ro->code, $ro->headers, $body, null], $lifeTime);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 21
    public function get(AbstractUri $uri)
93
    {
94 21
        $id = $this->getVaryUri($uri);
95 21
        $data = $this->kvs->fetch($id);
96 21
        if ($data === false) {
97 19
            return false;
98
        }
99 14
        $age = \time() - \strtotime($data[2]['Last-Modified']);
100 14
        $data[2]['Age'] = $age;
101
102 14
        return $data;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 12
    public function purge(AbstractUri $uri)
109
    {
110 12
        $id = $this->getVaryUri($uri);
111 12
        $this->deleteEtagDatabase($uri);
112
113 12
        return $this->kvs->delete($id);
114
    }
115
116
    /**
117
     * Delete etag in etag repository
118
     *
119
     * @param AbstractUri $uri
120
     */
121 12
    public function deleteEtagDatabase(AbstractUri $uri)
122
    {
123 12
        $etagId = self::ETAG_BY_URI . $this->getVaryUri($uri); // invalidate etag
124
125 12
        $oldEtagKey = $this->kvs->fetch($etagId);
126
127 12
        $this->kvs->delete($oldEtagKey);
128 12
    }
129
130
    /**
131
     * @throws \ReflectionException
132
     */
133 23
    private function getHttpCacheAnnotation(ResourceObject $ro)
134
    {
135 23
        $annotation = $this->reader->getClassAnnotation(new \ReflectionClass($ro), HttpCache::class);
136 23
        if ($annotation instanceof HttpCache || $annotation === null) {
0 ignored issues
show
Bug introduced by
The class BEAR\RepositoryModule\Annotation\HttpCache 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...
137 23
            return $annotation;
138
        }
139
        throw new \LogicException();
140
    }
141
142
    /**
143
     * @throws \ReflectionException
144
     */
145 23
    private function getCacheableAnnotation(ResourceObject $ro)
146
    {
147 23
        $annotation = $this->reader->getClassAnnotation(new \ReflectionClass($ro), Cacheable::class);
148 23
        if ($annotation instanceof Cacheable || $annotation === null) {
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...
149 23
            return $annotation;
150
        }
151
        throw new \LogicException();
152
    }
153
154
    /**
155
     * @param mixed $body
156
     *
157
     * @return mixed
158
     */
159 23
    private function evaluateBody($body)
160
    {
161 23
        if (! \is_array($body)) {
162 7
            return $body;
163
        }
164 16
        foreach ($body as &$item) {
165 16
            if ($item instanceof RequestInterface) {
166 16
                $item = ($item)();
167
            }
168
        }
169
170 16
        return $body;
171
    }
172
173
    /**
174
     * Update etag in etag repository
175
     *
176
     * @param ResourceObject $ro
177
     */
178 23
    private function updateEtagDatabase(ResourceObject $ro)
179
    {
180 23
        $etag = $ro->headers['ETag'];
181 23
        $uri = (string) $ro->uri;
182 23
        $etagUri = self::ETAG_BY_URI . $uri;
183 23
        $oldEtag = $this->kvs->fetch($etagUri);
184 23
        if ($oldEtag) {
185 11
            $this->kvs->delete($oldEtag);
186
        }
187 23
        $etagId = \BEAR\QueryRepository\HttpCache::ETAG_KEY . $etag;
188 23
        $this->kvs->save($etagId, $uri);     // save etag
189 23
        $this->kvs->save($etagUri, $etagId); // save uri  mapping etag
190 23
    }
191
192 24
    private function getExpiryTime(ResourceObject $ro, Cacheable $cacheable = null) : int
193
    {
194 24
        if ($cacheable === null) {
195 4
            return 0;
196
        }
197
198 20
        if ($cacheable->expiryAt) {
199 2
            return $this->getExpiryAtSec($ro, $cacheable);
200
        }
201
202 18
        return $cacheable->expirySecond ? $cacheable->expirySecond : $this->expiry[$cacheable->expiry];
203
    }
204
205 2
    private function getExpiryAtSec(ResourceObject $ro, Cacheable $cacheable) : int
206
    {
207 2
        if (! isset($ro->body[$cacheable->expiryAt])) {
208 1
            $msg = \sprintf('%s::%s', \get_class($ro), $cacheable->expiryAt);
209 1
            throw new ExpireAtKeyNotExists($msg);
210
        }
211 1
        $expiryAt = $ro->body[$cacheable->expiryAt];
212 1
        $sec = \strtotime($expiryAt) - \time();
213
214 1
        return $sec;
215
    }
216
217 22
    private function setMaxAge(ResourceObject $ro, int $age)
218
    {
219 22
        $setMaxAge = \sprintf('max-age=%d', $age);
220 22
        if (isset($ro->headers['Cache-Control'])) {
221 6
            $ro->headers['Cache-Control'] .= ', ' . $setMaxAge;
222
223 6
            return;
224
        }
225 19
        $ro->headers['Cache-Control'] = $setMaxAge;
226 19
    }
227
228 23
    private function getVaryUri(AbstractUri $uri) : string
229
    {
230 23
        if (! isset($_SERVER['X_VARY'])) {
231 15
            return (string) $uri;
232
        }
233 9
        $varys = \explode(',', $_SERVER['X_VARY']);
234 9
        $varyId = '';
235 9
        foreach ($varys as $vary) {
236 9
            $phpVaryKey = \sprintf('X_%s', \strtoupper($vary));
237 9
            if (isset($_SERVER[$phpVaryKey])) {
238 9
                $varyId .= $_SERVER[$phpVaryKey];
239
            }
240
        }
241
242 9
        return (string) $uri . $varyId;
243
    }
244
}
245