CFileCache   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 121
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createKey() 0 4 1
A filename() 0 4 1
A get() 0 18 4
A put() 0 14 2
A prune() 0 4 1
A pruneAll() 0 7 1
1
<?php
2
3
namespace Anax\Cache;
4
5
/**
6
 * File based cache.
7
 *
8
 */
9
class CFileCache
10
{
11
    use \Anax\TConfigure;
12
13
14
15
    /**
16
     * Create a key to use for the cache.
17
     *
18
     * @param string $class name of the class, including
19
     *                      namespace.
20
     * @param string $id    unique id for item in each class.
21
     *
22
     * @return string the filename.
23
     */
24
    public function createKey($class, $id)
25
    {
26
        return str_replace('\\', '-', $class) . '#' . $id;
27
    }
28
29
30
31
    /**
32
     * Generate a filename for the cached object.
33
     *
34
     * @param string $key to the cached object.
35
     *
36
     * @return string the filename.
37
     */
38
    public function filename($key)
39
    {
40
        return $this->config['basepath'] . '/' . $key;
41
    }
42
43
44
45
    /**
46
     * Get an item from the cache if available.
47
     *
48
     * @param string  $key to the cached object.
49
     * @param boolean $age check the age or not, defaults to
50
     *                     false.
51
     *
52
     * @return mixed the cached object or false if it has aged
53
     *               or null if it does not exists.
54
     */
55
    public function get($key, $age = false)
56
    {
57
        $file = $this->filename($key);
58
59
        if (is_file($file)) {
60
            if ($age) {
61
                $age = filemtime($file) + $this->config['age'] > time();
62
            }
63
64
            if (!$age) {
65
                // json
66
                // text
67
                return unserialize(file_get_contents($file));
68
            }
69
            return false;
70
        }
71
        return null;
72
    }
73
74
75
76
    /**
77
     * Put an item to the cache.
78
     *
79
     * @param string $key  to the cached object.
80
     * @param mixed  $item the object to be cached.
81
     *
82
     * @throws Exception if failing to write to cache.
83
     *
84
     * @return void
85
     */
86
    public function put($key, $item)
87
    {
88
        $file = $this->filename($key);
89
90
        // json
91
        // text
92
        if (!file_put_contents($file, serialize($item))) {
93
            throw new \Exception(
94
                t("Failed writing cache object '!key'.", [
95
                    '!key' => $key
96
                ])
97
            );
98
        }
99
    }
100
101
102
103
    /**
104
     * Prune a item from cache.
105
     *
106
     * @param string $key to the cached object.
107
     *
108
     * @return void
109
     */
110
    public function prune($key)
111
    {
112
        @unlink($this->filename($key));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
113
    }
114
115
116
117
    /**
118
     * Prune all items from cache.
119
     *
120
     * @return int number of items removed.
121
     */
122
    public function pruneAll()
123
    {
124
        $files = glob($this->config['basepath'] . '/*');
125
        $items = count($files);
126
        array_map('unlink', $files);
127
        return $items;
128
    }
129
}
130