Passed
Push — master ( 68aff5...cd8fb9 )
by Nicolaas
03:34
created

Cache::cleanCacheName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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
        $array = $cache->has($cacheName) ? unserialize($cache->get($cacheName)) : [];
0 ignored issues
show
Unused Code introduced by
The assignment to $array is dead and can be removed.
Loading history...
33
34
        return $cache->has($cacheName) ? unserialize($cache->get($cacheName)) : [];
35
    }
36
37
    public function setCacheValues($cacheName, array $array): self
38
    {
39
        $cacheName = $this->cleanCacheName($cacheName);
40
        $cache = $this->getCache();
41
        $cache->set($cacheName, serialize($array));
42
43
        return $this;
44
    }
45
46
    public function cleanCacheName(string $string)
47
    {
48
        return crc32($string);
49
    }
50
}
51