Completed
Push — master ( d0c6f2...e28c79 )
by Avtandil
03:11
created

PhpRedisConnector::createClient()   D

Complexity

Conditions 9
Paths 1

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 23
nc 1
nop 1
dl 0
loc 39
ccs 0
cts 33
cp 0
crap 90
rs 4.909
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\Connectors\PhpRedisConnector as BasePhpRedisConnector;
15
use Redis;
16
17
class PhpRedisConnector extends BasePhpRedisConnector
18
{
19
    /**
20
     * Create the Redis client instance.
21
     *
22
     * @param  array $config
23
     * @return \Redis
24
     */
25
    protected function createClient(array $config)
26
    {
27
        return tap(new Redis, function ($client) use ($config) {
28
            $this->establishConnection($client, $config);
29
30
            if (! empty($config['password'])) {
31
                $client->auth($config['password']);
32
            }
33
34
            if (! empty($config['database'])) {
35
                $client->select($config['database']);
36
            }
37
38
            if (! empty($config['prefix'])) {
39
                $client->setOption(Redis::OPT_PREFIX, $config['prefix']);
40
            }
41
42
            if (! empty($config['read_timeout'])) {
43
                $client->setOption(Redis::OPT_READ_TIMEOUT, $config['read_timeout']);
44
            }
45
46
            if (! empty($config['serializer'])) {
47
                $serializer = 0;
48
                switch ($config['serializer']) {
49
                    case 'igbinary':
50
                        $serializer = Redis::SERIALIZER_IGBINARY;
51
                        break;
52
                    case 'php':
53
                        $serializer = Redis::SERIALIZER_PHP;
54
                        break;
55
                }
56
                $client->setOption(Redis::OPT_SERIALIZER, (string) $serializer);
57
            }
58
59
            if (! empty($config['scan'])) {
60
                $client->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
61
            }
62
        });
63
    }
64
}
65