FileStore::set()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 3
1
<?php
2
/**
3
 * Saves cached values in the file.
4
 *
5
 * @package SugiPHP.Cache
6
 * @author  Plamen Popov <[email protected]>
7
 * @license http://opensource.org/licenses/mit-license.php (MIT License)
8
 */
9
10
namespace SugiPHP\Cache;
11
12
class FileStore implements StoreInterface
13
{
14
    /**
15
     * Path to directory where cache files will be saved.
16
     */
17
    protected $path;
18
19
    /**
20
     * Creates a File store
21
     *
22
     * @param string
23
     */
24
    public function __construct($path)
25
    {
26
        $this->path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function add($key, $value, $ttl = 0)
33
    {
34
        if ($this->has($key)) {
35
            return false;
36
        }
37
38
        return $this->set($key, $value, $ttl);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function set($key, $value, $ttl = 0)
45
    {
46
        $file = $this->filename($key);
47
        $expire = ($ttl) ? time() + $ttl : "9999999999";
48
        // serializing is done mainly to distinguish type of the $value - string, number, etc.
49
        $contents = $expire.serialize($value);
50
51
        return (boolean) (@file_put_contents($file, $contents, LOCK_EX));
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function get($key)
58
    {
59
        $file = $this->filename($key);
60
        if (!is_file($file)) {
61
            return null;
62
        }
63
64
        $contents = file_get_contents($file);
65
66
        // check TTL
67
        $expire = substr($contents, 0, 10);
68
        if ($expire < time()) {
69
            // if cache is expired delete cache file
70
            $this->delete($key);
71
72
            return null;
73
        }
74
75
        return unserialize(substr($contents, 10));
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function has($key)
82
    {
83
        return (is_null($this->get($key))) ? false : true;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function delete($key)
90
    {
91
        @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...
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function flush()
98
    {
99
        $files = glob($this->path."*.cache");
100
        if ($files) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $files of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
101
            foreach ($files as $file) {
102
                @unlink($file);
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...
103
            }
104
        }
105
    }
106
107
    /**
108
     * Generates a filename based on the $key parameter
109
     *
110
     * @param string $key
111
     *
112
     * @return string
113
     */
114
    protected function filename($key)
115
    {
116
        return $this->path.md5($key).".cache";
117
    }
118
}
119