Completed
Push — 1.x ( 6aacf6...608ed3 )
by Akihito
01:28
created

QueryRepository::getCacheable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
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
        foreach ($ro->body as &$item) {
70 14
            if ($item instanceof RequestInterface) {
71
                $item = ($item)();
72
            }
73 14
        }
74 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...
75 14
            if (! $ro->view) {
76 1
                // render
77
                $ro->view = $ro->toString();
78 1
            }
79
80
            return $this->kvs->save((string) $ro->uri, [$ro->uri, $ro->code, $ro->headers, $ro->body, $ro->view], $lifeTime);
81 1
        }
82
        // "value" cache type
83
        return $this->kvs->save((string) $ro->uri, [$ro->uri, $ro->code, $ro->headers, $ro->body, null], $lifeTime);
84 13
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function get(AbstractUri $uri)
90 14
    {
91
        $data = $this->kvs->fetch((string) $uri);
92 14
        if ($data === false) {
93 14
            return false;
94 13
        }
95
96
        return $data;
97 10
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function purge(AbstractUri $uri)
103 9
    {
104
        $this->deleteEtagDatabase($uri);
105 9
106
        return $this->kvs->delete((string) $uri);
107 9
    }
108
109
    /**
110
     * Delete etag in etag repository
111
     *
112
     * @param AbstractUri $uri
113
     */
114
    public function deleteEtagDatabase(AbstractUri $uri)
115 9
    {
116
        $etagId = self::ETAG_BY_URI . (string) $uri; // invalidate etag
117 9
        $oldEtagKey = $this->kvs->fetch($etagId);
118 9
119
        $this->kvs->delete($oldEtagKey);
120 9
    }
121 9
122
    /**
123
     * @return Cacheable|null
124
     */
125
    private function getCacheable(ResourceObject $ro)
126 14
    {
127
        /** @var Cacheable|null $cache */
128 14
        $cache = $this->reader->getClassAnnotation(new \ReflectionClass($ro), Cacheable::class);
129 12
130
        return $cache;
131 12
    }
132
133
    /**
134 2
     * Update etag in etag repository
135
     *
136
     * @param ResourceObject $ro
137
     */
138
    private function updateEtagDatabase(ResourceObject $ro)
139
    {
140
        $etag = $ro->headers['ETag'];
141
        $uri = (string) $ro->uri;
142 14
        $etagUri = self::ETAG_BY_URI . $uri;
143
        $oldEtag = $this->kvs->fetch($etagUri);
144 14
        if ($oldEtag) {
145 14
            $this->kvs->delete($oldEtag);
146 14
        }
147 14
        $etagId = HttpCache::ETAG_KEY . $etag;
148 14
        $this->kvs->save($etagId, $uri);     // save etag
149 7
        $this->kvs->save($etagUri, $etagId); // save uri  mapping etag
150
    }
151 14
152 14
    /**
153 14
     * @param Cacheable $cacheable
154 14
     *
155
     * @return int
156
     */
157
    private function getExpiryTime(Cacheable $cacheable = null)
158
    {
159
        if ($cacheable === null) {
160
            return 0;
161 15
        }
162
163 15
        return $cacheable->expirySecond ? $cacheable->expirySecond : $this->expiry[$cacheable->expiry];
164 1
    }
165
}
166