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

PredisAdapter::tryCatch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
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