RedisQueue   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 129
Duplicated Lines 4.65 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 59.51%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
lcom 1
cbo 0
dl 6
loc 129
ccs 25
cts 42
cp 0.5951
rs 10
c 1
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A put() 0 9 2
A get() 0 16 4
A size() 0 4 1
A remove() 0 4 1
C __construct() 6 34 7
A __destruct() 0 4 1
A close() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Jenner
5
 * Date: 2015/8/20
6
 * Time: 15:03
7
 */
8
9
namespace Jenner\SimpleFork\Queue;
10
11
/**
12
 * redis queue
13
 *
14
 * @package Jenner\SimpleFork\Queue
15
 */
16
class RedisQueue implements QueueInterface
17
{
18
    /**
19
     * @var \Redis
20
     */
21
    protected $redis;
22
23
    /**
24
     * @var string redis key of queue
25
     */
26
    protected $channel;
27
28
    /**
29
     * @param string $host redis server host
30
     * @param int $port redis server port
31
     * @param int $database redis server database num
32
     * @param string $channel redis queue key
33
     * @param string $prefix prefix of redis queue key
34
     */
35 6
    public function __construct(
36
        $host = '127.0.0.1',
37
        $port = 6379,
38
        $database = 0,
39
        $channel = 'cache',
40
        $prefix = 'simple-fork-'
41
    )
42
    {
43 6
        $this->redis = new \Redis();
44 6
        $connection_result = $this->redis->connect($host, $port);
45 6
        if (!$connection_result) {
46
            throw new \RuntimeException('can not connect to the redis server');
47
        }
48
49 6 View Code Duplication
        if ($database != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            $select_result = $this->redis->select($database);
51
            if (!$select_result) {
52
                throw new \RuntimeException('can not select the database');
53
            }
54
        }
55
56 6
        if (empty($channel)) {
57
            throw new \InvalidArgumentException('channel can not be empty');
58
        }
59
60 6
        $this->channel = $channel;
61
62 6
        if (empty($prefix)) return;
63
64 6
        $set_option_result = $this->redis->setOption(\Redis::OPT_PREFIX, $prefix);
65 6
        if (!$set_option_result) {
66
            throw new \RuntimeException('can not set the \Redis::OPT_PREFIX Option');
67
        }
68 6
    }
69
70
    /**
71
     * put value into the queue
72
     *
73
     * @param $value
74
     * @return bool
75
     */
76 3
    public function put($value)
77
    {
78
79 3
        if ($this->redis->lPush($this->channel, $value) !== false) {
80 3
            return true;
81
        }
82
83
        return false;
84
    }
85
86
    /**
87
     * get value from the queue
88
     *
89
     * @param bool $block if block when the queue is empty
90
     * @return bool|string
91
     */
92 6
    public function get($block = false)
93
    {
94 6
        if (!$block) {
95 6
            return $this->redis->rPop($this->channel);
96
        } else {
97
            while (true) {
98
                $record = $this->redis->rPop($this->channel);
99
                if ($record === false) {
100
                    usleep(1000);
101
                    continue;
102
                }
103
104
                return $record;
105
            }
106
        }
107
    }
108
109
    /**
110
     * get the size of the queue
111
     *
112
     * @return int
113
     */
114 6
    public function size()
115
    {
116 6
        return $this->redis->lSize($this->channel);
117
    }
118
119
    /**
120
     * remove the queue resource
121
     *
122
     * @return mixed
123
     */
124
    public function remove()
125
    {
126
        return $this->redis->delete($this->channel);
127
    }
128
129
    /**
130
     * close the connection
131
     */
132 3
    public function __destruct()
133
    {
134 3
        $this->close();
135 3
    }
136
137
    /**
138
     * close the connection
139
     */
140 6
    public function close()
141
    {
142 6
        $this->redis->close();
143
    }
144
}