Cache::getCacheValues()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Sunnysideup\SiteWideSearch\Helpers;
4
5
use Psr\SimpleCache\CacheInterface;
6
use SilverStripe\Core\Flushable;
7
use SilverStripe\Core\Injector\Injector;
8
9
class Cache implements Flushable
10
{
11
    /**
12
     * Flush all MemberCacheFlusher services.
13
     */
14
    public static function flush()
15
    {
16
        $obj = Injector::inst()->get(Cache::class);
17
        $cache = $obj->getCache();
18
        if ($cache) {
19
            $cache->clear();
20
        }
21
    }
22
23
    public function getCache()
24
    {
25
        return Injector::inst()->get(CacheInterface::class . '.siteWideSearch');
26
    }
27
28
    public function getCacheValues($cacheName): array
29
    {
30
        $cacheName = $this->cleanCacheName($cacheName);
31
        $cache = $this->getCache();
32
        if ($cache->has($cacheName)) {
33
            unserialize($cache->get($cacheName));
34
        }
35
36
        return $cache->has($cacheName) ? unserialize($cache->get($cacheName)) : [];
37
    }
38
39
    public function setCacheValues($cacheName, array $array): self
40
    {
41
        $cacheName = $this->cleanCacheName($cacheName);
42
        $cache = $this->getCache();
43
        $cache->set($cacheName, serialize($array));
44
45
        return $this;
46
    }
47
48
    public function cleanCacheName(string $string)
49
    {
50
        return crc32($string);
51
    }
52
}
53