Completed
Push — master ( 8f8133...fe009d )
by Avtandil
04:27 queued 02:45
created

PhpRedisConnector::getSerializerFromConfig()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.5726

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 19
ccs 7
cts 13
cp 0.5385
crap 5.5726
rs 9.6333
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 Illuminate\Support\Facades\Redis as RedisFacade;
11
use InvalidArgumentException;
12
use LogicException;
13
use Redis;
14
use RedisArray;
15
16
class PhpRedisConnector extends BasePhpRedisConnector
17
{
18
    /**
19
     * Create the Redis client instance.
20
     *
21
     * @param  array $config
22
     * @return \Redis
23
     */
24 1
    protected function createClient(array $config)
25
    {
26
        return tap(new Redis(), function (Redis $client) use ($config) {
27 1
            if ($client instanceof RedisFacade) {
28
                throw new LogicException(
29
                    'Please remove or rename the Redis facade alias in your "app" configuration file in order to avoid collision with the PHP Redis extension.'
30
                );
31
            }
32
33 1
            $this->establishConnection($client, $config);
34
35 1
            if (! empty($config['password'])) {
36
                $client->auth((string) $config['password']);
37
            }
38
39 1
            if (! empty($config['database'])) {
40 1
                $client->select((int) $config['database']);
41
            }
42
43 1
            if (! empty($config['prefix'])) {
44 1
                $client->setOption(Redis::OPT_PREFIX, (string) $config['prefix']);
45
            }
46
47 1
            if (! empty($config['read_timeout'])) {
48 1
                $client->setOption(Redis::OPT_READ_TIMEOUT, (string) $config['read_timeout']);
49
            }
50
51 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...
52 1
                $serializer = $this->getSerializerFromConfig($config['serializer']);
53 1
                $client->setOption(Redis::OPT_SERIALIZER, (string) $serializer);
54
            }
55
56 1
            if (empty($config['scan'])) {
57 1
                $client->setOption(Redis::OPT_SCAN, (string) Redis::SCAN_RETRY);
58
            }
59 1
        });
60
    }
61
62
    /**
63
     * Create a new clustered PhpRedis connection.
64
     *
65
     * @param  array $config
66
     * @param  array $clusterOptions
67
     * @param  array $options
68
     * @return \Illuminate\Redis\Connections\PhpRedisClusterConnection
69
     */
70 1
    public function connectToCluster(array $config, array $clusterOptions, array $options)
71
    {
72 1
        $options = array_merge($options, $clusterOptions, Arr::pull($config, 'options', []));
73
74
        // Use native Redis clustering
75 1
        if (Arr::get($options, 'cluster') === 'redis') {
76
            return new PhpRedisClusterConnection($this->createRedisClusterInstance(
77
                array_map([$this, 'buildClusterConnectionString'], $config),
78
                $options
79
            ));
80
        }
81
82
        // Use client-side sharding
83 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...
84 1
            array_map([$this, 'buildRedisArrayConnectionString'], $config),
85
            $options
86
        ));
87
    }
88
89
    /**
90
     * Build a PhpRedis hosts array.
91
     *
92
     * @param  array $server
93
     * @return string
94
     */
95 1
    protected function buildRedisArrayConnectionString(array $server)
96
    {
97 1
        return $server['host'] . ':' . $server['port'];
98
    }
99
100
    /**
101
     * Create a new redis array instance.
102
     *
103
     * @param  array $servers
104
     * @param  array $options
105
     * @return \RedisArray
106
     */
107 1
    protected function createRedisArrayInstance(array $servers, array $options)
108
    {
109 1
        $client = new RedisArray($servers, Arr::only($options, [
110 1
            'function',
111
            'previous',
112
            'retry_interval',
113
            'lazy_connect',
114
            'connect_timeout',
115
            'read_timeout',
116
            'algorithm',
117
            'consistent',
118
            'distributor',
119
        ]));
120
121 1
        if (! empty($options['password'])) {
122
            // @TODO: Remove after this will be implemented
123
            // https://github.com/phpredis/phpredis/issues/1508
124
            throw new InvalidArgumentException('RedisArray does not support authorization');
125
            //$client->auth((string) $options['password']);
126
        }
127
128 1
        if (! empty($options['database'])) {
129 1
            $client->select((int) $options['database']);
130
        }
131
132 1
        if (! empty($options['prefix'])) {
133 1
            $client->setOption(Redis::OPT_PREFIX, (string) $options['prefix']);
134
        }
135
136 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...
137 1
            $serializer = $this->getSerializerFromConfig($options['serializer']);
138 1
            $client->setOption(Redis::OPT_SERIALIZER, (string) $serializer);
139
        }
140
141 1
        return $client;
142
    }
143
144
    /**
145
     * Resolve serializer
146
     *
147
     * @param  string $serializer
148
     * @return int
149
     * @throws \InvalidArgumentException
150
     */
151 2
    protected function getSerializerFromConfig(string $serializer): int
152
    {
153 2
        switch ($serializer) {
154
            default:
155
                $flag = Redis::SERIALIZER_NONE;
156
                break;
157 2
            case 'igbinary':
158 2
                if (! defined('Redis::SERIALIZER_IGBINARY')) {
159
                    throw new InvalidArgumentException('Error: phpredis was not compiled with igbinary support!');
160
                }
161 2
                $flag = Redis::SERIALIZER_IGBINARY;
162 2
                break;
163
            case 'php':
164
                $flag = Redis::SERIALIZER_PHP;
165
                break;
166
        }
167
168 2
        return $flag;
169
    }
170
}
171