Passed
Push — master ( 380356...2e1f1e )
by Petr
10:45
created

StaticCache   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 83
ccs 22
cts 22
cp 1
rs 10
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setCache() 0 3 1
A getCache() 0 3 1
A init() 0 4 1
A clear() 0 4 1
A exists() 0 4 1
A get() 0 4 1
A set() 0 4 1
A checkCache() 0 4 2
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
    }
28
29
    /**
30
     * @return Formatted|ICache|null
31
     */
32 2
    public static function getCache()
33
    {
34 2
        return static::$cache;
35
    }
36
37
    /**
38
     * Init cache
39
     * @param string $what
40
     * @throws CacheException
41
     */
42 2
    public static function init(string $what): void
43
    {
44 2
        static::checkCache();
45 1
        static::$cache->/** @scrutinizer ignore-call */init($what);
46
    }
47
48
    /**
49
     * Is cache set?
50
     * @return boolean
51
     * @throws CacheException
52
     */
53 1
    public static function exists(): bool
54
    {
55 1
        static::checkCache();
56 1
        return static::$cache->/** @scrutinizer ignore-call */exists();
57
    }
58
59
    /**
60
     * Set data into cache
61
     * @param mixed $content
62
     * @throws CacheException
63
     * @return boolean
64
     */
65 1
    public static function set($content): bool
66
    {
67 1
        static::checkCache();
68 1
        return static::$cache->/** @scrutinizer ignore-call */set($content);
69
    }
70
71
    /**
72
     * Return cache content
73
     * @throws CacheException
74
     * @return mixed
75
     */
76 1
    public static function get()
77
    {
78 1
        static::checkCache();
79 1
        return static::$cache->/** @scrutinizer ignore-call */get();
80
    }
81
82
    /**
83
     * Delete data in cache
84
     * @throws CacheException
85
     */
86 1
    public static function clear(): void
87
    {
88 1
        static::checkCache();
89 1
        static::$cache->/** @scrutinizer ignore-call */clear();
90
    }
91
92
    /**
93
     * @throws CacheException
94
     */
95 2
    protected static function checkCache(): void
96
    {
97 2
        if (empty(static::$cache)) {
98 1
            throw new CacheException('Cache not initialized');
99
        }
100
    }
101
}
102