SqliteCache::save()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 27
rs 9.7666
cc 4
nc 8
nop 4
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 - 2023 KumbiaPHP Team (http://www.kumbiaphp.com)
15
 * @license    https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE   New BSD License
16
 */
17
18
/**
19
 * Cache con Sqlite
20
 *
21
 * @category   Kumbia
22
 * @package    Cache
23
 * @subpackage Drivers
24
 */
25
class SqliteCache extends Cache
26
{
27
28
    /**
29
     * Conexion a la base de datos Sqlite
30
     *
31
     * @var resource
32
     * */
33
    protected $_db = null;
34
35
    /**
36
     * Constructor
37
     *
38
     * */
39
    public function __construct()
40
    {
41
        /**
42
         * Abre una conexión SqLite a la base de datos cache
43
         *
44
         */
45
        $this->_db = sqlite_open(APP_PATH . 'temp/cache.db');
46
        $result = sqlite_query($this->_db, "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND tbl_name='cache' ");
47
        $count = sqlite_fetch_single($result);
48
49
        if (!$count) {
50
            sqlite_exec(' CREATE TABLE cache (id TEXT, "group" TEXT, value TEXT, lifetime TEXT) ', $this->_db);
0 ignored issues
show
Bug introduced by
' CREATE TABLE cache (id... TEXT, lifetime TEXT) ' of type string is incompatible with the type resource expected by parameter $dbhandle of sqlite_exec(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
            sqlite_exec(/** @scrutinizer ignore-type */ ' CREATE TABLE cache (id TEXT, "group" TEXT, value TEXT, lifetime TEXT) ', $this->_db);
Loading history...
Bug introduced by
$this->_db of type resource is incompatible with the type string expected by parameter $query of sqlite_exec(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
            sqlite_exec(' CREATE TABLE cache (id TEXT, "group" TEXT, value TEXT, lifetime TEXT) ', /** @scrutinizer ignore-type */ $this->_db);
Loading history...
51
        }
52
    }
53
54
    /**
55
     * Carga un elemento cacheado
56
     *
57
     * @param string $id
58
     * @param string $group
59
     * @return string
60
     */
61
    public function get($id, $group='default')
62
    {
63
        $this->_id = $id;
64
        $this->_group = $group;
65
66
        $id = addslashes($id);
67
        $group = addslashes($group);
68
69
        $id = addslashes($id);
70
        $group = addslashes($group);
71
        $lifetime = time();
72
73
        $result = sqlite_query($this->_db, " SELECT value FROM cache WHERE id='$id' AND \"group\"='$group' AND lifetime>'$lifetime' OR lifetime='undefined' ");
74
        return sqlite_fetch_single($result);
75
    }
76
77
    /**
78
     * Guarda un elemento en la cache con nombre $id y valor $value
79
     *
80
     * @param string $id
81
     * @param string $group
82
     * @param string $value
83
     * @param int $lifetime tiempo de vida en forma timestamp de unix
84
     * @return boolean
85
     */
86
    public function save($value, $lifetime='', $id='', $group='default')
87
    {
88
        if (!$id) {
89
            $id = $this->_id;
90
            $group = $this->_group;
91
        }
92
93
        if ($lifetime) {
94
            $lifetime = strtotime($lifetime);
95
        } else {
96
            $lifetime = 'undefined';
97
        }
98
99
        $id = addslashes($id);
100
        $group = addslashes($group);
101
        $value = addslashes($value);
102
103
        $result = sqlite_query($this->_db, " SELECT COUNT(*) FROM cache WHERE id='$id' AND \"group\"='$group' ");
104
        $count = sqlite_fetch_single($result);
105
106
107
        // Ya existe el elemento cacheado
108
        if ($count) {
109
            return sqlite_exec(" UPDATE cache SET value='$value', lifetime='$lifetime' WHERE id='$id' AND \"group\"='$group' ", $this->_db);
0 ignored issues
show
Bug introduced by
' UPDATE cache SET value..."group\"=''.$group.'' ' of type string is incompatible with the type resource expected by parameter $dbhandle of sqlite_exec(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

109
            return sqlite_exec(/** @scrutinizer ignore-type */ " UPDATE cache SET value='$value', lifetime='$lifetime' WHERE id='$id' AND \"group\"='$group' ", $this->_db);
Loading history...
Bug introduced by
$this->_db of type resource is incompatible with the type string expected by parameter $query of sqlite_exec(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

109
            return sqlite_exec(" UPDATE cache SET value='$value', lifetime='$lifetime' WHERE id='$id' AND \"group\"='$group' ", /** @scrutinizer ignore-type */ $this->_db);
Loading history...
110
        }
111
112
        return sqlite_exec(" INSERT INTO cache (id, \"group\", value, lifetime) VALUES ('$id','$group','$value','$lifetime')", $this->_db);
113
    }
114
115
    /**
116
     * Limpia la cache
117
     *
118
     * @param string $group
119
     * @return boolean
120
     */
121
    public function clean($group='')
122
    {
123
        if ($group) {
124
            $group = addslashes($group);
125
            return sqlite_exec(" DELETE FROM cache WHERE \"group\"='$group' ", $this->_db);
0 ignored issues
show
Bug introduced by
' DELETE FROM cache WHER..."group\"=''.$group.'' ' of type string is incompatible with the type resource expected by parameter $dbhandle of sqlite_exec(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
            return sqlite_exec(/** @scrutinizer ignore-type */ " DELETE FROM cache WHERE \"group\"='$group' ", $this->_db);
Loading history...
Bug introduced by
$this->_db of type resource is incompatible with the type string expected by parameter $query of sqlite_exec(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
            return sqlite_exec(" DELETE FROM cache WHERE \"group\"='$group' ", /** @scrutinizer ignore-type */ $this->_db);
Loading history...
126
        }
127
        return sqlite_exec(" DELETE FROM cache ", $this->_db);
128
    }
129
130
    /**
131
     * Elimina un elemento de la cache
132
     *
133
     * @param string $id
134
     * @param string $group
135
     * @return boolean
136
     */
137
    public function remove($id, $group='default')
138
    {
139
        $id = addslashes($id);
140
        $group = addslashes($group);
141
142
        return sqlite_exec(" DELETE FROM cache WHERE id='$id' AND \"group\"='$group' ", $this->_db);
0 ignored issues
show
Bug introduced by
$this->_db of type resource is incompatible with the type string expected by parameter $query of sqlite_exec(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

142
        return sqlite_exec(" DELETE FROM cache WHERE id='$id' AND \"group\"='$group' ", /** @scrutinizer ignore-type */ $this->_db);
Loading history...
Bug introduced by
' DELETE FROM cache WHER..."group\"=''.$group.'' ' of type string is incompatible with the type resource expected by parameter $dbhandle of sqlite_exec(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

142
        return sqlite_exec(/** @scrutinizer ignore-type */ " DELETE FROM cache WHERE id='$id' AND \"group\"='$group' ", $this->_db);
Loading history...
143
    }
144
145
}
146