Passed
Pull Request — master (#1)
by Peter
07:07
created

CacheManager::getCacheData()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
nc 7
nop 1
dl 0
loc 29
rs 8.8333
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Template;
6
7
use Opulence\Cache\ICacheBridge;
8
9
class CacheManager
10
{
11
    public const CACHE_KEY_TEMPLATES = 'templates_%s';
12
    public const CACHE_KEY_DOCUMENT  = 'document_%s';
13
14
    /** @var ICacheBridge */
15
    protected $cacheBridge;
16
17
    /**
18
     * Cache constructor.
19
     *
20
     * @param ICacheBridge $cacheBridge
21
     */
22
    public function __construct(ICacheBridge $cacheBridge)
23
    {
24
        $this->cacheBridge = $cacheBridge;
25
    }
26
27
    /**
28
     * @param string $cacheId
29
     *
30
     * @return CacheData|null
31
     */
32
    public function getCacheData(string $cacheId): ?CacheData
33
    {
34
        $key = $this->getCacheKey($cacheId);
35
        try {
36
            $payload = $this->cacheBridge->get($key);
37
        } catch (\Exception $e) {
38
            return null;
39
        }
40
41
        if (empty($payload) || !is_string($payload)) {
42
            return null;
43
        }
44
45
        $data = json_decode($payload, true);
46
        if (!is_array($data)) {
47
            return null;
48
        }
49
50
        $cacheData = new CacheData();
51
52
        if (array_key_exists(CacheData::PAYLOAD_KEY_DATE, $data)) {
53
            $cacheData->setDate($data[CacheData::PAYLOAD_KEY_DATE]);
54
        }
55
56
        if (array_key_exists(CacheData::PAYLOAD_KEY_SUBTEMPLATES, $data)) {
57
            $cacheData->setSubTemplates($data[CacheData::PAYLOAD_KEY_SUBTEMPLATES]);
58
        }
59
60
        return $cacheData;
61
    }
62
63
    /**
64
     * @param string $cacheId
65
     * @param array  $blocks
66
     *
67
     * @return bool
68
     */
69
    public function storeCacheData(string $cacheId, array $blocks): bool
70
    {
71
        $cacheData = (new CacheData())->setSubTemplates($blocks);
72
73
        $payload = json_encode(
74
            [
75
                CacheData::PAYLOAD_KEY_DATE         => $cacheData->getDate(),
76
                CacheData::PAYLOAD_KEY_SUBTEMPLATES => $cacheData->getSubTemplates(),
77
            ]
78
        );
79
80
        $key = $this->getCacheKey($cacheId);
81
82
        $this->cacheBridge->set($key, $payload, PHP_INT_MAX);
83
84
        return $this->cacheBridge->has($key);
85
    }
86
87
    /**
88
     * @param string $cacheId
89
     *
90
     * @return string
91
     */
92
    public function getDocument(string $cacheId): string
93
    {
94
        $key = $this->getDocumentCacheKey($cacheId);
95
96
        try {
97
            return (string)$this->cacheBridge->get($key);
98
        } catch (\Exception $e) {
99
            return '';
100
        }
101
    }
102
103
    /**
104
     * @param string $cacheId
105
     * @param string $payload
106
     *
107
     * @return bool
108
     */
109
    public function storeDocument(string $cacheId, string $payload): bool
110
    {
111
        $key = $this->getDocumentCacheKey($cacheId);
112
113
        $this->cacheBridge->set($key, $payload, PHP_INT_MAX);
114
115
        return $this->cacheBridge->has($key);
116
    }
117
118
    public function flush()
119
    {
120
        $this->cacheBridge->flush();
121
    }
122
123
    /**
124
     * @param string $cacheId
125
     *
126
     * @return string
127
     */
128
    private function getCacheKey(string $cacheId): string
129
    {
130
        return sprintf(static::CACHE_KEY_TEMPLATES, $cacheId);
131
    }
132
133
    /**
134
     * @param string $cacheId
135
     *
136
     * @return string
137
     */
138
    private function getDocumentCacheKey(string $cacheId): string
139
    {
140
        return sprintf(static::CACHE_KEY_DOCUMENT, $cacheId);
141
    }
142
}
143