Completed
Pull Request — 1.x (#66)
by
unknown
02:19
created

ResourceStorage::getVaryUri()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.7333
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\RepositoryModule\Annotation\Storage;
8
use BEAR\Resource\AbstractUri;
9
use BEAR\Resource\RequestInterface;
10
use BEAR\Resource\ResourceObject;
11
use Doctrine\Common\Cache\CacheProvider;
12
13
final class ResourceStorage implements ResourceStorageInterface
14
{
15
    use VaryUriTrait;
16
17
    /**
18
     * Prefix for ETag URI
19
     */
20
    const ETAG_TABLE = 'etag-table-';
21
22
    /**
23
     * Prefix of ETag value
24
     */
25
    const ETAG_VAL = 'etag-val-';
26
27
    /**
28
     * @var CacheProvider
29
     */
30
    private $cache;
31
32
    /**
33 30
     * @Storage
34
     */
35 30
    public function __construct(CacheProvider $cache)
36 30
    {
37
        $this->cache = $cache;
38
    }
39
40
    /**
41 3
     * {@inheritdoc}
42
     */
43 3
    public function hasEtag(string $etag) : bool
44
    {
45
        return $this->cache->contains(self::ETAG_VAL . $etag);
46
    }
47
48
    /**
49 26
     * {@inheritdoc}
50
     */
51 26
    public function updateEtag(ResourceObject $ro, int $lifeTime)
52 26
    {
53 26
        $varyUri = $this->getVaryUri($ro->uri);
54
        $etag = self::ETAG_VAL . $ro->headers['ETag'];
55 26
        $uri = self::ETAG_TABLE . $varyUri;
56
        // delete old ETag
57 26
        $this->deleteEtag($ro->uri);
58
        // save ETag uri
59 26
        $this->cache->save($uri, $etag, $lifeTime);
60 26
        // save ETag value
61
        $this->cache->save($etag, $uri, $lifeTime);
62
    }
63
64
    /**
65 27
     * {@inheritdoc}
66
     */
67 27
    public function deleteEtag(AbstractUri $uri)
68 27
    {
69
        $varyUri = self::ETAG_TABLE . $this->getVaryUri($uri); // invalidate etag
70 27
        $oldEtagKey = $this->cache->fetch($varyUri);
71 27
72
        $this->cache->delete($oldEtagKey);
73
    }
74
75
    /**
76 25
     * {@inheritdoc}
77
     */
78 25
    public function get(AbstractUri $uri)
79
    {
80 25
        $varyUri = $this->getVaryUri($uri);
81
82
        return $this->cache->fetch($varyUri);
83
    }
84
85
    /**
86 12
     * {@inheritdoc}
87
     */
88 12
    public function delete(AbstractUri $uri) : bool
89 12
    {
90
        $this->deleteEtag($uri);
91 12
        $varyUri = $this->getVaryUri($uri);
92
93
        return $this->cache->delete($varyUri);
94
    }
95
96
    /**
97 24
     * {@inheritdoc}
98
     */
99 24
    public function saveValue(ResourceObject $ro, int $lifeTime)
100 24
    {
101 24
        $body = $this->evaluateBody($ro->body);
102
        $uri = $this->getVaryUri($ro->uri);
103 24
        $val = [$ro->uri, $ro->code, $ro->headers, $body, null];
104
105
        return $this->cache->save($uri, $val, $lifeTime);
106
    }
107
108
    /**
109 2
     * {@inheritdoc}
110
     */
111 2
    public function saveView(ResourceObject $ro, int $lifeTime)
112 2
    {
113 2
        $body = $this->evaluateBody($ro->body);
114
        $uri = $this->getVaryUri($ro->uri);
115 2
        $val = [$ro->uri, $ro->code, $ro->headers, $body, $ro->view];
116
117
        return $this->cache->save($uri, $val, $lifeTime);
118 26
    }
119
120 26
    private function evaluateBody($body)
121 8
    {
122
        if (! \is_array($body)) {
123 18
            return $body;
124 18
        }
125 2
        foreach ($body as &$item) {
126
            if ($item instanceof RequestInterface) {
127
                $item = ($item)();
128
            }
129 18
        }
130
131
        return $body;
132 27
    }
133
}
134