MysqlCache   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 159
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 6
dl 159
loc 159
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B flush() 34 34 3
A clean() 14 14 3
C load() 43 43 13
B save() 28 28 6
A touch() 5 5 1
A make_mysql_table_name() 4 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
 * to do: separate into MEMORY and NON-MEMORY
4
 *
5
 */
6
7 View Code Duplication
class MysqlCache extends Object implements flushable
0 ignored issues
show
Duplication introduced by
This class 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...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    private static $cache_in_mysql_tables = array();
0 ignored issues
show
Unused Code introduced by
The property $cache_in_mysql_tables is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
10
11
    public static function flush()
12
    {
13
        $cache = SS_Cache::factory('any');
14
        $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
15
        $tables = Config::inst()->get('EcommerceCache', 'cache_in_mysql_tables');
16
        if (is_array($tables)) {
17
            foreach ($tables as $table) {
18
                $table = self::make_mysql_table_name($table);
19
                DB::query(
20
                    '
21
                    DROP TABLE IF EXISTS "'.$table.'";
22
                    '
23
                );
24
                DB::query(
25
                    '
26
                    CREATE TABLE "'.$table.'" (
27
                      "PAGEID" int(11) NOT NULL,
28
                      "CACHEKEY" CHAR(50) NOT NULL,
29
                      "DATA" TEXT
30
                    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
31
                    '
32
                );
33
34
                DB::query(
35
                    '
36
                    ALTER TABLE "'.$table.'"
37
                      ADD PRIMARY KEY ("PAGEID","CACHEKEY"),
38
                      ADD KEY "CACHEKEY" ("CACHEKEY"),
39
                      ADD KEY "PAGEID" ("PAGEID");
40
                    '
41
                );
42
            }
43
        }
44
    }
45
46
    public static function clean()
47
    {
48
        $tables = Config::inst()->get('EcommerceCache', 'cache_in_mysql_tables');
49
        if (is_array($tables)) {
50
            foreach ($tables as $table) {
51
                $table = self::make_mysql_table_name($table);
52
                DB::query(
53
                    '
54
                    DELETE FROM "'.$table.'";
55
                    '
56
                );
57
            }
58
        }
59
    }
60
61
    private static $_items = array();
62
63
    /**
64
     * @param string           $table
65
     * @param int              $id
66
     * @param string           $cacheKey
67
     * @return mixed
68
     */
69
    public static function load($table, $id, $cacheKey)
70
    {
71
        $tables = Config::inst()->get('EcommerceCache', 'cache_in_mysql_tables');
72
        if (is_array($tables) && in_array($table, $tables)) {
73
            $table = self::make_mysql_table_name($table);
74
            $id = (int)$id;
75
            if (isset(self::$_items[$table])) {
76
                if (isset(self::$_items[$table][$id])) {
77
                    if (isset(self::$_items[$table][$id][$cacheKey])) {
78
                        if (self::$_items[$table][$id][$cacheKey] !== null) {
79
                            return @unserialize(self::$_items[$table][$id][$cacheKey]);
80
                        }
81
                    }
82
                }
83
            }
84
85
            //we are now loading the data ...
86
            if (!isset(self::$_items[$table])) {
87
                self::$_items[$table] = array();
88
            }
89
            if (! isset(self::$_items[$table][$id])) {
90
                self::$_items[$table][$id] = array();
91
                $rows = DB::query('SELECT "CACHEKEY", "DATA" FROM "'.$table.'" WHERE "PAGEID" = '.$id.' ;');
92
                foreach ($rows as $row) {
93
                    self::$_items[$table][$id][$row['CACHEKEY']] = $row['DATA'];
94
                }
95
                //return the value, if there is one.
96
                if (isset(self::$_items[$table][$id])) {
97
                    if (isset(self::$_items[$table][$id][$cacheKey])) {
98
                        return @unserialize(self::$_items[$table][$id][$cacheKey]);
99
                    }
100
                }
101
            }
102
        } else {
103
            $cache = SS_Cache::factory($table.'_'.$id);
104
            $data = $cache->load($cacheKey);
105
            if (!$data) {
106
                return;
107
            }
108
109
            return @unserialize($data);
110
        }
111
    }
112
113
    /**
114
     * @param string           $table
115
     * @param int              $id
116
     * @param string           $cacheKey
117
     * @param mixed            $data
118
     *
119
     */
120
    public static function save($table, $id, $cacheKey, $data)
121
    {
122
        $tables = Config::inst()->get('EcommerceCache', 'cache_in_mysql_tables');
123
        if (is_array($tables) && in_array($table, $tables)) {
124
            $table = self::make_mysql_table_name($table);
125
            $id = (int)$id;
126
            if (strlen($cacheKey) > 50) {
127
                user_error('ERROR: CACHEKEY longer than 50 characters: '.$cacheKey);
128
            }
129
            $data = Convert::raw2sql(serialize($data));
130
            if (!isset(self::$_items[$table])) {
131
                self::$_items[$table] = array();
132
            }
133
            if (isset(self::$_items[$table][$id])) {
134
                self::$_items[$table][$id] = array();
135
            }
136
            self::$_items[$table][$id][$cacheKey] = $data;
137
            DB::query('
138
                INSERT INTO "'.$table.'" ("PAGEID", "CACHEKEY", "DATA")
139
                VALUES ('.$id.', \''.$cacheKey.'\', \''.$data.'\')
140
                ON DUPLICATE KEY UPDATE DATA = \''.$data.'\';
141
            ');
142
        } else {
143
            $cache = SS_Cache::factory($table."_".$id);
144
            $data = serialize($data);
145
            $cache->save($data, $cacheKey);
146
        }
147
    }
148
149
    /**
150
     * @param string           $table
151
     * @param int              $id
152
     * @param string           $cacheKey
153
     * @param mixed            $data
154
     */
155
    public static function touch($table, $id, $cacheKey, $data)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
156
    {
157
        $data = self::load($table, $inty, $cacheKey);
0 ignored issues
show
Bug introduced by
The variable $inty does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
158
        self::save($table, $id, $cacheKey, $data);
159
    }
160
161
    private static function make_mysql_table_name($table)
162
    {
163
        return strtoupper($table). '_CACHE';
164
    }
165
}
166