Completed
Push — master ( 3bec47...6f7c35 )
by Mathieu
01:33
created

Cache::init()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 15

Duplication

Lines 19
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 1
b 0
f 0
nc 5
nop 0
dl 19
loc 19
rs 8.8571
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 View Code Duplication
    protected function init()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
    {
20
        if (static::$container === null) {
21
            switch ($this->type) {
22
                case 'memcache':
23
                    static::$container = Suricate::CacheMemcache(true);
24
                    break;
25
                case 'apc':
26
                    static::$container = Suricate::CacheApc(true);
27
                    break;
28
                case 'file':
29
                    static::$container = Suricate::CacheFile(true);
30
                    break;
31
                default:
32
                    throw new \Exception("Unknown cache type " . $this->type);
33
                    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...
34
            }
35
        }
36
    }
37
38
    public function getInstance()
39
    {
40
        $this->init();
41
        return static::$container;
42
    }
43
44
    /**
45
     * Setter
46
     * Put a variable in cache
47
     * @param string $variable The key that will be associated with the item.
48
     * @param mixed $value    The variable to store.
49
     * @param int $expiry   Expiration time of the item. If it's equal to zero, the item will never expire.
50
     *                      You can also use Unix timestamp or a number of seconds starting from current time,
51
     *                      but in the latter case the number of seconds may not exceed 2592000 (30 days).
52
     */
53
    public function set($variable, $value, $expiry = null)
54
    {
55
        $this->init();
56
        return static::$container->set($variable, $value, $expiry);
57
    }
58
59
    /**
60
     * Get a variable from cache
61
     * @param  string $variable The key to fetch
62
     * @return mixed           Data fetched from cache, false if not found
63
     */
64
    public function get($variable)
65
    {
66
        $this->init();
67
        return static::$container->get($variable);
68
    }
69
}
70