Completed
Push — master ( 579af5...b29473 )
by Oleg
07:53
created

micro/cache/FileCache.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php /** MicroFileCache */
2
3
namespace Micro\Cache;
4
5
use Micro\Base\Exception;
6
use Micro\File\FileHelper;
7
8
/**
9
 * Class FileCache
10
 *
11
 * @author Oleg Lunegov <[email protected]>
12
 * @link https://github.com/lugnsk/micro
13
 * @copyright Copyright &copy; 2013 Oleg Lunegov
14
 * @license /LICENSE
15
 * @package Micro
16
 * @subpackage Cache
17
 * @version 1.0
18
 * @since 1.0
19
 */
20
class FileCache extends BaseCache
21
{
22
    /** @var string $driver directory name */
23
    protected $driver;
24
25
    /**
26
     * Constructor
27
     *
28
     * @access pubic
29
     *
30
     * @param array $config config array
31
     *
32
     * @result void
33
     * @throws Exception
34
     */
35
    public function __construct(array $config = [])
36
    {
37
        parent::__construct($config);
38
39
        $path = !empty($config['path']) ? $config['path'] : sys_get_temp_dir() . '/cache';
40 View Code Duplication
        if (!@mkdir($path, 0600) && !is_dir($path)) {
0 ignored issues
show
This code seems to be duplicated across 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...
41
            throw new Exception('Can`not create/check access to directory: ' . $path);
42
        }
43
        $this->driver = $path;
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function check()
50
    {
51
        return is_writable($this->driver) ? true : false;
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function delete($name)
58
    {
59
        unlink($this->driver . '/' . $name);
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function clean()
66
    {
67
        FileHelper::removeDir($this->driver);
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function info()
74
    {
75
        return count(scandir($this->driver)) - 2;
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function getMeta($id)
82
    {
83
        return filesize($this->driver . '/' . $id);
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public function increment($name, $offset = 1)
90
    {
91
        $this->set($name, ((int)$this->get($name) + $offset));
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97
    public function set($name, $value)
98
    {
99
        return file_put_contents($this->driver . '/' . $name, $value);
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105
    public function get($name)
106
    {
107
        return file_get_contents($this->driver . '/' . $name);
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113
    public function decrement($name, $offset = 1)
114
    {
115
        $this->set($name, ((int)$this->get($name) - $offset));
116
    }
117
} 
118