Completed
Push — next ( 9ea627...5f8210 )
by François
02:20
created

Memcache::getClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4286
cc 2
eloc 4
nc 2
nop 0
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
    protected $client = null;
23
24
    protected $prefix = null;
25
26
    protected $cache = array();
27
28
    public function __construct(array $options = array())
29
    {
30
        if (!empty($options['client'])) {
31
            $this->setClient($options['client']);
32
        }
33
    }
34
35
    /**
36
     * @return PhpMemcache|PhpMemcached
37
     */
38
    public function getClient()
39
    {
40
        if (empty($this->client)) {
41
           throw new Exception('No client available.');
42
        }
43
        return $this->client;
44
    }
45
46
    /**
47
     * @param PhpMemcache|PhpMemcached
48
     *
49
     * @return PhpMemcache|PhpMemcached
50
     */
51
    public function setClient($client)
52
    {
53
        return $this->client = $client;
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    public function get($key)
60
    {
61
        $client = $this->getClient();
62
        if (empty($client)) {
63
            return;
64
        }
65
        if (!empty($this->prefix)) {
66
            $key = $this->prefix . '-' . $key;
67
        }
68
        if (empty($this->cache[$key])) {
69
            $this->cache[$key] = $client->get($key);
70
        }
71
        return $this->cache[$key];
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77
    public function set($key, $value, $expire = 0)
78
    {
79
        $client = $this->getClient();
80
        if (empty($client)) {
81
            return;
82
        }
83
        if (!empty($this->prefix)) {
84
            $key = $this->prefix . '-' . $key;
85
        }
86
        $this->cache[$key] = $value;
87
        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...
88
            return $client->set($key, $value, null, $expire);
89
        } else {
90
            return $client->set($key, $value, $expire);
91
        }
92
    }
93
94
}
95