Cache   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
eloc 22
c 5
b 0
f 1
dl 0
loc 71
ccs 0
cts 19
cp 0
rs 10
wmc 7

5 Methods

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