SugarCacheRedis::_fixKeyName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
/*********************************************************************************
3
 * SugarCRM Community Edition is a customer relationship management program developed by
4
 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
5
6
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
7
 * Copyright (C) 2011 - 2014 Salesagility Ltd.
8
 *
9
 * This program is free software; you can redistribute it and/or modify it under
10
 * the terms of the GNU Affero General Public License version 3 as published by the
11
 * Free Software Foundation with the addition of the following permission added
12
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
13
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
14
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
15
 *
16
 * This program is distributed in the hope that it will be useful, but WITHOUT
17
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
19
 * details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License along with
22
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
23
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24
 * 02110-1301 USA.
25
 *
26
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
27
 * SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
28
 *
29
 * The interactive user interfaces in modified source and object code versions
30
 * of this program must display Appropriate Legal Notices, as required under
31
 * Section 5 of the GNU Affero General Public License version 3.
32
 *
33
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
34
 * these Appropriate Legal Notices must retain the display of the "Powered by
35
 * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
36
 * reasonably feasible for  technical reasons, the Appropriate Legal Notices must
37
 * display the words  "Powered by SugarCRM" and "Supercharged by SuiteCRM".
38
 ********************************************************************************/
39
40
41
require_once('include/SugarCache/SugarCacheAbstract.php');
42
43
/**
44
 * Redis SugarCache backend, using the PHP Redis C library at http://github.com/nicolasff/phpredis
45
 */
46
class SugarCacheRedis extends SugarCacheAbstract
47
{
48
    /**
49
     * @var Redis server name string
50
     */
51
    protected $_host = 'localhost';
52
    
53
    /**
54
     * @var Redis server port int
55
     */
56
    protected $_port = 6379;
57
    
58
    /**
59
     * @var Redis object
60
     */
61
    protected $_redis = null;
62
    
63
    /**
64
     * @see SugarCacheAbstract::$_priority
65
     */
66
    protected $_priority = 920;
67
    
68
    /**
69
     * @see SugarCacheAbstract::useBackend()
70
     */
71
    public function useBackend()
72
    {
73
        if ( !parent::useBackend() )
74
            return false;
75
        
76
        if ( extension_loaded("redis")
77
                && empty($GLOBALS['sugar_config']['external_cache_disabled_redis'])
78
                && $this->_getRedisObject() )
79
            return true;
80
            
81
        return false;
82
    }
83
    
84
    /**
85
     * @see SugarCacheAbstract::__construct()
86
     */
87
    public function __construct()
88
    {
89
        parent::__construct();
90
    }
91
    
92
    /**
93
     * Get the memcache object; initialize if needed
94
     */
95
    protected function _getRedisObject()
96
    {
97
        try {
98
            if ( !($this->_redis instanceOf Redis) ) {
0 ignored issues
show
Bug introduced by
The class Redis does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
99
                $this->_redis = new Redis();
100
                $this->_host = SugarConfig::getInstance()->get('external_cache.redis.host', $this->_host);
101
                $this->_port = SugarConfig::getInstance()->get('external_cache.redis.port', $this->_port);
102
                if ( !$this->_redis->connect($this->_host,$this->_port) ) {
103
                    return false;
104
                }
105
            }
106
        }
107
        catch (RedisException $e)
0 ignored issues
show
Bug introduced by
The class RedisException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
108
        {
109
            return false;
110
        }
111
        
112
        return $this->_redis;
113
    }
114
    
115
    /**
116
     * @see SugarCacheAbstract::_setExternal()
117
     */
118
    protected function _setExternal(
119
        $key,
120
        $value
121
        )
122
    {
123
        $value = serialize($value);
124
        $key = $this->_fixKeyName($key);
125
        
126
        $this->_getRedisObject()->set($key,$value);
127
        $this->_getRedisObject()->expire($key, $this->_expireTimeout);
128
    }
129
    
130
    /**
131
     * @see SugarCacheAbstract::_getExternal()
132
     */
133
    protected function _getExternal(
134
        $key
135
        )
136
    {
137
        $key = $this->_fixKeyName($key);
138
        $returnValue = $this->_getRedisObject()->get($key);
139
        // return null if we don't get a cache hit
140
        if ( $returnValue === false ) {
141
            return null;
142
        }
143
        
144
        return is_string($returnValue) ?
145
            unserialize($returnValue) :
146
            $returnValue;
147
    }
148
    
149
    /**
150
     * @see SugarCacheAbstract::_clearExternal()
151
     */
152
    protected function _clearExternal(
153
        $key
154
        )
155
    {
156
        $key = $this->_fixKeyName($key);
157
        $this->_getRedisObject()->delete($key);
158
    }
159
    
160
    /**
161
     * @see SugarCacheAbstract::_resetExternal()
162
     */
163
    protected function _resetExternal()
164
    {
165
        $this->_getRedisObject()->flushAll();
166
    }
167
    
168
    /**
169
     * Fixed the key naming used so we don't have any spaces
170
     *
171
     * @param  string $key
172
     * @return string fixed key name
173
     */
174
    protected function _fixKeyName($key)
175
    {
176
        return str_replace(' ','_',$key);
177
    }
178
}
179