Completed
Push — master ( 9c3153...125483 )
by Avtandil
16s queued 14s
created

PhpRedisConnector::createRedisArrayInstance()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 36

Duplication

Lines 4
Ratio 11.11 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

Changes 0
Metric Value
cc 5
nc 9
nop 2
dl 4
loc 36
ccs 12
cts 13
cp 0.9231
crap 5.0113
rs 9.0328
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Longman\LaravelLodash\Redis\Connectors;
6
7
use Illuminate\Redis\Connections\PhpRedisClusterConnection;
8
use Illuminate\Redis\Connectors\PhpRedisConnector as BasePhpRedisConnector;
9
use Illuminate\Support\Arr;
10
use InvalidArgumentException;
11
use Redis;
12
use RedisArray;
13
14
class PhpRedisConnector extends BasePhpRedisConnector
15
{
16
    /**
17
     * Create the Redis client instance.
18
     *
19
     * @param  array $config
20
     * @return \Redis
21
     */
22 1
    protected function createClient(array $config)
23
    {
24
        return tap(new Redis(), function (Redis $client) use ($config) {
25 1
            $this->establishConnection($client, $config);
26
27 1
            if (! empty($config['password'])) {
28
                $client->auth((string) $config['password']);
29
            }
30
31 1
            if (! empty($config['database'])) {
32 1
                $client->select((int) $config['database']);
33
            }
34
35 1
            if (! empty($config['prefix'])) {
36 1
                $client->setOption(Redis::OPT_PREFIX, (string) $config['prefix']);
37
            }
38
39 1
            if (! empty($config['read_timeout'])) {
40 1
                $client->setOption(Redis::OPT_READ_TIMEOUT, (string) $config['read_timeout']);
41
            }
42
43 1 View Code Duplication
            if (! empty($config['serializer'])) {
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...
44 1
                $serializer = $this->getSerializerFromConfig($config['serializer']);
45 1
                $client->setOption(Redis::OPT_SERIALIZER, (string) $serializer);
46
            }
47
48 1
            if (empty($config['scan'])) {
49 1
                $client->setOption(Redis::OPT_SCAN, (string) Redis::SCAN_RETRY);
50
            }
51 1
        });
52
    }
53
54
    /**
55
     * Create a new clustered PhpRedis connection.
56
     *
57
     * @param  array $config
58
     * @param  array $clusterOptions
59
     * @param  array $options
60
     * @return \Illuminate\Redis\Connections\PhpRedisClusterConnection
61
     */
62 1
    public function connectToCluster(array $config, array $clusterOptions, array $options)
63
    {
64 1
        $options = array_merge($options, $clusterOptions, Arr::pull($config, 'options', []));
65
66
        // Use native Redis clustering
67 1
        if (Arr::get($options, 'cluster') === 'redis') {
68
            return new PhpRedisClusterConnection($this->createRedisClusterInstance(
69
                array_map([$this, 'buildClusterConnectionString'], $config),
70
                $options
71
            ));
72
        }
73
74
        // Use client-side sharding
75 1
        return new PhpRedisClusterConnection($this->createRedisArrayInstance(
0 ignored issues
show
Documentation introduced by
$this->createRedisArrayI...'), $config), $options) is of type object<RedisArray>, but the function expects a object<Redis>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76 1
            array_map([$this, 'buildRedisArrayConnectionString'], $config),
77 1
            $options
78
        ));
79
    }
80
81
    /**
82
     * Build a PhpRedis hosts array.
83
     *
84
     * @param  array $server
85
     * @return string
86
     */
87 1
    protected function buildRedisArrayConnectionString(array $server)
88
    {
89 1
        return $server['host'] . ':' . $server['port'];
90
    }
91
92
    /**
93
     * Create a new redis array instance.
94
     *
95
     * @param  array $servers
96
     * @param  array $options
97
     * @return \RedisArray
98
     */
99 1
    protected function createRedisArrayInstance(array $servers, array $options)
100
    {
101 1
        $client = new RedisArray($servers, Arr::only($options, [
102 1
            'function',
103
            'previous',
104
            'retry_interval',
105
            'lazy_connect',
106
            'connect_timeout',
107
            'read_timeout',
108
            'algorithm',
109
            'consistent',
110
            'distributor',
111
        ]));
112
113 1
        if (! empty($options['password'])) {
114
            // @TODO: Remove after this will be implemented
115
            // https://github.com/phpredis/phpredis/issues/1508
116
            throw new InvalidArgumentException('RedisArray does not support authorization');
117
            //$client->auth((string) $options['password']);
118
        }
119
120 1
        if (! empty($options['database'])) {
121 1
            $client->select((int) $options['database']);
122
        }
123
124 1
        if (! empty($options['prefix'])) {
125 1
            $client->setOption(Redis::OPT_PREFIX, (string) $options['prefix']);
126
        }
127
128 1 View Code Duplication
        if (! empty($options['serializer'])) {
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...
129 1
            $serializer = $this->getSerializerFromConfig($options['serializer']);
130 1
            $client->setOption(Redis::OPT_SERIALIZER, (string) $serializer);
131
        }
132
133 1
        return $client;
134
    }
135
136
    /**
137
     * Resolve serializer
138
     *
139
     * @param  string $serializer
140
     * @return int
141
     * @throws \InvalidArgumentException
142
     */
143 2
    protected function getSerializerFromConfig(string $serializer): int
144
    {
145 2
        switch ($serializer) {
146
            default:
147
                $flag = Redis::SERIALIZER_NONE;
148
                break;
149 2
            case 'igbinary':
150 2
                if (! defined('Redis::SERIALIZER_IGBINARY')) {
151
                    throw new InvalidArgumentException('Error: phpredis was not compiled with igbinary support!');
152
                }
153 2
                $flag = Redis::SERIALIZER_IGBINARY;
154 2
                break;
155
            case 'php':
156
                $flag = Redis::SERIALIZER_PHP;
157
                break;
158
        }
159
160 2
        return $flag;
161
    }
162
}
163