NixfileCache   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 98
Duplicated Lines 10.2 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 10
loc 98
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A _getFilename() 0 4 1
A get() 0 11 3
A save() 0 18 4
A clean() 10 10 4
A remove() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.
9
 *
10
 * @category   Kumbia
11
 * @package    Cache
12
 * @subpackage Drivers
13
 *
14
 * @copyright  Copyright (c) 2005 - 2019 KumbiaPHP Team (http://www.kumbiaphp.com)
15
 * @license    https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE   New BSD License
16
 */
17
18
/**
19
 * Cacheo de Archivos para Sistemas Operativos *Nix
20
 *
21
 * @category   Kumbia
22
 * @package    Cache
23
 * @subpackage Drivers
24
 */
25
class NixfileCache extends Cache
26
{
27
    /**
28
     * Maxima marca de tiempo aproximada para procesadores de 32bits
29
     *
30
     * 18 de Enero de 2038
31
     */
32
    const MAX_TIMESTAMP = 2147401800;
33
34
    /**
35
     * Obtiene el nombre de archivo a partir de un id y grupo
36
     *
37
     * @param string $id
38
     * @param string $group
39
     * @return string
40
     * */
41
    protected function _getFilename($id, $group)
42
    {
43
        return 'cache_' . md5($id) . '.' . md5($group);
44
    }
45
46
    /**
47
     * Carga un elemento cacheado
48
     *
49
     * @param string $id
50
     * @param string $group
51
     * @return string
52
     */
53
    public function get($id, $group = 'default')
54
    {
55
        $this->_id = $id;
56
        $this->_group = $group;
57
58
        $filename = APP_PATH . 'temp/cache/' . $this->_getFilename($id, $group);
59
60
        if (is_file($filename) && filemtime($filename) >= time()) {
61
            return file_get_contents($filename);
62
        }
63
    }
64
65
    /**
66
     * Guarda un elemento en la cache con nombre $id y valor $value
67
     *
68
     * @param string $id
69
     * @param string $group
70
     * @param string $value
71
     * @param int $lifetime tiempo de vida en forma timestamp de unix
72
     * @return boolean
73
     */
74
    public function save($value, $lifetime = '', $id = '', $group = 'default')
75
    {
76
        if (!$id) {
77
            $id = $this->_id;
78
            $group = $this->_group;
79
        }
80
81
        if ($lifetime) {
82
            $lifetime = strtotime($lifetime);
83
        } else {
84
            $lifetime = self::MAX_TIMESTAMP;
85
        }
86
87
        $filename = APP_PATH . 'temp/cache/' . $this->_getFilename($id, $group);
88
89
        // Almacena en la fecha de modificacion la fecha de expiracion
90
        return file_put_contents($filename, $value) && touch($filename, $lifetime);
91
    }
92
93
    /**
94
     * Limpia la cache
95
     *
96
     * @param string $group
97
     * @return boolean
98
     */
99 View Code Duplication
    public function clean($group = '')
0 ignored issues
show
Duplication introduced by joanhey
This method seems to be duplicated in 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...
100
    {
101
        $pattern = $group ? APP_PATH . 'temp/cache/' . '*.' . md5($group) : APP_PATH . 'temp/cache/*';
102
        foreach (glob($pattern) as $filename) {
103
            if (!unlink($filename)) {
104
                return false;
105
            }
106
        }
107
        return true;
108
    }
109
110
    /**
111
     * Elimina un elemento de la cache
112
     *
113
     * @param string $id
114
     * @param string $group
115
     * @return bool
116
     */
117
    public function remove($id, $group = 'default')
118
    {
119
        return unlink(APP_PATH . 'temp/cache/' . $this->_getFilename($id, $group));
120
    }
121
122
}
123