Passed
Push — develop ( b81587...a0fec2 )
by Mathieu
01:42
created

Cache::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Suricate;
3
4
/**
5
 * Cache
6
 *
7
 * @package Suricate
8
 * @author  Mathieu LESNIAK <[email protected]>
9
 *
10
 * @property string $type
11
 */
12
13
class Cache extends Service implements Interfaces\ICache
14
{
15
    protected $parametersList = ['type'];
16
    public static $container;
17
    protected $cacheTypes = [
18
        'memcache'  => 'Suricate\Suricate::CacheMemcache',
19
        'memcached' => 'Suricate\Suricate::CacheMemcached',
20
        'apc'       => 'Suricate\Suricate::CacheApc',
21
        'file'      => 'Suricate\Suricate::CacheFile',
22
    ];
23
24
    protected function init()
25
    {
26
        if (static::$container === null) {
27
            if (isset($this->cacheTypes[$this->type])) {
28
                static::$container = $this->cacheTypes[$this->type](true);
29
                return;
30
            }
31
            throw new \Exception("Unknown cache type " . $this->type);
32
        }
33
    }
34
35
    public function getInstance()
36
    {
37
        $this->init();
38
        return static::$container;
39
    }
40
41
    /**
42
     * Setter
43
     * Put a variable in cache
44
     * @param string $variable The key that will be associated with the item.
45
     * @param mixed $value    The variable to store.
46
     * @param int $expiry   Expiration time of the item. If it's equal to zero, the item will never expire.
47
     *                      You can also use Unix timestamp or a number of seconds starting from current time,
48
     *                      but in the latter case the number of seconds may not exceed 2592000 (30 days).
49
     */
50
    public function set(string $variable, $value, $expiry = null)
51
    {
52
        $this->init();
53
        return static::$container->set($variable, $value, $expiry);
54
    }
55
56
    /**
57
     * Get a variable from cache
58
     * @param  string $variable The key to fetch
59
     * @return mixed           Data fetched from cache, false if not found
60
     */
61
    public function get(string $variable)
62
    {
63
        $this->init();
64
        return static::$container->get($variable);
65
    }
66
67
    public function delete(string $variable)
68
    {
69
        $this->init();
70
        return static::$container->delete($variable);
71
    }
72
}
73