Memcached   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 178
Duplicated Lines 10.67 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 19
loc 178
ccs 55
cts 55
cp 1
rs 10
wmc 23
lcom 1
cbo 1

12 Methods

Rating   Name   Duplication   Size   Complexity  
A createMemcached() 0 4 1
A setMemcachedInstance() 0 5 1
A getCacheKey() 0 5 1
A has() 0 9 2
A remove() 11 11 3
A setSetting() 0 5 1
A getSetting() 0 5 2
A get() 8 8 2
A isAvailable() 0 7 2
A __construct() 0 7 2
A getMemcachedInstance() 0 10 3
A set() 0 15 3

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
namespace FMUP\Cache\Driver;
3
4
use FMUP\Cache\CacheInterface;
5
use FMUP\Cache\Exception;
6
7
/**
8
 * Class Memcached
9
 * This driver needs MEMCACHED installed on server to work properly
10
 * @package FMUP\Cache\Driver
11
 */
12
class Memcached implements CacheInterface
13
{
14
    const SETTINGS_MEMCACHED = 'SETTINGS_MEMCACHED';
15
    /**
16
     * @see http://php.net/manual/fr/memcached.expiration.php
17
     */
18
    const SETTINGS_TTL_IN_SECOND = 'SETTINGS_TTL_IN_SECOND';
19
    const SETTINGS_CACHE_PREFIX = 'SETTINGS_CACHE_PREFIX';
20
21
    private $isAvailable = null;
22
    private $memcachedInstance = null;
23
    private $settings = array();
24
25
    /**
26
     * Check whether memcached is available
27
     * @return bool
28
     */
29 1
    public function isAvailable()
30
    {
31 1
        if (is_null($this->isAvailable)) {
32 1
            $this->isAvailable = class_exists('\Memcached');
33
        }
34 1
        return $this->isAvailable;
35
    }
36
37
    /**
38
     * constructor of Memcached
39
     * @param array $settings
40
     */
41 15
    public function __construct($settings = array())
42
    {
43 15
        if (isset($settings[self::SETTINGS_MEMCACHED])) {
44 1
            $this->setMemcachedInstance($settings[self::SETTINGS_MEMCACHED]);
45
        }
46 15
        $this->settings = $settings;
47 15
    }
48
49
    /**
50
     * Get the memcached instance
51
     * @return \Memcached
52
     * @throws Exception
53
     */
54 5
    public function getMemcachedInstance()
55
    {
56 5
        if (!$this->memcachedInstance) {
57 2
            if (!$this->isAvailable()) {
58 1
                throw new Exception('Memcached is not available');
59
            }
60 1
            $this->memcachedInstance = $this->createMemcached();
61
        }
62 4
        return $this->memcachedInstance;
63
    }
64
65
    /**
66
     * @return \Memcached
67
     * @codeCoverageIgnore
68
     */
69
    protected function createMemcached()
70
    {
71
        return new \Memcached();
72
    }
73
74
    /**
75
     * Define a memcached instance
76
     * @param \Memcached $memcachedInstance
77
     * @return $this
78
     */
79 3
    public function setMemcachedInstance(\Memcached $memcachedInstance)
80
    {
81 3
        $this->memcachedInstance = $memcachedInstance;
82 3
        return $this;
83
    }
84
85
    /**
86
     * @param string $key
87
     * @return string
88
     */
89 4
    protected function getCacheKey($key)
90
    {
91 4
        $prefix = (string)$this->getSetting(self::SETTINGS_CACHE_PREFIX);
92 4
        return $prefix . $key;
93
    }
94
95
96
    /**
97
     * Check whether a key exists
98
     * @param string $key
99
     * @return bool
100
     * @throws Exception
101
     */
102 2
    public function has($key)
103
    {
104 2
        if (!$this->isAvailable()) {
105 1
            throw new Exception('Memcached is not available');
106
        }
107 1
        $key = $this->getCacheKey($key);
108 1
        $keys = array_flip($this->getMemcachedInstance()->getAllKeys());
109 1
        return isset($keys[$key]);
110
    }
111
112
    /**
113
     * @param string $key
114
     * @return mixed
115
     * @throws Exception
116
     */
117 2 View Code Duplication
    public function get($key)
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...
118
    {
119 2
        if (!$this->isAvailable()) {
120 1
            throw new Exception('Memcached is not available');
121
        }
122 1
        $key = $this->getCacheKey($key);
123 1
        return $this->getMemcachedInstance()->get($key);
124
    }
125
126
    /**
127
     * Define a
128
     * @param string $key
129
     * @param mixed $value
130
     * @return $this
131
     * @throws Exception
132
     */
133 3
    public function set($key, $value)
134
    {
135 3
        if (!$this->isAvailable()) {
136 1
            throw new Exception('Memcached is not available');
137
        }
138 2
        $ttl = (int)$this->getSetting(self::SETTINGS_TTL_IN_SECOND);
139 2
        $key = $this->getCacheKey($key);
140 2
        if (!$this->getMemcachedInstance()->set($key, $value, $ttl)) {
141 1
            throw new Exception(
142 1
                'Error while inserting value in memcached : ' . $this->getMemcachedInstance()->getResultMessage(),
143 1
                $this->getMemcachedInstance()->getResultCode()
144
            );
145
        }
146 1
        return $this;
147
    }
148
149
    /**
150
     * Delete a value in memcache
151
     * @param string $key
152
     * @return $this
153
     * @throws Exception
154
     */
155 3 View Code Duplication
    public function remove($key)
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...
156
    {
157 3
        if (!$this->isAvailable()) {
158 1
            throw new Exception('Memcached is not available');
159
        }
160 2
        $key = $this->getCacheKey($key);
161 2
        if (!$this->getMemcachedInstance()->delete($key)) {
162 1
            throw new Exception('Error while deleting key in memcached');
163
        }
164 1
        return $this;
165
    }
166
167
    /**
168
     * Define a setting
169
     * @param string $setting
170
     * @param mixed $value
171
     * @return $this
172
     */
173 1
    public function setSetting($setting, $value)
174
    {
175 1
        $this->settings[(string)$setting] = $value;
176 1
        return $this;
177
    }
178
179
    /**
180
     * Retrieve a defined setting
181
     * @param string $setting
182
     * @return mixed|null
183
     */
184 5
    public function getSetting($setting)
185
    {
186 5
        $setting = (string) $setting;
187 5
        return isset($this->settings[$setting]) ? $this->settings[$setting] : null;
188
    }
189
}
190