Completed
Push — 1.x ( de4d77...cabfbb )
by Akihito
03:15
created

QueryRepository::put()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 12
cts 12
cp 1
rs 9.7
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository;
6
7
use BEAR\QueryRepository\Exception\ExpireAtKeyNotExists;
8
use BEAR\RepositoryModule\Annotation\Cacheable;
9
use BEAR\RepositoryModule\Annotation\HttpCache;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, BEAR\QueryRepository\HttpCache.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use BEAR\Resource\AbstractUri;
11
use BEAR\Resource\RequestInterface;
12
use BEAR\Resource\ResourceObject;
13
use Doctrine\Common\Annotations\Reader;
14
15
final class QueryRepository implements QueryRepositoryInterface
16
{
17
    /**
18
     * @var ResourceStorageInterface
19
     */
20
    private $storage;
21
22
    /**
23
     * @var Reader
24
     */
25
    private $reader;
26
27
    /**
28
     * @var Expiry
29
     */
30
    private $expiry;
31
32
    /**
33
     * @var EtagSetterInterface
34
     */
35
    private $setEtag;
36
37 28
    public function __construct(
38
        EtagSetterInterface $setEtag,
39
        ResourceStorageInterface $storage,
40
        Reader $reader,
41
        Expiry $expiry
42
    ) {
43 28
        $this->setEtag = $setEtag;
44 28
        $this->reader = $reader;
45 28
        $this->storage = $storage;
46 28
        $this->expiry = $expiry;
47 28
    }
48
49
    /**
50
     * {@inheritdoc}
51
     *
52
     * @throws \ReflectionException
53
     */
54 26
    public function put(ResourceObject $ro)
55
    {
56 26
        $ro->toString();
57 26
        $httpCache = $this->getHttpCacheAnnotation($ro);
58 26
        $cacheable = $this->getCacheableAnnotation($ro);
59 26
        ($this->setEtag)($ro, null, $httpCache);
60 26
        $lifeTime = $this->getExpiryTime($ro, $cacheable);
61 25
        if (isset($ro->headers['ETag'])) {
62 25
            $this->storage->updateEtag($ro, $lifeTime);
63
        }
64 25
        $this->setMaxAge($ro, $lifeTime);
65 25
        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...
66 2
            return $this->saveViewCache($ro, $lifeTime);
67
        }
68
69 23
        return $this->storage->saveValue($ro, $lifeTime);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 24
    public function get(AbstractUri $uri)
76
    {
77 24
        $data = $this->storage->get($uri);
78 24
        if ($data === false) {
79 22
            return false;
80
        }
81 14
        $age = \time() - \strtotime($data[2]['Last-Modified']);
82 14
        $data[2]['Age'] = $age;
83
84 14
        return $data;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 12
    public function purge(AbstractUri $uri)
91
    {
92 12
        $this->storage->deleteEtag($uri);
93
94 12
        return $this->storage->delete($uri);
95
    }
96
97
    /**
98
     * @throws \ReflectionException
99
     */
100 26
    private function getHttpCacheAnnotation(ResourceObject $ro)
101
    {
102 26
        $annotation = $this->reader->getClassAnnotation(new \ReflectionClass($ro), HttpCache::class);
103 26
        if ($annotation instanceof HttpCache || $annotation === null) {
0 ignored issues
show
Bug introduced by
The class BEAR\RepositoryModule\Annotation\HttpCache 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...
104 26
            return $annotation;
105
        }
106
107
        throw new \LogicException();
108
    }
109
110
    /**
111
     * @throws \ReflectionException
112
     *
113
     * @return ?Cacheable
0 ignored issues
show
Documentation introduced by
The doc-type ?Cacheable could not be parsed: Unknown type name "?Cacheable" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
114
     */
115 26
    private function getCacheableAnnotation(ResourceObject $ro)
116
    {
117 26
        $annotation = $this->reader->getClassAnnotation(new \ReflectionClass($ro), Cacheable::class);
118 26
        if ($annotation instanceof Cacheable || $annotation === null) {
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...
119 26
            return $annotation;
120
        }
121
122
        throw new \LogicException();
123
    }
124
125
    private function evaluateBody($body)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
126
    {
127
        if (! \is_array($body)) {
128
            return $body;
129
        }
130
        foreach ($body as &$item) {
131
            if ($item instanceof RequestInterface) {
132
                $item = ($item)();
133
            }
134
        }
135
136
        return $body;
137
    }
138
139 27
    private function getExpiryTime(ResourceObject $ro, Cacheable $cacheable = null) : int
140
    {
141 27
        if ($cacheable === null) {
142 4
            return 0;
143
        }
144
145 23
        if ($cacheable->expiryAt) {
146 2
            return $this->getExpiryAtSec($ro, $cacheable);
147
        }
148
149 21
        return $cacheable->expirySecond ? $cacheable->expirySecond : $this->expiry[$cacheable->expiry];
150
    }
151
152 2
    private function getExpiryAtSec(ResourceObject $ro, Cacheable $cacheable) : int
153
    {
154 2
        if (! isset($ro->body[$cacheable->expiryAt])) {
155 1
            $msg = \sprintf('%s::%s', \get_class($ro), $cacheable->expiryAt);
156
157 1
            throw new ExpireAtKeyNotExists($msg);
158
        }
159 1
        $expiryAt = $ro->body[$cacheable->expiryAt];
160
161 1
        return \strtotime($expiryAt) - \time();
162
    }
163
164 25
    private function setMaxAge(ResourceObject $ro, int $age)
165
    {
166 25
        if ($age === 0) {
167 21
            return;
168
        }
169 4
        $setMaxAge = \sprintf('max-age=%d', $age);
170 4
        $noCacheControleHeader = ! isset($ro->headers['Cache-Control']);
171 4
        if ($noCacheControleHeader) {
172 1
            $ro->headers['Cache-Control'] = $setMaxAge;
173
174 1
            return;
175
        }
176 3
        $isMaxAgeAlreadyDefined = strpos($ro->headers['Cache-Control'], 'max-age') !== false;
177 3
        if ($isMaxAgeAlreadyDefined) {
178 1
            return;
179
        }
180 2
        $ro->headers['Cache-Control'] .= ', ' . $setMaxAge;
181 2
    }
182
183 2
    private function saveViewCache(ResourceObject $ro, int $lifeTime)
184
    {
185 2
        if (! $ro->view) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ro->view of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
186
            $ro->view = $ro->toString();
187
        }
188
189 2
        return $this->storage->saveView($ro, $lifeTime);
190
    }
191
}
192