StaticCache::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace kalanis\kw_cache;
4
5
6
use kalanis\kw_cache\Cache\Formatted;
7
use kalanis\kw_cache\Interfaces\ICache;
8
9
10
/**
11
 * Class StaticCache
12
 * @package kalanis\kw_cache
13
 * Static face for caching values in selected storage
14
 * Cannot implement interface due passing mixed content there and back
15
 */
16
class StaticCache
17
{
18
    /** @var ICache|Formatted|null */
19
    protected static $cache = null;
20
21
    /**
22
     * @param Formatted|ICache|null $cache
23
     */
24 2
    public static function setCache($cache = null): void
25
    {
26 2
        static::$cache = $cache;
27 2
    }
28
29
    /**
30
     * @throws CacheException
31
     * @return Formatted|ICache
32
     */
33 2
    public static function getCache()
34
    {
35 2
        if (empty(static::$cache)) {
36 1
            throw new CacheException('Cache not initialized');
37
        }
38 1
        return static::$cache;
39
    }
40
41
    /**
42
     * Init cache
43
     * @param string[] $what
44
     * @throws CacheException
45
     */
46 1
    public static function init(array $what): void
47
    {
48 1
        static::getCache()->init($what);
49 1
    }
50
51
    /**
52
     * Is cache set?
53
     * @throws CacheException
54
     * @return boolean
55
     */
56 1
    public static function exists(): bool
57
    {
58 1
        return static::getCache()->exists();
59
    }
60
61
    /**
62
     * Set data into cache
63
     * @param mixed $content
64
     * @throws CacheException
65
     * @return boolean
66
     */
67 1
    public static function set($content): bool
68
    {
69 1
        return static::getCache()->set($content);
70
    }
71
72
    /**
73
     * Return cache content
74
     * @throws CacheException
75
     * @return mixed
76
     */
77 1
    public static function get()
78
    {
79 1
        return static::getCache()->get();
80
    }
81
82
    /**
83
     * Delete data in cache
84
     * @throws CacheException
85
     */
86 1
    public static function clear(): void
87
    {
88 1
        static::getCache()->clear();
89 1
    }
90
}
91