RedisHandler::clear()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Phive Queue package.
5
 *
6
 * (c) Eugene Leonovich <[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 Phive\Queue\Tests\Handler;
13
14
use Phive\Queue\RedisQueue;
15
16
class RedisHandler extends Handler
17
{
18
    /**
19
     * @var \Redis
20
     */
21
    private $redis;
22
23
    public function createQueue()
24
    {
25
        return new RedisQueue($this->redis);
26
    }
27
28
    public function clear()
29
    {
30
        $this->redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_NONE);
31
        $prefix = $this->redis->getOption(\Redis::OPT_PREFIX);
32
        $offset = strlen($prefix);
33
34
        $keys = $this->redis->keys('*');
35
        foreach ($keys as $key) {
36
            $this->redis->del(substr($key, $offset));
37
        }
38
    }
39
40
    public function createRedis()
41
    {
42
        $redis = new \Redis();
43
        $redis->connect($this->getOption('host'), $this->getOption('port'));
44
        $redis->setOption(\Redis::OPT_PREFIX, $this->getOption('prefix'));
45
46
        return $redis;
47
    }
48
49
    protected function configure()
50
    {
51
        $this->redis = $this->createRedis();
52
    }
53
}
54