Completed
Push — master ( ed1354...366677 )
by Avtandil
05:33
created

buildRedisArrayConnectionString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Laravel Lodash package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
declare(strict_types=1);
11
12
namespace Longman\LaravelLodash\Redis\Connectors;
13
14
use Illuminate\Redis\Connections\PhpRedisClusterConnection;
15
use Illuminate\Redis\Connectors\PhpRedisConnector as BasePhpRedisConnector;
16
use Illuminate\Support\Arr;
17
use InvalidArgumentException;
18
use Redis;
19
use RedisArray;
20
21
class PhpRedisConnector extends BasePhpRedisConnector
22
{
23
    /**
24
     * Create the Redis client instance.
25
     *
26
     * @param  array $config
27
     * @return \Redis
28
     */
29
    protected function createClient(array $config)
30
    {
31
        return tap(new Redis(), function (Redis $client) use ($config) {
32
            $this->establishConnection($client, $config);
33
34
            if (! empty($config['password'])) {
35
                $client->auth((string) $config['password']);
36
            }
37
38
            if (! empty($config['database'])) {
39
                $client->select((int) $config['database']);
40
            }
41
42
            if (! empty($config['prefix'])) {
43
                $client->setOption(Redis::OPT_PREFIX, (string) $config['prefix']);
44
            }
45
46
            if (! empty($config['read_timeout'])) {
47
                $client->setOption(Redis::OPT_READ_TIMEOUT, (string) $config['read_timeout']);
48
            }
49
50 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...
51
                $serializer = $this->getSerializerFromConfig($config['serializer']);
52
                $client->setOption(Redis::OPT_SERIALIZER, (string) $serializer);
53
            }
54
55
            if (! empty($config['scan'])) {
56
                $client->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
57
            }
58
        });
59
    }
60
61
    /**
62
     * Create a new clustered PhpRedis connection.
63
     *
64
     * @param  array $config
65
     * @param  array $clusterOptions
66
     * @param  array $options
67
     * @return \Illuminate\Redis\Connections\PhpRedisClusterConnection
68
     */
69
    public function connectToCluster(array $config, array $clusterOptions, array $options)
70
    {
71
        $options = array_merge($options, $clusterOptions, Arr::pull($config, 'options', []));
72
73
        // Use native Redis clustering
74
        if (Arr::get($options, 'cluster') === 'redis') {
75
            return new PhpRedisClusterConnection($this->createRedisClusterInstance(
76
                array_map([$this, 'buildClusterConnectionString'], $config),
77
                $options
78
            ));
79
        }
80
81
        // Use client-side sharding
82
        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...
83
            array_map([$this, 'buildRedisArrayConnectionString'], $config),
84
            $options
85
        ));
86
    }
87
88
    /**
89
     * Build a PhpRedis hosts array.
90
     *
91
     * @param  array $server
92
     * @return string
93
     */
94
    protected function buildRedisArrayConnectionString(array $server)
95
    {
96
        return $server['host'] . ':' . $server['port'];
97
    }
98
99
    /**
100
     * Create a new redis array instance.
101
     *
102
     * @param  array $servers
103
     * @param  array $options
104
     * @return \RedisArray
105
     */
106
    protected function createRedisArrayInstance(array $servers, array $options)
107
    {
108
        $client = new RedisArray($servers, Arr::only($options, [
109
            'function',
110
            'previous',
111
            'retry_interval',
112
            'lazy_connect',
113
            'connect_timeout',
114
            'read_timeout',
115
            'algorithm',
116
            'consistent',
117
            'distributor',
118
        ]));
119
120
        if (! empty($options['password'])) {
121
            // @TODO: Remove after this will be implemented
122
            // https://github.com/phpredis/phpredis/issues/1508
123
            throw new InvalidArgumentException('RedisArray does not support authorization');
124
            //$client->auth((string) $options['password']);
125
        }
126
127
        if (! empty($options['database'])) {
128
            $client->select((int) $options['database']);
129
        }
130
131
        if (! empty($options['prefix'])) {
132
            $client->setOption(Redis::OPT_PREFIX, (string) $options['prefix']);
133
        }
134
135 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...
136
            $serializer = $this->getSerializerFromConfig($options['serializer']);
137
            $client->setOption(Redis::OPT_SERIALIZER, (string) $serializer);
138
        }
139
140
        return $client;
141
    }
142
143
    protected function getSerializerFromConfig(string $serializer): int
144
    {
145
        switch ($serializer) {
146
            default:
147
                $flag = Redis::SERIALIZER_NONE;
148
                break;
149
            case 'igbinary':
150
                $flag = Redis::SERIALIZER_IGBINARY;
151
                break;
152
            case 'php':
153
                $flag = Redis::SERIALIZER_PHP;
154
                break;
155
        }
156
157
        return $flag;
158
    }
159
}
160