Completed
Push — 1.x ( 2a62f4...e7c13e )
by Akihito
9s
created

QueryRepository::purge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
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\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
            $annotations = unserialize($ro->classAnnotations);
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 11
            return $annotations[Cacheable::class];
127
        }
128
129 2
        return $this->reader->getClassAnnotation(new \ReflectionClass($ro), Cacheable::class);
130
    }
131
132
    /**
133
     * Update etag in etag repository
134
     *
135
     * @param ResourceObject $ro
136
     */
137 13
    private function updateEtagDatabase(ResourceObject $ro)
138
    {
139 13
        $etag = $ro->headers['ETag'];
140 13
        $uri = (string) $ro->uri;
141 13
        $etagUri = self::ETAG_BY_URI . $uri;
142 13
        $oldEtag = $this->kvs->fetch($etagUri);
143 13
        if ($oldEtag) {
144 7
            $this->kvs->delete($oldEtag);
145
        }
146 13
        $etagId = HttpCache::ETAG_KEY . $etag;
147 13
        $this->kvs->save($etagId, $uri);     // save etag
148 13
        $this->kvs->save($etagUri, $etagId); // save uri  mapping etag
149 13
    }
150
151
    /**
152
     * @param Cacheable $cacheable
153
     *
154
     * @return int
155
     */
156 14
    private function getExpiryTime(Cacheable $cacheable = null)
157
    {
158 14
        if ($cacheable === null) {
159 1
            return 0;
160
        }
161
162 13
        return $cacheable->expirySecond ? $cacheable->expirySecond : $this->expiry[$cacheable->expiry];
163
    }
164
}
165