File   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 0
dl 0
loc 124
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A set() 0 5 1
B get() 0 16 5
A delete() 0 4 1
A flush() 0 4 1
A _getSubDirectory() 0 9 2
B _removeDirectory() 0 13 7
1
<?php
2
3
/**
4
 * Manage Cache by file
5
 *
6
 * @category  	lib
7
 * @package		lib\Cache
8
 * @author    	Judicaël Paquet <[email protected]>
9
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
10
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
11
 * @version   	Release: 1.0.0
12
 * @filesource	https://github.com/las93/venus2
13
 * @link      	https://github.com/las93
14
 * @since     	1.0
15
 */
16
namespace Venus\lib\Cache;
17
use \Venus\lib\Cache\CacheInterface;
18
19
/**
20
 * This class manage the Cache by file
21
 *
22
 * @category  	lib
23
 * @package		lib\Cache
24
 * @author    	Judicaël Paquet <[email protected]>
25
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
26
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
27
 * @version   	Release: 1.0.0
28
 * @filesource	https://github.com/las93/venus2
29
 * @link      	https://github.com/las93
30
 * @since     	1.0
31
 */
32
class File implements CacheInterface
33
{
34
    /**
35
     * var containe this folder of cache
36
     *
37
     * @access private
38
     * @var    string
39
     */
40
    private $_sFolder = '';
41
42
    /**
43
     * constructor
44
     *
45
     * @access public
46
     */
47
    public function __construct()
48
    {
49
        $this->_sFolder = str_replace('bundles'.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'Cache', CACHE_DIR, __DIR__).DIRECTORY_SEPARATOR;
50
    }
51
52
    /**
53
     * set a value
54
     *
55
     * @access public
56
     * @param  string $sName name of the session
57
     * @param  mixed $mValue value of this sesion var
58
     * @param  int $iFlag flags
59
     * @param  int $iExpire expiration of cache
60
     * @return \Venus\lib\Cache\File
61
     */
62
    public function set(string $sName, $mValue, int $iFlag, int $iExpire)
63
    {
64
        file_put_contents($this->_sFolder.$this->_getSubDirectory($sName).md5($sName).'.fil.cac', serialize($mValue));
65
        return $this;
66
    }
67
68
    /**
69
     * get a value
70
     *
71
     * @access public
72
     * @param  string $sName name of the session
73
     * @param  int $iFlags flags
74
     * @param  int $iTimeout expiration of cache
75
     * @return mixed
76
     */
77
    public function get(string $sName, int &$iFlags = null, int $iTimeout = 0)
78
    {
79
        if ($iTimeout > 0 && file_exists($this->_sFolder.$this->_getSubDirectory($sName).md5($sName).'.fil.cac')
80
            && time() - filemtime($this->_sFolder.$this->_getSubDirectory($sName).md5($sName).'.fil.cac') > $iTimeout) {
81
82
            unlink($this->_sFolder.$this->_getSubDirectory($sName).md5($sName).'.fil.cac');
83
        }
84
85
        if (file_exists($this->_sFolder.$this->_getSubDirectory($sName).md5($sName).'.fil.cac')) {
86
87
            return unserialize(file_get_contents($this->_sFolder . $this->_getSubDirectory($sName) . md5($sName) . '.fil.cac'));
88
        } else {
89
90
            return false;
91
        }
92
    }
93
94
    /**
95
     * delete a value
96
     *
97
     * @access public
98
     * @param  string $sName name of the session
99
     * @return mixed
100
     */
101
    public function delete(string $sName)
102
    {
103
        return unlink($this->_sFolder.$this->_getSubDirectory($sName).md5($sName).'.fil.cac');
104
    }
105
106
    /**
107
     * flush the cache
108
     *
109
     * @access public
110
     * @param  string $sName name of the session
0 ignored issues
show
Bug introduced by
There is no parameter named $sName. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
111
     * @return mixed
112
     */
113
    public function flush()
114
    {
115
        $this->_removeDirectory($this->_sFolder);
116
    }
117
118
    /**
119
     *
120
     *
121
     * @access public
122
     * @param  string $sName name of the session
123
     * @return mixed
124
     */
125
    private function _getSubDirectory($sName)
126
    {
127
        if (!file_exists($this->_sFolder.substr(md5($sName), 0, 2).DIRECTORY_SEPARATOR.substr(md5($sName), 2, 2))) {
128
129
            mkdir($this->_sFolder.substr(md5($sName), 0, 2).DIRECTORY_SEPARATOR.substr(md5($sName), 2, 2), 0777, true);
130
        }
131
132
        return substr(md5($sName), 0, 2).DIRECTORY_SEPARATOR.substr(md5($sName), 2, 2).DIRECTORY_SEPARATOR;
133
    }
134
135
    /**
136
     * remove a directory recursivly
137
     *
138
     * @access private
139
     * @param  string $sName nom du répertoire
140
     * @return void
141
     */
142
    private function _removeDirectory($sName)
143
    {
144
        if ($rDirectory = opendir($sName)) {
145
146
            while (($sFile = readdir($rDirectory)) !== false) {
147
148
                if ($sFile > '0' && filetype($sName.$sFile) == "file") { unlink($sName.$sFile); } elseif ($sFile > '0' && filetype($sName.$sFile) == "dir") { remove_dir($sName.$sFile."\\"); }
149
            }
150
151
            closedir($rDirectory);
152
            rmdir($sName);
153
        }
154
    }
155
}
156