Redis::getClient()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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 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