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

Cache   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 40
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A flush() 0 6 2
A getCache() 0 3 1
A setCacheValues() 0 7 1
A cleanCacheName() 0 3 1
A getCacheValues() 0 7 3
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