Memcache   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 76.19%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 38
rs 10
c 1
b 0
f 0
ccs 16
cts 21
cp 0.7619
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 8 3
A get() 0 12 4
A __construct() 0 8 4
1
<?php
2
3
/**
4
 * Memcache wrapper
5
 *
6
 * @package TheyWorkForYou
7
 */
8
9
namespace MySociety\TheyWorkForYou;
10
11
class Memcache {
12
    public static $memcache;
13
14
    public function __construct() {
15 2
        if (!self::$memcache) {
16 2
            if (class_exists('\Memcached')) {
17 1
                self::$memcache = new \Memcached();
18 1
                self::$memcache->addServer(OPTION_TWFY_MEMCACHED_HOST, OPTION_TWFY_MEMCACHED_PORT);
0 ignored issues
show
Bug introduced by
The constant MySociety\TheyWorkForYou...ION_TWFY_MEMCACHED_PORT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant MySociety\TheyWorkForYou...ION_TWFY_MEMCACHED_HOST was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
19 1
            } elseif (class_exists('\Memcache')) {
20
                self::$memcache = new \Memcache();
21
                self::$memcache->connect(OPTION_TWFY_MEMCACHED_HOST, OPTION_TWFY_MEMCACHED_PORT);
22
            }
23
        }
24
    }
25 2
26
    public function set($key, $value, $timeout = 3600) {
27 2
        if (!self::$memcache) {
28 2
            return;
29 2
        }
30
        if (class_exists('\Memcached')) {
31
            self::$memcache->set(OPTION_TWFY_DB_NAME . ':' . $key, $value, $timeout);
32
        } else {
33 2
            self::$memcache->set(OPTION_TWFY_DB_NAME . ':' . $key, $value, MEMCACHE_COMPRESSED, $timeout);
34
        }
35 1
    }
36
37 1
    public function get($key) {
38 1
        // see http://php.net/manual/en/memcache.get.php#112056 for explanation of this
39 1
        $was_found = false;
40
        if (class_exists('\Memcached')) {
41
            $value = self::$memcache->get(OPTION_TWFY_DB_NAME . ':' . $key, null, $was_found);
42
        } elseif (class_exists('\Memcache')) {
43 1
            $value = self::$memcache->get(OPTION_TWFY_DB_NAME . ':' . $key, $was_found);
44 1
        }
45
        if ($was_found === false) {
0 ignored issues
show
introduced by
The condition $was_found === false is always true.
Loading history...
46
            return false; // mmmmm
47
        } else {
48
            return $value;
49
        }
50
    }
51
}
52