Passed
Pull Request — main (#6)
by
unknown
03:18
created

RedisConnector   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 20
c 4
b 0
f 0
dl 0
loc 57
rs 10
wmc 17

3 Methods

Rating   Name   Duplication   Size   Complexity  
A pipeline() 0 3 1
A getRedisConnection() 0 3 1
C getNativeRedisConnection() 0 33 15
1
<?php
2
3
namespace Padosoft\SuperCache;
4
5
use Illuminate\Support\Facades\Redis;
6
7
class RedisConnector
8
{
9
    public function getRedisConnection(?string $connection_name = null): \Illuminate\Redis\Connections\Connection
10
    {
11
        return Redis::connection($connection_name ?? config('supercache.connection'));
12
    }
13
14
    /**
15
     * Establishes a native Redis connection based on the provided connection name and optional host and port.
16
     *
17
     * @param  string|null $connection_name The name of the Redis connection configuration to use. Defaults to 'default'.
18
     * @param  string|null $host            The hostname to use for the connection. If not provided, it will be retrieved from the configuration.
19
     * @param  string|null $port            The port number to use for the connection. If not provided, it will be retrieved from the configuration.
20
     * @return array|null  Returns an associative array with the Redis connection instance and the selected database, or null on failure.
21
     *                     The array contains:
22
     *                     - 'connection': The instance of the native Redis connection.
23
     *                     - 'database': The selected database index.
24
     */
25
    public function getNativeRedisConnection(?string $connection_name = null, ?string $host = null, ?string $port = null): ?array
26
    {
27
        // Crea una nuova connessione nativa Redis
28
        $config = config('database.redis.clusters.' . ($connection_name ?? config('supercache.connection')));
29
        if ($config !== null && ($host === null || $port === null)) {
30
            // Sono nel caso del cluster, host e port sono obbligatori in quanto le connessioni vanno stabilite per ogni nodo del cluster
31
            throw new \RuntimeException('Host e port non possono essere null per le connessioni in cluster');
32
        }
33
        if ($config === null) {
34
            // Sono nel caso di una connessione standalone
35
            $config = config('database.redis.' . ($connection_name ?? config('supercache.connection')));
36
        }
37
        $nativeRedis = new \Redis();
38
        if ($host !== null && $port !== null) {
39
            // Se ho host e port (caso del cluster) uso questi
40
            $nativeRedis->connect($host, $port);
0 ignored issues
show
Bug introduced by
$port of type string is incompatible with the type integer expected by parameter $port of Redis::connect(). ( Ignorable by Annotation )

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

40
            $nativeRedis->connect($host, /** @scrutinizer ignore-type */ $port);
Loading history...
41
        } else {
42
            // Altrimenti utilizzo host e port dalla configurazione della connessione standalone
43
            $nativeRedis->connect($config['host'], $config['port']);
44
        }
45
46
        // Autenticazione con username e password (se configurati)
47
        if (array_key_exists('username', $config) && $config['username'] !== '' && array_key_exists('password', $config) && $config['password'] !== '') {
48
            $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

48
            $nativeRedis->auth(/** @scrutinizer ignore-type */ [$config['username'], $config['password']]);
Loading history...
49
        } elseif (array_key_exists('password', $config) && $config['password'] !== '') {
50
            $nativeRedis->auth($config['password']); // Per versioni Redis senza ACL
51
        }
52
53
        // Seleziono il database corretto (Per il cluster è sempre 0)
54
        $database = (array_key_exists('database', $config) && $config['database'] !== '') ? (int) $config['database'] : 0;
55
        $nativeRedis->select($database);
56
57
        return ['connection' => $nativeRedis, 'database' => $database];
58
    }
59
60
    // Metodo per ottimizzare le operazioni Redis con pipeline
61
    public function pipeline($callback, ?string $connection_name = null)
62
    {
63
        return $this->getRedisConnection(($connection_name ?? config('supercache.connection')))->pipeline($callback);
64
    }
65
}
66