Completed
Push — 1.x ( c4696d...9aaaa1 )
by Akihito
01:34
created

QueryRepository::purge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 1
cts 1
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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\RepositoryModule\Annotation\Cacheable;
10
use BEAR\RepositoryModule\Annotation\Storage;
11
use BEAR\Resource\AbstractUri;
12
use BEAR\Resource\RequestInterface;
13
use BEAR\Resource\ResourceObject;
14
use Doctrine\Common\Annotations\Reader;
15
use Doctrine\Common\Cache\Cache;
16
17
class QueryRepository implements QueryRepositoryInterface
18
{
19
    const ETAG_BY_URI = 'etag-by-uri';
20
21
    /**
22
     * @var Cache
23
     */
24
    private $kvs;
25
26
    /**
27
     * @var Reader
28
     */
29
    private $reader;
30
31
    /**
32
     * @var Expiry
33
     */
34
    private $expiry;
35
36
    /**
37
     * @var EtagSetterInterface
38
     */
39
    private $setEtag;
40
41
    /**
42
     * @Storage("kvs")
43
     */
44
    public function __construct(
45
        EtagSetterInterface $setEtag,
46
        Cache $kvs,
47
        Reader $reader,
48
        Expiry $expiry
49
    ) {
50
        $this->setEtag = $setEtag;
51 16
        $this->reader = $reader;
52
        $this->kvs = $kvs;
53
        $this->expiry = $expiry;
54
    }
55
56
    /**
57 16
     * {@inheritdoc}
58 16
     */
59 16
    public function put(ResourceObject $ro)
60 16
    {
61 16
        $ro->toString();
62
        ($this->setEtag)($ro);
63
        if (isset($ro->headers['ETag'])) {
64
            $this->updateEtagDatabase($ro);
65
        }
66 14
        /* @var $cacheable Cacheable */
67
        $cacheable = $this->getCacheable($ro);
68 14
        $lifeTime = $this->getExpiryTime($cacheable);
69 14
        $this->evaluate($ro);
70 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...
71
            if (! $ro->view) {
72
                // render
73 14
                $ro->view = $ro->toString();
74 14
            }
75 14
76 1
            return $this->kvs->save((string) $ro->uri, [$ro->uri, $ro->code, $ro->headers, $ro->body, $ro->view], $lifeTime);
77
        }
78 1
        // "value" cache type
79
        return $this->kvs->save((string) $ro->uri, [$ro->uri, $ro->code, $ro->headers, $ro->body, null], $lifeTime);
80
    }
81 1
82
    /**
83
     * {@inheritdoc}
84 13
     */
85
    public function get(AbstractUri $uri)
86
    {
87
        $data = $this->kvs->fetch((string) $uri);
88
        if ($data === false) {
89
            return false;
90 14
        }
91
92 14
        return $data;
93 14
    }
94 13
95
    /**
96
     * {@inheritdoc}
97 10
     */
98
    public function purge(AbstractUri $uri)
99
    {
100
        $this->deleteEtagDatabase($uri);
101
102
        return $this->kvs->delete((string) $uri);
103 9
    }
104
105 9
    /**
106
     * Delete etag in etag repository
107 9
     *
108
     * @param AbstractUri $uri
109
     */
110
    public function deleteEtagDatabase(AbstractUri $uri)
111
    {
112
        $etagId = self::ETAG_BY_URI . (string) $uri; // invalidate etag
113
        $oldEtagKey = $this->kvs->fetch($etagId);
114
115 9
        $this->kvs->delete($oldEtagKey);
116
    }
117 9
118 9
    private function evaluate(ResourceObject $ro)
119
    {
120 9
        if (! \is_array($ro->body)) {
121 9
            return;
122
        }
123
        foreach ($ro->body as &$item) {
124
            if ($item instanceof RequestInterface) {
125
                $item = ($item)();
126 14
            }
127
        }
128 14
    }
129 12
130
    /**
131 12
     * @return Cacheable|null
132
     */
133
    private function getCacheable(ResourceObject $ro)
134 2
    {
135
        /** @var Cacheable|null $cache */
136
        $cache = $this->reader->getClassAnnotation(new \ReflectionClass($ro), Cacheable::class);
137
138
        return $cache;
139
    }
140
141
    /**
142 14
     * Update etag in etag repository
143
     *
144 14
     * @param ResourceObject $ro
145 14
     */
146 14
    private function updateEtagDatabase(ResourceObject $ro)
147 14
    {
148 14
        $etag = $ro->headers['ETag'];
149 7
        $uri = (string) $ro->uri;
150
        $etagUri = self::ETAG_BY_URI . $uri;
151 14
        $oldEtag = $this->kvs->fetch($etagUri);
152 14
        if ($oldEtag) {
153 14
            $this->kvs->delete($oldEtag);
154 14
        }
155
        $etagId = HttpCache::ETAG_KEY . $etag;
156
        $this->kvs->save($etagId, $uri);     // save etag
157
        $this->kvs->save($etagUri, $etagId); // save uri  mapping etag
158
    }
159
160
    /**
161 15
     * @param Cacheable $cacheable
162
     *
163 15
     * @return int
164 1
     */
165
    private function getExpiryTime(Cacheable $cacheable = null)
166
    {
167 14
        if ($cacheable === null) {
168
            return 0;
169
        }
170
171
        return $cacheable->expirySecond ? $cacheable->expirySecond : $this->expiry[$cacheable->expiry];
172
    }
173
}
174