Cache_Core   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 190
Duplicated Lines 12.11 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 23
loc 190
rs 10
wmc 20
lcom 1
cbo 3

9 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 9 9 2
B __construct() 14 54 9
A get() 0 7 1
A find() 0 4 1
A set() 0 16 3
A delete() 0 7 1
A delete_tag() 0 4 1
A delete_all() 0 4 1
A sanitize_id() 0 5 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 defined('SYSPATH') or die('No direct access allowed.');
2
/**
3
 * Provides a driver-based interface for finding, creating, and deleting cached
4
 * resources. Caches are identified by a unique string. Tagging of caches is
5
 * also supported, and caches can be found and deleted by id or tag.
6
 *
7
 * $Id: Cache.php 4321 2009-05-04 01:39:44Z kiall $
8
 *
9
 * @package    Cache
10
 * @author     Kohana Team
11
 * @copyright  (c) 2007-2008 Kohana Team
12
 * @license    http://kohanaphp.com/license.html
13
 */
14
class Cache_Core
15
{
16
    protected static $instances = array();
17
18
    // For garbage collection
19
    protected static $loaded;
20
21
    // Configuration
22
    protected $config;
23
24
    // Driver object
25
    protected $driver;
26
27
    /**
28
     * Returns a singleton instance of Cache.
29
     *
30
     * @param   string  configuration
31
     * @return  Cache_Core
32
     */
33 View Code Duplication
    public static function & instance($config = false)
0 ignored issues
show
Duplication introduced by
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...
34
    {
35
        if (! isset(Cache::$instances[$config])) {
36
            // Create a new instance
37
            Cache::$instances[$config] = new Cache($config);
38
        }
39
40
        return Cache::$instances[$config];
41
    }
42
43
    /**
44
     * Loads the configured driver and validates it.
45
     *
46
     * @param   array|string  custom configuration or config group name
47
     * @return  void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
48
     */
49
    public function __construct($config = false)
50
    {
51 View Code Duplication
        if (is_string($config)) {
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...
52
            $name = $config;
53
54
            // Test the config group name
55
            if (($config = Kohana::config('cache.'.$config)) === null) {
56
                throw new Kohana_Exception('cache.undefined_group', $name);
57
            }
58
        }
59
60
        if (is_array($config)) {
61
            // Append the default configuration options
62
            $config += Kohana::config('cache.default');
63
        } else {
64
            // Load the default group
65
            $config = Kohana::config('cache.default');
66
        }
67
68
        // Cache the config in the object
69
        $this->config = $config;
70
71
        // Set driver name
72
        $driver = 'Cache_'.ucfirst($this->config['driver']).'_Driver';
73
74
        // Load the driver
75 View Code Duplication
        if (! Kohana::auto_load($driver)) {
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...
76
            throw new Kohana_Exception('core.driver_not_found', $this->config['driver'], get_class($this));
77
        }
78
79
        // Initialize the driver
80
        $this->driver = new $driver($this->config['params']);
81
82
        // Validate the driver
83 View Code Duplication
        if (! ($this->driver instanceof Cache_Driver)) {
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...
84
            throw new Kohana_Exception('core.driver_implements', $this->config['driver'], get_class($this), 'Cache_Driver');
85
        }
86
87
        Kohana::log('debug', 'Cache Library initialized');
88
89
        if (Cache::$loaded !== true) {
90
            $this->config['requests'] = (int) $this->config['requests'];
91
92
            if ($this->config['requests'] > 0 and mt_rand(1, $this->config['requests']) === 1) {
93
                // Do garbage collection
94
                $this->driver->delete_expired();
95
96
                Kohana::log('debug', 'Cache: Expired caches deleted.');
97
            }
98
99
            // Cache has been loaded once
100
            Cache::$loaded = true;
101
        }
102
    }
103
104
    /**
105
     * Fetches a cache by id. NULL is returned when a cache item is not found.
106
     *
107
     * @param   string  cache id
108
     * @return  mixed   cached data or NULL
109
     */
110
    public function get($id)
111
    {
112
        // Sanitize the ID
113
        $id = $this->sanitize_id($id);
114
115
        return $this->driver->get($id);
116
    }
117
118
    /**
119
     * Fetches all of the caches for a given tag. An empty array will be
120
     * returned when no matching caches are found.
121
     *
122
     * @param   string  cache tag
123
     * @return  array   all cache items matching the tag
124
     */
125
    public function find($tag)
126
    {
127
        return $this->driver->find($tag);
128
    }
129
130
    /**
131
     * Set a cache item by id. Tags may also be added and a custom lifetime
132
     * can be set. Non-string data is automatically serialized.
133
     *
134
     * @param   string        unique cache id
135
     * @param   mixed         data to cache
136
     * @param   array|string  tags for this item
137
     * @param   integer       number of seconds until the cache expires
138
     * @return  boolean
139
     */
140
    public function set($id, $data, $tags = null, $lifetime = null)
141
    {
142
        if (is_resource($data)) {
143
            throw new Kohana_Exception('cache.resources');
144
        }
145
146
        // Sanitize the ID
147
        $id = $this->sanitize_id($id);
148
149
        if ($lifetime === null) {
150
            // Get the default lifetime
151
            $lifetime = $this->config['lifetime'];
152
        }
153
154
        return $this->driver->set($id, $data, (array) $tags, $lifetime);
155
    }
156
157
    /**
158
     * Delete a cache item by id.
159
     *
160
     * @param   string   cache id
161
     * @return  boolean
162
     */
163
    public function delete($id)
164
    {
165
        // Sanitize the ID
166
        $id = $this->sanitize_id($id);
167
168
        return $this->driver->delete($id);
169
    }
170
171
    /**
172
     * Delete all cache items with a given tag.
173
     *
174
     * @param   string   cache tag name
175
     * @return  boolean
176
     */
177
    public function delete_tag($tag)
178
    {
179
        return $this->driver->delete($tag, true);
180
    }
181
182
    /**
183
     * Delete ALL cache items items.
184
     *
185
     * @return  boolean
186
     */
187
    public function delete_all()
188
    {
189
        return $this->driver->delete(true);
190
    }
191
192
    /**
193
     * Replaces troublesome characters with underscores.
194
     *
195
     * @param   string   cache id
196
     * @return  string
197
     */
198
    protected function sanitize_id($id)
199
    {
200
        // Change slashes and spaces to underscores
201
        return str_replace(array('/', '\\', ' '), '_', $id);
202
    }
203
} // End Cache
204