MemcachedAdapter   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 114
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A set() 0 6 1
A get() 0 5 1
A delete() 0 5 1
A clear() 0 6 1
A getInstance() 0 4 1
A key() 0 12 3
A createKey() 0 4 1
1
<?php
2
3
namespace Journey\Cache\Adapters;
4
5
use Journey\Cache\CacheAdapterInterface;
6
use Memcached;
7
8
class MemcachedAdapter implements CacheAdapterInterface
9
{
10
    /**
11
     * Instance of memcached.
12
     *
13
     * @var \Memcached
14
     */
15
    protected $instance;
16
17
    /**
18
     * Initialize a new localcache.
19
     *
20
     * @param string $filepath
0 ignored issues
show
Bug introduced by
There is no parameter named $filepath. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
21
     */
22 6
    public function __construct(array $config)
23
    {
24 6
        $this->config = $config;
0 ignored issues
show
Bug introduced by
The property config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25 6
        $this->instance = new Memcached();
26 6
        if ($config['servers']) {
27 6
            foreach ($config['servers'] as $server) {
28 6
                $this->instance->addServer($server['host'], $server['port']);
29 6
            }
30 6
        }
31 6
    }
32
33
    /**
34
     * Must implement a set method.
35
     *
36
     * @param  string $key   key to set as the cache value.
37
     * @param  mixed  $value returns the value of the cached item.
38
     * @param  integer  $expiration when the cache should expire (unix timestamp)
39
     * @return $this
40
     */
41 4
    public function set($key, $value, $expiration = 0)
42
    {
43 4
        $key = $this->createKey($key);
44 4
        $this->instance->set($key, $value, $expiration);
45 4
        return $this;
46
    }
47
48
    /**
49
     * Must implement a get method.
50
     *
51
     * @param  string $key   Get a cached item by key.
52
     * @return mixed         Returns cached item or false.
53
     */
54 4
    public function get($key)
55
    {
56 4
        $key = $this->createKey($key);
57 4
        return $this->instance->get($key);
58
    }
59
60
    /**
61
     * Must implement a delete method.
62
     *
63
     * @param  string $key delete a specific cached item by key.
64
     * @return $this
65
     */
66 1
    public function delete($key)
67
    {
68 1
        $this->instance->delete($this->createKey($key));
69 1
        return $this;
70
    }
71
72
    /**
73
     * Clear all of the values set by this cache instance.
74
     *
75
     * @return $this
76
     */
77 6
    public function clear()
78
    {
79 6
        $this->instance->delete('memcached_adapter_namespace');
80 6
        $this->key(true);
81 6
        return $this;
82
    }
83
84
    /**
85
     * Return the Memcached instance.
86
     *
87
     * @return \Memcached
88
     */
89 1
    public function getInstance()
90
    {
91 1
        return $this->instance;
92
    }
93
94
    /**
95
     * Get the value of a namespace key.
96
     *
97
     * @return string
98
     */
99 6
    public function key($reset = false)
100
    {
101 6
        static $key;
102 6
        $key = $reset ? false : $key;
103 6
        if (!$key) {
104 6
            $key = $this->instance->get('memcached_adapter_namespace', function ($memc, $key, &$value) {
105 6
                $value = bin2hex(openssl_random_pseudo_bytes(8));
106 6
                return true;
107 6
            });
108 6
        }
109 6
        return $key;
110
    }
111
112
    /**
113
     * Given a user provided key, return a namespaced key.
114
     *
115
     * @return string
116
     */
117 4
    public function createKey($key)
118
    {
119 4
        return $this->key() . "-" . $key;
120
    }
121
}
122