Cache   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 102
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getCacheObject() 0 17 3
A get() 0 4 1
A put() 0 4 1
A has() 0 4 1
A pull() 0 7 1
A delete() 0 4 1
A clear() 0 4 1
1
<?php
2
/**
3
 * Cache
4
 * Dependency injected cache API
5
 *
6
 * @package     erdiko/core
7
 * @copyright   2012-2017 Arroyo Labs, Inc. http://www.arroyolabs.com
8
 * @author      John Arroyo <[email protected]>
9
 */
10
namespace erdiko\core;
11
12
13
class Cache
14
{
15
    /** Cache singleton instance */
16
    private static $instance = array();
17
18
    /**
19
     * Get the cache instance
20
     *
21
     * @param string $cacheConfig
22
     * @return object
23
     */
24
    public static function getCacheObject($cacheConfig = 'default')
25
    {
26
            //Check if the caller requests an new object
27
        if (empty(static::$instance[$cacheConfig])) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
28
            $config = Helper::getConfig('application', $cacheConfig);
29
30
            //Check if the object already be created
31
            if (isset($config["cache"][$cacheConfig])) {
32
                static::$instance[$cacheConfig] = new $config["cache"][$cacheConfig]['class'];
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
33
            } else {
34
                throw new \Exception("There is no cache config defined ({$cacheConfig})");
35
            }
36
        }
37
38
        return static::$instance[$cacheConfig];
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
39
40
    }
41
42
    /**
43
     * Get the value stored at the given key
44
     *
45
     * @param string $key
46
     * @param string $cacheConfig
47
     */
48
    public static function get($key, $cacheConfig = 'default')
49
    {
50
        return static::getCacheObject($cacheConfig)->get($key);
51
    }
52
53
    /**
54
     * Put the supplied value into the given key
55
     *
56
     * @param string $key
57
     * @param mixed $value
58
     * @param string $cacheConfig
59
     */
60
    public static function put($key, $value, $cacheConfig = 'default')
61
    {
62
        return static::getCacheObject($cacheConfig)->put($key, $value);
63
    }
64
    
65
    /**
66
     * Check if the key exist
67
     *
68
     * @param string $key
69
     * @param string $cacheConfig
70
     * @return bool
71
     */
72
    public static function has($key, $cacheConfig = 'default')
73
    {
74
        return static::getCacheObject($cacheConfig)->has($key);
75
    }
76
77
    /**
78
     * Retrieve the cache value and then delete it before returning that value
79
     *
80
     * @param string $key
81
     * @param string $cacheConfig
82
     * @return mixed
83
     */
84
    public static function pull($key, $cacheConfig = 'default')
0 ignored issues
show
Unused Code introduced by
The parameter $cacheConfig is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
85
    {
86
        $value = static::get($key);
87
        static::delete($key);
88
89
        return $value;
90
    }
91
92
    /**
93
     * Remove an item from the cache
94
     *
95
     * @param string $key
96
     * @param string $cacheConfig
97
     * @return bool
98
     */
99
    public static function delete($key, $cacheConfig = 'default')
100
    {
101
        return static::getCacheObject($cacheConfig)->delete($key);
102
    }
103
104
    /**
105
     * Delete all cache keys (Purge)
106
     *
107
     * @param string $cacheConfig
108
     * @return bool
109
     */
110
    public static function clear($cacheConfig = 'default')
111
    {
112
        return static::getCacheObject($cacheConfig)->clear();
113
    }
114
}
115