Cache::setCacheType()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 6
nc 5
nop 1
1
<?php
2
3
/**
4
 * Manage Cache
5
 *
6
 * @category  	lib
7
 * @author    	Judicaël Paquet <[email protected]>
8
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
9
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
10
 * @version   	Release: 1.0.0
11
 * @filesource	https://github.com/las93/venus2
12
 * @link      	https://github.com/las93
13
 * @since     	1.0
14
 */
15
namespace Venus\lib;
16
17
use \Venus\core\Config as Config;
18
use \Venus\lib\Cache\Apc as Apc;
19
use \Venus\lib\Cache\File as CacheFile;
20
use \Venus\lib\Cache\Memcache as CacheMemcache;
21
use \Venus\lib\Cache\Redis as Redis;
22
use \Venus\lib\Cache\Mock as Mock;
23
24
/**
25
 * This class manage the Cache
26
 *
27
 * @category  	lib
28
 * @author    	Judicaël Paquet <[email protected]>
29
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
30
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
31
 * @version   	Release: 1.0.0
32
 * @filesource	https://github.com/las93/venus2
33
 * @link      	https://github.com/las93
34
 * @since     	1.0
35
 */
36
class Cache
37
{
38
	/**
39
	 * type of cache (memcache/file)
40
	 *
41
	 * @access private
42
	 * @var    string
43
	 */
44
	private static $_sTypeOfCache = 'file';
45
46
	/**
47
	 * type of cache (memcache/file)
48
	 *
49
	 * @access private
50
	 * @var    array
51
	 */
52
	private static $_aCache = array();
53
54
	/**
55
	 * set the cache that we would use
56
	 *
57
	 * @access private
58
	 * @param  string $sCacheName name of cache
59
	 * @return object
60
	 */
61
	public static function setCacheType(string $sCacheName)
62
	{
63
		if ($sCacheName === 'file') { self::$_sTypeOfCache = 'file'; }
64
		else if ($sCacheName === 'memcache') { self::$_sTypeOfCache = 'memcache'; }
65
		else if ($sCacheName === 'apc') { self::$_sTypeOfCache = 'apc'; }
66
		else if ($sCacheName === 'redis') { self::$_sTypeOfCache = 'redis'; }
67
		else { self::$_sTypeOfCache = 'mock'; }
68
	}
69
70
	/**
71
	 * set a value
72
	 *
73
	 * @access public
74
	 * @param  string $sName name of the session
75
	 * @param  mixed $mValue value of this sesion var
76
	 * @param  int $iFlag unused
77
	 * @param  int $iExpire expiration of cache
78
	 * @return void
79
	 */
80
	public static function set(string $sName, $mValue, int $iFlag = 0, int $iExpire = 0)
81
	{
82
		return self::_getCacheObject()->set($sName, $mValue, $iFlag, $iExpire);
83
	}
84
85
	/**
86
	 * get a value
87
	 *
88
	 * @access public
89
	 * @param  string $sName name of the session
90
	 * @param  int $iFlags flags
91
	 * @param  int $iTimeout expiration of cache
92
	 * @return bool
93
	 */
94
	public static function get(string $sName, int &$iFlags = null, int $iTimeout = 0) : bool
95
	{
96
		return self::_getCacheObject()->get($sName, $iFlags, $iTimeout);
97
	}
98
99
	/**
100
	 * delete a value
101
	 *
102
	 * @access public
103
	 * @param  string $sName name of the session
104
	 * @return boolean
105
	 */
106
	public static function delete(string $sName) : bool
107
	{
108
		return self::_getCacheObject()->delete($sName);
109
	}
110
111
	/**
112
	 * flush the cache
113
	 *
114
	 * @access public
115
	 * @return boolean
116
	 */
117
	public static function flush() : bool
118
	{
119
		return self::_getCacheObject()->flush();
120
	}
121
122
	/**
123
	 * initialize the cache class and get the good object
124
	 *
125
	 * @access private
126
	 * @return object
127
	 */
128
	private static function _getCacheObject()
129
	{
130
		if (self::$_sTypeOfCache === 'file') {
131
132
			if (!isset(self::$_aCache['file'])) { self::$_aCache['file'] = new CacheFile; }
133
134
			return self::$_aCache['file'];
135
		}
136
		else if (self::$_sTypeOfCache === 'memcache') {
137
  		  
138
			if (!isset(self::$_aCache['memcache'])) { 
139
			    
140
			    $oDbConf = Config::get('Memcache')->configuration;
141
			    
142
			    if (isset($oDbConf->port)) { $sPort = $oDbConf->port; }
143
			    else { $sPort = null; }
144
			    
145
			    if (isset($oDbConf->timeout)) { $iTimeout = $oDbConf->timeout; }
146
			    else { $iTimeout = null; }
147
			    
148
			    self::$_aCache['memcache'] = new CacheMemcache($oDbConf->host, $sPort, $iTimeout); 
0 ignored issues
show
Unused Code introduced by
The call to Memcache::__construct() has too many arguments starting with $oDbConf->host.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
149
			}
150
151
			return self::$_aCache['memcache'];
152
		}
153
		else if (self::$_sTypeOfCache === 'apc') {
154
155
			if (!isset(self::$_aCache['apc'])) { self::$_aCache['apc'] = new Apc; }
156
157
			return self::$_aCache['apc'];
158
		}
159
		else if (self::$_sTypeOfCache === 'redis') {
160
		    
161
			if (!isset(self::$_aCache['redis'])) {
162
			    
163
			    $oDbConf = Config::get('Redis')->configuration;
164
			    self::$_aCache['memcache'] = new Redis($oDbConf);
165
			}
166
167
			return self::$_aCache['redis'];
168
		}
169
		else if (self::$_sTypeOfCache === 'mock') {
170
		    
171
			if (!isset(self::$_aCache['mock'])) { self::$_aCache['mock'] = new Mock; }
172
173
			return self::$_aCache['mock'];
174
		}
175
	}
176
}
177