Completed
Push — master ( 104680...55a46d )
by Mike
03:18
created

PredisAdapter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 97
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 95.35%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 97
loc 97
ccs 41
cts 43
cp 0.9535
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A ping() 6 6 1
A bRPopLPush() 6 6 1
A hGet() 6 6 1
A sAdd() 6 6 1
A sRem() 6 6 1
A sIsMember() 6 6 1
A sMembers() 6 6 1
A lLen() 6 6 1
A lRange() 6 6 1
A zScore() 7 7 2
A evalLua() 6 6 1
A tryCatch() 8 8 2

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
namespace MGDigital\BusQue\Redis\Predis;
4
5
use MGDigital\BusQue\Exception\RedisException;
6
use MGDigital\BusQue\Redis\RedisAdapterInterface;
7
use Predis\ClientInterface;
8
use Predis\Connection\ConnectionException;
9
10 View Code Duplication
class PredisAdapter implements RedisAdapterInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
11
{
12
13
    private $client;
14
15 11
    public function __construct(ClientInterface $client)
16
    {
17 11
        $this->client = $client;
18 11
    }
19
20 1
    public function ping()
21
    {
22
        $this->tryCatch(function () {
23 1
            $this->client->ping();
24 1
        });
25 1
    }
26
27 1
    public function bRPopLPush(string $source, string $destination, int $timeout)
28
    {
29
        return $this->tryCatch(function () use ($source, $destination, $timeout) {
30 1
            return $this->client->brpoplpush($source, $destination, $timeout);
31 1
        });
32
    }
33
34 1
    public function hGet(string $key, string $field)
35
    {
36
        return $this->tryCatch(function () use ($key, $field) {
37 1
            return $this->client->hget($key, $field);
38 1
        });
39
    }
40
41 1
    public function sAdd(string $key, array $members)
42
    {
43
        $this->tryCatch(function () use ($key, $members) {
44 1
            $this->client->sadd($key, $members);
45 1
        });
46 1
    }
47
48 1
    public function sRem(string $key, array $members)
49
    {
50
        $this->tryCatch(function () use ($key, $members) {
51 1
            $this->client->srem($key, $members);
52 1
        });
53 1
    }
54
55 1
    public function sIsMember(string $key, string $value): bool
56
    {
57
        return $this->tryCatch(function () use ($key, $value) {
58 1
            return $this->client->sismember($key, $value);
59 1
        });
60
    }
61
62 1
    public function sMembers(string $key): array
63
    {
64
        return $this->tryCatch(function () use ($key) {
65 1
            return $this->client->smembers($key);
66 1
        });
67
    }
68
69 1
    public function lLen(string $key): int
70
    {
71
        return $this->tryCatch(function () use ($key) {
72 1
            return $this->client->llen($key);
73 1
        });
74
    }
75
76 1
    public function lRange(string $key, int $offset = 0, int $limit = 10): array
77
    {
78
        return $this->tryCatch(function () use ($key, $offset, $limit) {
79 1
            return $this->client->lrange($key, $offset, $limit);
80 1
        });
81
    }
82
83 1
    public function zScore(string $key, string $value)
84
    {
85
        $score = $this->tryCatch(function () use ($key, $value) {
86 1
            return $this->client->zscore($key, $value);
87 1
        });
88 1
        return $score === null ? $score : intval($score);
89
    }
90
91
    public function evalLua(string $lua, array $args)
92
    {
93
        return $this->tryCatch(function () use ($lua, $args) {
94
            return $this->client->eval($lua, 0, ...$args);
95
        });
96
    }
97
98 10
    private function tryCatch(callable $callable)
99
    {
100
        try {
101 10
            return $callable();
102 10
        } catch (ConnectionException $e) {
103 10
            throw new RedisException($e->getMessage(), 0, $e);
104
        }
105
    }
106
}
107