Completed
Pull Request — 1.x (#53)
by Akihito
01:24
created

QueryRepository   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 4
dl 0
loc 196
ccs 50
cts 50
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A updateEtagDatabase() 0 13 2
A __construct() 0 11 1
A put() 0 24 5
A get() 0 12 2
A purge() 0 7 1
A deleteEtagDatabase() 0 8 1
A evaluateBody() 0 13 4
A getExpiryTime() 0 12 4
A getExpiryAtSec() 0 11 2
A setMaxAge() 0 10 2
A getVaryUri() 0 16 4
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
    public function __construct(
47
        EtagSetterInterface $setEtag,
48
        Cache $kvs,
49
        Reader $reader,
50
        Expiry $expiry
51 16
    ) {
52
        $this->setEtag = $setEtag;
53
        $this->reader = $reader;
54
        $this->kvs = $kvs;
55
        $this->expiry = $expiry;
56
    }
57 16
58 16
    /**
59 16
     * {@inheritdoc}
60 16
     *
61 16
     * @throws \ReflectionException
62
     */
63
    public function put(ResourceObject $ro)
64
    {
65
        $ro->toString();
66 14
        $httpCache = $this->reader->getClassAnnotation(new \ReflectionClass($ro), HttpCache::class);
67
        $cacheable = $this->reader->getClassAnnotation(new \ReflectionClass($ro), Cacheable::class);
68 14
        ($this->setEtag)($ro, null, $httpCache);
69 14
        if (isset($ro->headers['ETag'])) {
70 14
            $this->updateEtagDatabase($ro);
71
        }
72
        $body = $this->evaluateBody($ro->body);
73 14
        $lifeTime = $this->getExpiryTime($ro, $cacheable);
0 ignored issues
show
Bug introduced by
It seems like $cacheable defined by $this->reader->getClassA...ation\Cacheable::class) on line 67 can also be of type object; however, BEAR\QueryRepository\Que...sitory::getExpiryTime() does only seem to accept null|object<BEAR\Reposit...e\Annotation\Cacheable>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
74 14
        $this->setMaxAge($ro, $lifeTime);
75 14
        $id = $this->getVaryUri($ro->uri);
76 1
        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...
77
            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...
78 1
                // render
79
                $ro->view = $ro->toString();
80
            }
81 1
82
            return $this->kvs->save($id, [$ro->uri, $ro->code, $ro->headers, $body, $ro->view], $lifeTime);
83
        }
84 13
        // "value" cache type
85
        return $this->kvs->save($id, [$ro->uri, $ro->code, $ro->headers, $body, null], $lifeTime);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90 14
     */
91
    public function get(AbstractUri $uri)
92 14
    {
93 14
        $id = $this->getVaryUri($uri);
94 13
        $data = $this->kvs->fetch($id);
95
        if ($data === false) {
96
            return false;
97 10
        }
98
        $age = \time() - \strtotime($data[2]['Last-Modified']);
99
        $data[2]['Age'] = $age;
100
101
        return $data;
102
    }
103 9
104
    /**
105 9
     * {@inheritdoc}
106
     */
107 9
    public function purge(AbstractUri $uri)
108
    {
109
        $id = $this->getVaryUri($uri);
110
        $this->deleteEtagDatabase($uri);
111
112
        return $this->kvs->delete($id);
113
    }
114
115 9
    /**
116
     * Delete etag in etag repository
117 9
     *
118 9
     * @param AbstractUri $uri
119
     */
120 9
    public function deleteEtagDatabase(AbstractUri $uri)
121 9
    {
122
        $etagId = self::ETAG_BY_URI . $this->getVaryUri($uri); // invalidate etag
123
124
        $oldEtagKey = $this->kvs->fetch($etagId);
125
126 14
        $this->kvs->delete($oldEtagKey);
127
    }
128 14
129 12
    private function evaluateBody($body)
130
    {
131 12
        if (! \is_array($body)) {
132
            return $body;
133
        }
134 2
        foreach ($body as &$item) {
135
            if ($item instanceof RequestInterface) {
136
                $item = ($item)();
137
            }
138
        }
139
140
        return $body;
141
    }
142 14
143
    /**
144 14
     * Update etag in etag repository
145 14
     *
146 14
     * @param ResourceObject $ro
147 14
     */
148 14
    private function updateEtagDatabase(ResourceObject $ro)
149 7
    {
150
        $etag = $ro->headers['ETag'];
151 14
        $uri = (string) $ro->uri;
152 14
        $etagUri = self::ETAG_BY_URI . $uri;
153 14
        $oldEtag = $this->kvs->fetch($etagUri);
154 14
        if ($oldEtag) {
155
            $this->kvs->delete($oldEtag);
156
        }
157
        $etagId = \BEAR\QueryRepository\HttpCache::ETAG_KEY . $etag;
158
        $this->kvs->save($etagId, $uri);     // save etag
159
        $this->kvs->save($etagUri, $etagId); // save uri  mapping etag
160
    }
161 15
162
    private function getExpiryTime(ResourceObject $ro, Cacheable $cacheable = null) : int
163 15
    {
164 1
        if ($cacheable === null) {
165
            return 0;
166
        }
167 14
168
        if ($cacheable->expiryAt) {
169
            return $this->getExpiryAtSec($ro, $cacheable);
170
        }
171
172
        return $cacheable->expirySecond ? $cacheable->expirySecond : $this->expiry[$cacheable->expiry];
173
    }
174
175
    private function getExpiryAtSec(ResourceObject $ro, Cacheable $cacheable) : int
176
    {
177
        if (! isset($ro->body[$cacheable->expiryAt])) {
178
            $msg = \sprintf('%s::%s', \get_class($ro), $cacheable->expiryAt);
179
            throw new ExpireAtKeyNotExists($msg);
180
        }
181
        $expiryAt = $ro->body[$cacheable->expiryAt];
182
        $sec = \strtotime($expiryAt) - \time();
183
184
        return $sec;
185
    }
186
187
    private function setMaxAge(ResourceObject $ro, int $age)
188
    {
189
        $setMaxAge = \sprintf('max-age=%d', $age);
190
        if (isset($ro->headers['Cache-Control'])) {
191
            $ro->headers['Cache-Control'] .= ', ' . $setMaxAge;
192
193
            return;
194
        }
195
        $ro->headers['Cache-Control'] = $setMaxAge;
196
    }
197
198
    private function getVaryUri(AbstractUri $uri) : string
199
    {
200
        if (! isset($_SERVER['X_VARY'])) {
201
            return (string) $uri;
202
        }
203
        $varys = \explode(',', $_SERVER['X_VARY']);
204
        $varyId = '';
205
        foreach ($varys as $vary) {
206
            $phpVaryKey = \sprintf('X_%s', \strtoupper($vary));
207
            if (isset($_SERVER[$phpVaryKey])) {
208
                $varyId .= $_SERVER[$phpVaryKey];
209
            }
210
        }
211
212
        return (string) $uri . $varyId;
213
    }
214
}
215