Cacher::getCache()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Sunnysideup\AssetsOverview\Traits;
4
5
use Psr\SimpleCache\CacheInterface;
6
use SilverStripe\Core\Injector\Injector;
7
8
trait Cacher
9
{
10
    private static $loadedFromCache = true;
11
12
    private static $cacheCache = null;
13
14
    /**
15
     * return false if the cache has been set or a cache key was not found.
16
     */
17
    public static function loadedFromCache(): bool
18
    {
19
        return self::$loadedFromCache;
20
    }
21
22
    public static function flushCache()
23
    {
24
        $cache = self::getCache();
25
        $cache->clear();
26
    }
27
28
    /**
29
     * @return CacheInterface
30
     */
31
    protected static function getCache()
32
    {
33
        if (null === self::$cacheCache) {
34
            self::$cacheCache = Injector::inst()->get(CacheInterface::class . '.assetsoverviewCache');
35
        }
36
37
        return self::$cacheCache;
38
    }
39
40
    /**
41
     * @param mixed $value
42
     */
43
    protected function setCacheValue(string $cacheKey, $value)
44
    {
45
        self::$loadedFromCache = false;
46
        $cache = self::getCache();
47
48
        $cache->set($cacheKey, serialize($value));
49
    }
50
51
    /**
52
     * @return mixed
53
     */
54
    protected function getCacheValue(string $cacheKey)
55
    {
56
        $cache = self::getCache();
57
58
        return unserialize((string) $cache->get($cacheKey));
59
    }
60
61
    protected function hasCacheKey(string $cacheKey): bool
62
    {
63
        $cache = self::getCache();
64
65
        return $cache->has($cacheKey);
66
    }
67
}
68