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

QueryRepository::evaluateBody()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 5
cts 5
cp 1
rs 9.8333
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 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\Storage;
12
use BEAR\Resource\AbstractUri;
13
use BEAR\Resource\RequestInterface;
14
use BEAR\Resource\ResourceObject;
15
use Doctrine\Common\Annotations\Reader;
16
use Doctrine\Common\Cache\Cache;
17
18
class QueryRepository implements QueryRepositoryInterface
19
{
20
    const ETAG_BY_URI = 'etag-by-uri';
21
22
    /**
23
     * @var Cache
24
     */
25
    private $kvs;
26
27
    /**
28
     * @var Reader
29
     */
30
    private $reader;
31
32
    /**
33
     * @var Expiry
34
     */
35
    private $expiry;
36
37
    /**
38
     * @var EtagSetterInterface
39
     */
40
    private $setEtag;
41
42
    /**
43
     * @Storage("kvs")
44
     */
45
    public function __construct(
46
        EtagSetterInterface $setEtag,
47
        Cache $kvs,
48
        Reader $reader,
49
        Expiry $expiry
50
    ) {
51 16
        $this->setEtag = $setEtag;
52
        $this->reader = $reader;
53
        $this->kvs = $kvs;
54
        $this->expiry = $expiry;
55
    }
56
57 16
    /**
58 16
     * {@inheritdoc}
59 16
     */
60 16
    public function put(ResourceObject $ro)
61 16
    {
62
        $ro->toString();
63
        ($this->setEtag)($ro);
64
        if (isset($ro->headers['ETag'])) {
65
            $this->updateEtagDatabase($ro);
66 14
        }
67
        /* @var $cacheable Cacheable */
68 14
        $cacheable = $this->getCacheable($ro);
69 14
        $body = $this->evaluateBody($ro->body);
70 14
        $lifeTime = $this->getExpiryTime($ro, $cacheable);
71
        $this->setMaxAge($ro, $lifeTime);
72
        $id = $this->getVaryUri($ro->uri);
73 14
        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...
74 14
            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...
75 14
                // render
76 1
                $ro->view = $ro->toString();
77
            }
78 1
79
            return $this->kvs->save($id, [$ro->uri, $ro->code, $ro->headers, $body, $ro->view], $lifeTime);
80
        }
81 1
        // "value" cache type
82
        return $this->kvs->save($id, [$ro->uri, $ro->code, $ro->headers, $body, null], $lifeTime);
83
    }
84 13
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function get(AbstractUri $uri)
89
    {
90 14
        $id = $this->getVaryUri($uri);
91
        $data = $this->kvs->fetch($id);
92 14
        if ($data === false) {
93 14
            return false;
94 13
        }
95
        $age = \time() - \strtotime($data[2]['Last-Modified']);
96
        $data[2]['Age'] = $age;
97 10
98
        return $data;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103 9
     */
104
    public function purge(AbstractUri $uri)
105 9
    {
106
        $id = $this->getVaryUri($uri);
107 9
        $this->deleteEtagDatabase($uri);
108
109
        return $this->kvs->delete($id);
110
    }
111
112
    /**
113
     * Delete etag in etag repository
114
     *
115 9
     * @param AbstractUri $uri
116
     */
117 9
    public function deleteEtagDatabase(AbstractUri $uri)
118 9
    {
119
        $etagId = self::ETAG_BY_URI . $this->getVaryUri($uri); // invalidate etag
120 9
121 9
        $oldEtagKey = $this->kvs->fetch($etagId);
122
123
        $this->kvs->delete($oldEtagKey);
124
    }
125
126 14
    private function evaluateBody($body)
127
    {
128 14
        if (! \is_array($body)) {
129 12
            return $body;
130
        }
131 12
        foreach ($body as &$item) {
132
            if ($item instanceof RequestInterface) {
133
                $item = ($item)();
134 2
            }
135
        }
136
137
        return $body;
138
    }
139
140
    /**
141
     * @return Cacheable|null
142 14
     */
143
    private function getCacheable(ResourceObject $ro)
144 14
    {
145 14
        /** @var Cacheable|null $cache */
146 14
        $cache = $this->reader->getClassAnnotation(new \ReflectionClass($ro), Cacheable::class);
147 14
148 14
        return $cache;
149 7
    }
150
151 14
    /**
152 14
     * Update etag in etag repository
153 14
     *
154 14
     * @param ResourceObject $ro
155
     */
156
    private function updateEtagDatabase(ResourceObject $ro)
157
    {
158
        $etag = $ro->headers['ETag'];
159
        $uri = (string) $ro->uri;
160
        $etagUri = self::ETAG_BY_URI . $uri;
161 15
        $oldEtag = $this->kvs->fetch($etagUri);
162
        if ($oldEtag) {
163 15
            $this->kvs->delete($oldEtag);
164 1
        }
165
        $etagId = HttpCache::ETAG_KEY . $etag;
166
        $this->kvs->save($etagId, $uri);     // save etag
167 14
        $this->kvs->save($etagUri, $etagId); // save uri  mapping etag
168
    }
169
170
    private function getExpiryTime(ResourceObject $ro, Cacheable $cacheable = null) : int
171
    {
172
        if ($cacheable === null) {
173
            return 0;
174
        }
175
176
        if ($cacheable->expiryAt) {
177
            return $this->getExpiryAtSec($ro, $cacheable);
178
        }
179
180
        return $cacheable->expirySecond ? $cacheable->expirySecond : $this->expiry[$cacheable->expiry];
181
    }
182
183
    private function getExpiryAtSec(ResourceObject $ro, Cacheable $cacheable) : int
184
    {
185
        if (! isset($ro->body[$cacheable->expiryAt])) {
186
            $msg = \sprintf('%s::%s', \get_class($ro), $cacheable->expiryAt);
187
            throw new ExpireAtKeyNotExists($msg);
188
        }
189
        $expiryAt = $ro->body[$cacheable->expiryAt];
190
        $sec = \strtotime($expiryAt) - \time();
191
192
        return $sec;
193
    }
194
195
    private function setMaxAge(ResourceObject $ro, int $age)
196
    {
197
        $setMaxAge = \sprintf('max-age=%d', $age);
198
        if (isset($ro->headers['Cache-Control'])) {
199
            $ro->headers['Cache-Control'] .= ', ' . $setMaxAge;
200
201
            return;
202
        }
203
        $ro->headers['Cache-Control'] = $setMaxAge;
204
    }
205
206
    private function getVaryUri(AbstractUri $uri) : string
207
    {
208
        if (! isset($_SERVER['X_VARY'])) {
209
            return (string) $uri;
210
        }
211
        $varys = \explode(',', $_SERVER['X_VARY']);
212
        $varyId = '';
213
        foreach ($varys as $vary) {
214
            $phpVaryKey = \sprintf('X_%s', \strtoupper($vary));
215
            if (isset($_SERVER[$phpVaryKey])) {
216
                $varyId .= $_SERVER[$phpVaryKey];
217
            }
218
        }
219
220
        return (string) $uri . $varyId;
221
    }
222
}
223