Completed
Push — master ( a1970e...6c5d6a )
by Oleg
04:15
created

FileDriver::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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