Completed
Push — master ( 3faadf...57140e )
by Klaus
11:44
created

RedisAdapter::getConnectionParameters()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 17
rs 9.2
c 2
b 0
f 0
cc 4
eloc 11
nc 8
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 9 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
declare(strict_types=1);
3
4
namespace Linio\Component\Cache\Adapter;
5
6
use Linio\Component\Cache\Exception\KeyNotFoundException;
7
use Predis\Client;
8
9
class RedisAdapter extends AbstractAdapter implements AdapterInterface
10
{
11
    const EXPIRE_RESOLUTION_EX = 'ex';
12
    const EXPIRE_RESOLUTION_PX = 'px';
13
14
    /**
15
     * @var \Predis\Client
16
     */
17
    protected $client;
18
19
    /**
20
     * @var int
21
     */
22
    protected $ttl;
23
24
    /**
25
     * @var array
26
     */
27
    protected $config;
28
29
    public function __construct(array $config = [], bool $lazy = true)
30
    {
31
        $this->config = $config;
32
33
        if (!$lazy) {
34
            $this->getClient();
35
        }
36
    }
37
38
    protected function getClient(): Client
39
    {
40
        if (!$this->client instanceof Client) {
41
            $this->createClient($this->config);
42
        }
43
44
        return $this->client;
45
    }
46
47
    public function get(string $key)
1 ignored issue
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
48
    {
49
        $value = $this->getClient()->get($key);
50
51
        if ($value === null && !$this->getClient()->exists($key)) {
52
            throw new KeyNotFoundException();
53
        }
54
55
        return $value;
56
    }
57
58
    public function getMulti(array $keys): array
59
    {
60
        $result = $this->getClient()->mget($keys);
61
        $values = [];
62
63
        foreach ($keys as $index => $key) {
64
            if ($result[$index] !== null) {
65
                $values[$key] = $result[$index];
66
            }
67
        }
68
69
        return $values;
70
    }
71
72
    public function set(string $key, $value): bool
73
    {
74
        if ($this->ttl == 0) {
75
            $result = $this->getClient()->set($key, $value);
76
        } else {
77
            $result = $this->getClient()->set($key, $value, static::EXPIRE_RESOLUTION_EX, $this->ttl);
78
        }
79
80
        return $result->getPayload() == 'OK';
81
    }
82
83
    public function setMulti(array $data): bool
84
    {
85
        $responses = $this->getClient()->pipeline(
86
            function ($pipe) use ($data) {
87
                foreach ($data as $key => $value) {
88
                    if ($this->ttl == 0) {
89
                        $pipe->set($key, $value);
90
                    } else {
91
                        $pipe->set($key, $value, static::EXPIRE_RESOLUTION_EX, $this->ttl);
92
                    }
93
                }
94
            }
95
        );
96
97
        $result = true;
98
        foreach ($responses as $response) {
0 ignored issues
show
Bug introduced by
The expression $responses of type object<Predis\Pipeline\Pipeline>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
99
            /* @var $response \Predis\Response\Status */
100
            $result = $result && ($response->getPayload() == 'OK');
101
        }
102
103
        return $result;
104
    }
105
106
    public function contains(string $key): bool
107
    {
108
        return $this->getClient()->exists($key);
109
    }
110
111
    public function delete(string $key): bool
112
    {
113
        $this->getClient()->del($key);
114
115
        return true;
116
    }
117
118
    public function deleteMulti(array $keys): bool
119
    {
120
        foreach ($keys as $key) {
121
            $this->getClient()->del($key);
122
        }
123
124
        return true;
125
    }
126
127
    public function flush(): bool
128
    {
129
        $result = $this->getClient()->flushAll();
130
131
        return $result->getPayload() == 'OK';
132
    }
133
134
    public function setClient(Client $client)
135
    {
136
        $this->client = $client;
137
    }
138
139
    protected function createClient(array $config)
140
    {
141
        $this->client = new Client($this->getConnectionParameters($config), ['prefix' => null]);
142
143
        if (isset($config['ttl'])) {
144
            $this->ttl = (int) $config['ttl'];
145
        }
146
147
        if (isset($config['cache_not_found_keys'])) {
148
            $this->cacheNotFoundKeys = (bool) $config['cache_not_found_keys'];
149
        }
150
    }
151
152
    protected function getConnectionParameters(array $config): array
153
    {
154
        $connectionParameters = [];
155
        $connectionParameters['host'] = $config['host'] ?? '127.0.0.1';
156
        $connectionParameters['port'] = $config['port'] ?? 6379;
157
        if (isset($config['database'])) {
158
            $connectionParameters['database'] = $config['database'];
159
        }
160
        if (isset($config['password'])) {
161
            $connectionParameters['password'] = $config['password'];
162
        }
163
        if (isset($config['connection_persistent'])) {
164
            $connectionParameters['connection_persistent'] = $config['connection_persistent'];
165
        }
166
167
        return $connectionParameters;
168
    }
169
170
    public function setNamespace(string $namespace)
171
    {
172
        $this->getClient()->getOptions()->prefix->setPrefix($namespace . ':');
0 ignored issues
show
Bug introduced by
Accessing prefix on the interface Predis\Configuration\OptionsInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
173
        parent::setNamespace($namespace);
174
    }
175
176
    public function setTtl(int $ttl)
177
    {
178
        $this->ttl = $ttl;
179
    }
180
}
181