Redis   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 3
A getClient() 0 7 2
A setClient() 0 8 2
A get() 0 4 1
A set() 0 4 1
A delete() 0 4 1
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 Redis as PhpRedis;
15
16
use Bouncer\Exception;
17
18
class Redis extends AbstractCache
19
{
20
21
    protected $client;
22
23
    protected $params = array();
24
25
    protected $redisOptions = [
26
        PhpRedis::OPT_SERIALIZER => PhpRedis::SERIALIZER_PHP
27
    ];
28
29
    /**
30
     * @param PhpRedis $params
31
     */
32
    public function __construct($params = null)
33
    {
34
        // Client Injection
35
        if (is_object($params) && $params instanceof PhpRedis) {
36
            $this->setClient($params);
37
        }
38
    }
39
40
    /**
41
     * @return PhpRedis
42
     *
43
     * @throws Exception
44
     */
45
    public function getClient()
46
    {
47
        if (empty($this->client)) {
48
           throw new Exception('No client available.');
49
        }
50
        return $this->client;
51
    }
52
53
    /**
54
     * @param PhpRedis $client
55
     */
56
    public function setClient(PhpRedis $client)
57
    {
58
        $this->client = $client;
59
        foreach ($this->redisOptions as $name => $value) {
60
            $this->client->setOption($name, $value);
61
        }
62
        return $this;
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public function get($key)
69
    {
70
        return $this->getClient()->get($key);
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    public function set($key, $value, $ttl = 0)
77
    {
78
        return $this->getClient()->set($key, $value, $ttl);
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84
    public function delete($key)
85
    {
86
        return $this->getClient()->delete($key);
87
    }
88
}
89