Completed
Push — master ( 037ec5...5f25ab )
by Mathieu
01:33
created

Cache::init()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 0
dl 0
loc 22
rs 8.9457
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 = array('type');
16
    public static $container;
17
18
    protected function init()
19
    {
20
        if (static::$container === null) {
21
            switch ($this->type) {
22
                case 'memcache':
23
                    static::$container = Suricate::CacheMemcache(true);
24
                    break;
25
                case 'memcached':
26
                    static::$container = Suricate::CacheMemcached(true);
27
                    break;
28
                case 'apc':
29
                    static::$container = Suricate::CacheApc(true);
30
                    break;
31
                case 'file':
32
                    static::$container = Suricate::CacheFile(true);
33
                    break;
34
                default:
35
                    throw new \Exception("Unknown cache type " . $this->type);
36
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
37
            }
38
        }
39
    }
40
41
    public function getInstance()
42
    {
43
        $this->init();
44
        return static::$container;
45
    }
46
47
    /**
48
     * Setter
49
     * Put a variable in cache
50
     * @param string $variable The key that will be associated with the item.
51
     * @param mixed $value    The variable to store.
52
     * @param int $expiry   Expiration time of the item. If it's equal to zero, the item will never expire.
53
     *                      You can also use Unix timestamp or a number of seconds starting from current time,
54
     *                      but in the latter case the number of seconds may not exceed 2592000 (30 days).
55
     */
56
    public function set(string $variable, $value, $expiry = null)
57
    {
58
        $this->init();
59
        return static::$container->set($variable, $value, $expiry);
60
    }
61
62
    /**
63
     * Get a variable from cache
64
     * @param  string $variable The key to fetch
65
     * @return mixed           Data fetched from cache, false if not found
66
     */
67
    public function get(string $variable)
68
    {
69
        $this->init();
70
        return static::$container->get($variable);
71
    }
72
}
73