Completed
Pull Request — 1.x (#35)
by Akihito
01:59
created

QueryRepository   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 16
c 6
b 0
f 0
lcom 1
cbo 4
dl 0
loc 147
ccs 48
cts 48
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A put() 0 18 4
A get() 0 9 2
A purge() 0 6 1
A deleteEtagDatabase() 0 7 1
A getCacheable() 0 8 2
A updateEtagDatabase() 0 13 2
A getExpiryTime() 0 8 3
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\Resource\AbstractUri;
11
use BEAR\Resource\ResourceObject;
12
use Doctrine\Common\Annotations\Reader;
13
use Doctrine\Common\Cache\CacheProvider;
14
use Ray\Di\Di\Named;
15
16
class QueryRepository implements QueryRepositoryInterface
17
{
18
    const ETAG_BY_URI = 'etag-by-uri';
19
20
    /**
21
     * @var CacheProvider
22
     */
23
    private $kvs;
24
25
    /**
26
     * @var Reader
27
     */
28
    private $reader;
29
30
    /**
31
     * @var array
32
     */
33
    private $expiry;
34
35
    /**
36
     * @var EtagSetterInterface
37
     */
38
    private $setEtag;
39
40
    /**
41
     * @param EtagSetterInterface $setEtag
42
     * @param CacheProvider       $kvs
43
     * @param Reader              $reader
44
     * @param string              $expiry
45
     *
46
     * @Named("kvs=BEAR\RepositoryModule\Annotation\Storage, expiry=BEAR\RepositoryModule\Annotation\ExpiryConfig")
47
     */
48 15
    public function __construct(
49
        EtagSetterInterface $setEtag,
50
        CacheProvider $kvs,
51
        Reader $reader,
52
        $expiry
53
    ) {
54 15
        $this->setEtag = $setEtag;
55 15
        $this->reader = $reader;
56 15
        $this->kvs = $kvs;
57 15
        $this->expiry = $expiry;
0 ignored issues
show
Documentation Bug introduced by
It seems like $expiry of type string is incompatible with the declared type array of property $expiry.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58 15
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 13
    public function put(ResourceObject $ro)
64
    {
65 13
        $this->setEtag->__invoke($ro);
66 13
        if (isset($ro->headers['ETag'])) {
67 13
            $this->updateEtagDatabase($ro);
68
        }
69
        /* @var $cacheable Cacheable */
70 13
        $cacheable = $this->getCacheable($ro);
71 13
        $lifeTime = $this->getExpiryTime($cacheable);
72 13
        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...
73
            // render
74 1
            $ro->view = $ro->toString();
75
76 1
            return $this->kvs->save((string) $ro->uri, [$ro->code, $ro->headers, $ro->body, $ro->view], $lifeTime);
77
        }
78
        // "value" cache type
79 12
        return $this->kvs->save((string) $ro->uri, [$ro->code, $ro->headers, $ro->body, null], $lifeTime);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 13
    public function get(AbstractUri $uri)
86
    {
87 13
        $data = $this->kvs->fetch((string) $uri);
88 13
        if ($data === false) {
89 12
            return false;
90
        }
91
92 9
        return $data;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 8
    public function purge(AbstractUri $uri)
99
    {
100 8
        $this->deleteEtagDatabase($uri);
101
102 8
        return $this->kvs->delete((string) $uri);
103
    }
104
105
    /**
106
     * Delete etag in etag repository
107
     *
108
     * @param AbstractUri $uri
109
     */
110 8
    public function deleteEtagDatabase(AbstractUri $uri)
111
    {
112 8
        $etagId = self::ETAG_BY_URI . (string) $uri; // invalidate etag
113 8
        $oldEtagKey = $this->kvs->fetch($etagId);
114
115 8
        $this->kvs->delete($oldEtagKey);
116 8
    }
117
118
    /**
119
     * @return Cacheable
120
     */
121 13
    private function getCacheable(ResourceObject $ro)
122
    {
123 13
        if (isset($ro->classAnnotations)) {
124 11
            return (unserialize($ro->classAnnotations))[Cacheable::class];
0 ignored issues
show
Bug introduced by
The property classAnnotations does not seem to exist in BEAR\Resource\ResourceObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
125
        }
126
127 2
        return $this->reader->getClassAnnotation(new \ReflectionClass($ro), Cacheable::class);
128
    }
129
130
    /**
131
     * Update etag in etag repository
132
     *
133
     * @param ResourceObject $ro
134
     */
135 13
    private function updateEtagDatabase(ResourceObject $ro)
136
    {
137 13
        $etag = $ro->headers['ETag'];
138 13
        $uri = (string) $ro->uri;
139 13
        $etagUri = self::ETAG_BY_URI . $uri;
140 13
        $oldEtag = $this->kvs->fetch($etagUri);
141 13
        if ($oldEtag) {
142 7
            $this->kvs->delete($oldEtag);
143
        }
144 13
        $etagId = HttpCache::ETAG_KEY . $etag;
145 13
        $this->kvs->save($etagId, $uri);     // save etag
146 13
        $this->kvs->save($etagUri, $etagId); // save uri  mapping etag
147 13
    }
148
149
    /**
150
     * @param Cacheable $cacheable
151
     *
152
     * @return int
153
     */
154 14
    private function getExpiryTime(Cacheable $cacheable = null)
155
    {
156 14
        if ($cacheable === null) {
157 1
            return 0;
158
        }
159
160 13
        return $cacheable->expirySecond ? $cacheable->expirySecond : $this->expiry[$cacheable->expiry];
161
    }
162
}
163