Completed
Pull Request — 1.x (#55)
by Akihito
02:57
created

ResourceStorage::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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