Passed
Push — main ( 7d8a2e...f5d7d4 )
by
unknown
04:29
created

RedisConnector::getNativeRedisConnection()   C

Complexity

Conditions 13
Paths 26

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 13
eloc 20
c 2
b 0
f 0
nc 26
nop 1
dl 0
loc 36
rs 6.6166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Padosoft\SuperCache;
4
5
use Illuminate\Support\Facades\Redis;
6
7
class RedisConnector
8
{
9
    protected $connection;
10
11
    public function __construct()
12
    {
13
        $this->connection = config('supercache.connection');
14
    }
15
16
    public function getRedisConnection(?string $connection_name = null)
17
    {
18
        return Redis::connection($connection_name ?? $this->connection);
19
    }
20
21
    /**
22
     * Establishes and returns a native Redis connection.
23
     * Questo metodo ritorna una cionnessione redis senza utilizzare il wrapper di Laravel.
24
     * La connessione nativa è necessaria per la sottoscrizione agli eventi (Es. psubscribe([...)) in quanto Laravel gestisce solo connessioni sync,
25
     * mentre per le sottoscrizioni è necessaria una connessione async
26
     *
27
     * @param  string|null $connection_name Optional. The name of the Redis connection to establish. If not provided, the default connection is used.
28
     * @return array       The Redis connection instance and database.
29
     */
30
    public function getNativeRedisConnection(?string $connection_name = null): array
31
    {
32
        $isCluster = config('database.redis.clusters.' . ($connection_name ?? 'default')) !== null ? true : false;
33
        if ($isCluster) {
34
            $config = config('database.redis.clusters.' . ($connection_name ?? 'default'));
35
            $url = $config[0]['host'] . ':' . $config[0]['port'];
36
            $nativeRedisCluster = new \RedisCluster(
37
                null, // Nome del cluster (può essere null)
38
                [$url], // Nodo master
39
                -1, // Timeout connessione
40
                -1, // Timeout lettura
41
                true, // Persistente
42
                ($config[0]['password'] !== null &&  $config[0]['password'] !== '' ? $config[0]['password'] : null)  // Password se necessaria
43
            );
44
45
            // Nel cluster c'è sempre un unico databse
46
            return ['connection' => $nativeRedisCluster, 'database' => 0];
47
        }
48
        // Crea una nuova connessione nativa Redis
49
        $nativeRedis = new \Redis();
50
        // Connessione al server Redis (no cluster)
51
        $config = config('database.redis.' . ($connection_name ?? 'default'));
52
        $nativeRedis->connect($config['host'], $config['port']);
53
54
        // Autenticazione con username e password (se configurati)
55
        if ($config['username'] !== null && $config['password'] !== null && $config['password'] !== '' && $config['username'] !== '') {
56
            $nativeRedis->auth([$config['username'], $config['password']]);
0 ignored issues
show
Bug introduced by
array($config['username'], $config['password']) of type array is incompatible with the type string expected by parameter $password of Redis::auth(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
            $nativeRedis->auth(/** @scrutinizer ignore-type */ [$config['username'], $config['password']]);
Loading history...
57
        } elseif ($config['password'] !== null && $config['password'] !== '') {
58
            $nativeRedis->auth($config['password']); // Per versioni Redis senza ACL
59
        }
60
61
        // Seleziono il database corretto
62
        $database = ($config['database'] !== null && $config['database'] !== '') ? (int) $config['database'] : 0;
63
        $nativeRedis->select($database);
64
65
        return ['connection' => $nativeRedis, 'database' => $database];
66
    }
67
68
    // Metodo per ottimizzare le operazioni Redis con pipeline
69
    public function pipeline($callback, ?string $connection_name = null)
70
    {
71
        return $this->getRedisConnection($connection_name)->pipeline($callback);
72
    }
73
}
74