Completed
Pull Request — master (#46)
by Klaus
09:50
created

RedisAdapter   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 171
Duplicated Lines 16.37 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 4
dl 28
loc 171
rs 9.92
c 0
b 0
f 0

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
declare(strict_types=1);
4
5
namespace Linio\Component\Cache\Adapter;
6
7
use Linio\Component\Cache\Exception\KeyNotFoundException;
8
use Predis\Client;
9
10
class RedisAdapter extends AbstractAdapter implements AdapterInterface
11
{
12
    const EXPIRE_RESOLUTION_EX = 'ex';
13
    const EXPIRE_RESOLUTION_PX = 'px';
14
15
    protected ?Client $client;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '?', expecting T_FUNCTION or T_CONST
Loading history...
16
    protected int $ttl = 0;
17
    protected array $config;
18
19
    public function __construct(array $config = [], bool $lazy = true)
20
    {
21
        $this->config = $config;
22
23
        if (!$lazy) {
24
            $this->client = $this->getClient();
25
        }
26
    }
27
28
    protected function getClient(): Client
29
    {
30
        if (!$this->client) {
31
            $this->client = $this->createClient($this->config);
32
        }
33
34
        return $this->client;
35
    }
36
37
    /**
38
     * @return mixed
39
     */
40
    public function get(string $key)
41
    {
42
        $value = $this->getClient()->get($key);
43
44
        if (empty($value) && $this->getClient()->exists($key) == 0) {
45
            throw new KeyNotFoundException();
46
        }
47
48
        return $value;
49
    }
50
51
    public function getMulti(array $keys): array
52
    {
53
        $result = $this->getClient()->mget($keys);
54
        $values = [];
55
56
        foreach ($keys as $index => $key) {
57
            if ($result[$index] !== null) {
58
                $values[$key] = $result[$index];
59
            }
60
        }
61
62
        return $values;
63
    }
64
65
    /**
66
     * @param mixed $value
67
     */
68
    public function set(string $key, $value): bool
69
    {
70
        if ($this->ttl == 0) {
71
            $result = $this->getClient()->set($key, $value);
72
        } else {
73
            $result = $this->getClient()->set($key, $value, static::EXPIRE_RESOLUTION_EX, $this->ttl);
74
        }
75
76
        return $result->getPayload() == 'OK';
77
    }
78
79
    public function setMulti(array $data): bool
80
    {
81
        /** @var iterable $responses */
82
        $responses = $this->getClient()->pipeline(
83
            /** @var Client $pipe */
84
            function ($pipe) use ($data): void {
85
                foreach ($data as $key => $value) {
86
                    if ($this->ttl == 0) {
87
                        $pipe->set($key, $value);
88
                    } else {
89
                        $pipe->set($key, $value, static::EXPIRE_RESOLUTION_EX, $this->ttl);
90
                    }
91
                }
92
            }
93
        );
94
95
        $result = true;
96
        foreach ($responses as $response) {
97
            /** @var \Predis\Response\Status $response */
98
            $result = $result && ($response->getPayload() == 'OK');
99
        }
100
101
        return $result;
102
    }
103
104
    public function contains(string $key): bool
105
    {
106
        return (bool) $this->getClient()->exists($key);
107
    }
108
109
    public function delete(string $key): bool
110
    {
111
        $this->getClient()->del([$key]);
112
113
        return true;
114
    }
115
116
    public function deleteMulti(array $keys): bool
117
    {
118
        $this->getClient()->del($keys);
119
120
        return true;
121
    }
122
123
    public function flush(): bool
124
    {
125
        $result = $this->getClient()->flushAll();
126
127
        return $result->getPayload() == 'OK';
128
    }
129
130
    public function setClient(Client $client): void
131
    {
132
        $this->client = $client;
133
    }
134
135
    protected function createClient(array $config): Client
136
    {
137
        $this->client = new Client($this->getConnectionParameters($config), ['prefix' => null]);
138
139
        if (isset($config['ttl'])) {
140
            $this->ttl = (int) $config['ttl'];
141
        }
142
143
        if (isset($config['cache_not_found_keys'])) {
144
            $this->cacheNotFoundKeys = (bool) $config['cache_not_found_keys'];
145
        }
146
147
        return $this->client;
148
    }
149
150
    protected function getConnectionParameters(array $config): array
151
    {
152
        $connectionParameters = [];
153
        $connectionParameters['host'] = $config['host'] ?? '127.0.0.1';
154
        $connectionParameters['port'] = $config['port'] ?? 6379;
155
        if (isset($config['database'])) {
156
            $connectionParameters['database'] = $config['database'];
157
        }
158
        if (isset($config['password'])) {
159
            $connectionParameters['password'] = $config['password'];
160
        }
161
        if (isset($config['connection_persistent'])) {
162
            $connectionParameters['connection_persistent'] = $config['connection_persistent'];
163
        }
164
165
        return $connectionParameters;
166
    }
167
168
    public function setNamespace(string $namespace): void
169
    {
170
        $this->getClient()->getOptions()->prefix->setPrefix($namespace . ':');
171
        parent::setNamespace($namespace);
172
    }
173
174
    public function setTtl(int $ttl): void
175
    {
176
        $this->ttl = $ttl;
177
    }
178
}
179