Memcache   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 2
dl 0
loc 110
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 9 5
A getClient() 0 8 2
A setClient() 0 4 1
A get() 0 14 4
A set() 0 16 4
A delete() 0 14 4
1
<?php
2
3
/*
4
 * This file is part of the Bouncer package.
5
 *
6
 * (c) François Hodierne <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bouncer\Cache;
13
14
use Memcache as PhpMemcache;
15
use Memcached as PhpMemcached;
16
17
use Bouncer\Exception;
18
19
class Memcache extends AbstractCache
20
{
21
22
    /**
23
     * @var PhpMemcache|PhpMemcached
24
     */
25
    protected $client = null;
26
27
    /**
28
     * @var string
29
     */
30
    protected $prefix = null;
31
32
    /**
33
     * @var array
34
     */
35
    protected $cache = array();
36
37
    /**
38
     * @param array $options
39
     */
40
    public function __construct(array $options = array())
41
    {
42
        if (isset($options['client']) && is_object($options['client'])) {
43
            $this->client = $options['client'];
44
        }
45
        if (isset($options['prefix']) && is_string($options['prefix'])) {
46
            $this->prefix = $options['prefix'];
47
        }
48
    }
49
50
    /**
51
     * @return PhpMemcache|PhpMemcached
52
     */
53
    public function getClient()
54
    {
55
        if (empty($this->client)) {
56
           throw new Exception('No client available.');
57
        }
58
59
        return $this->client;
60
    }
61
62
    /**
63
     * @param PhpMemcache|PhpMemcached
64
     *
65
     * @return PhpMemcache|PhpMemcached
66
     */
67
    public function setClient($client)
68
    {
69
        return $this->client = $client;
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function get($key)
76
    {
77
        $client = $this->getClient();
78
        if (empty($client)) {
79
            return;
80
        }
81
        if (!empty($this->prefix)) {
82
            $key = $this->prefix . '_' . $key;
83
        }
84
        if (empty($this->cache[$key])) {
85
            $this->cache[$key] = $client->get($key);
86
        }
87
        return $this->cache[$key];
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93
    public function set($key, $value, $ttl = 0)
94
    {
95
        $client = $this->getClient();
96
        if (empty($client)) {
97
            return;
98
        }
99
        if (!empty($this->prefix)) {
100
            $key = $this->prefix . '_' . $key;
101
        }
102
        $this->cache[$key] = $value;
103
        if ($client instanceof PhpMemcache) {
0 ignored issues
show
Bug introduced by
The class Memcache 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...
104
            return $client->set($key, $value, null, $ttl);
105
        } else {
106
            return $client->set($key, $value, $ttl);
107
        }
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113
    public function delete($key)
114
    {
115
        $client = $this->getClient();
116
        if (empty($client)) {
117
            return;
118
        }
119
        if (!empty($this->prefix)) {
120
            $key = $this->prefix . '_' . $key;
121
        }
122
        if (isset($this->cache[$key])) {
123
            unset($this->cache[$key]);
124
        }
125
        return $client->delete($key);
126
    }
127
128
}
129