Completed
Pull Request — 1.x (#37)
by Akihito
03:17
created

QueryRepository::put()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 8.8571
cc 5
eloc 11
nc 6
nop 1
crap 5
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\ExpiryConfig;
11
use BEAR\RepositoryModule\Annotation\Storage;
12
use BEAR\Resource\AbstractUri;
13
use BEAR\Resource\ResourceObject;
14
use Doctrine\Common\Annotations\Reader;
15
use Doctrine\Common\Cache\Cache;
16
use Doctrine\Common\Cache\CacheProvider;
17
18
class QueryRepository implements QueryRepositoryInterface
19
{
20
    const ETAG_BY_URI = 'etag-by-uri';
21
22
    /**
23
     * @var CacheProvider
24
     */
25
    private $kvs;
26
27
    /**
28
     * @var Reader
29
     */
30
    private $reader;
31
32
    /**
33
     * @var array
34
     */
35
    private $expiry;
36
37
    /**
38
     * @var EtagSetterInterface
39
     */
40
    private $setEtag;
41
42
    /**
43
     * @param EtagSetterInterface $setEtag
44
     * @param CacheProvider       $kvs
45
     * @param Reader              $reader
46
     * @param string              $expiry
47
     *
48
     * @Storage("kvs")
49
     * @ExpiryConfig("expiry")
50
     */
51 15
    public function __construct(
52
        EtagSetterInterface $setEtag,
53
        Cache $kvs,
54
        Reader $reader,
55
        $expiry
56
    ) {
57 15
        $this->setEtag = $setEtag;
58 15
        $this->reader = $reader;
59 15
        $this->kvs = $kvs;
0 ignored issues
show
Documentation Bug introduced by
$kvs is of type object<Doctrine\Common\Cache\Cache>, but the property $kvs was declared to be of type object<Doctrine\Common\Cache\CacheProvider>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

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