Conditions | 15 |
Paths | 49 |
Total Lines | 35 |
Code Lines | 17 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 3 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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); |
||
41 | $nativeRedis->connect($host, $port, 0, null, 0, -1); |
||
|
|||
42 | } else { |
||
43 | // Altrimenti utilizzo host e port dalla configurazione della connessione standalone |
||
44 | $nativeRedis->connect($config['host'], $config['port'], 0, null, 0, -1); |
||
45 | } |
||
46 | |||
47 | // Autenticazione con username e password (se configurati) |
||
48 | if (array_key_exists('username', $config) && $config['username'] !== '' && array_key_exists('password', $config) && $config['password'] !== '') { |
||
49 | $nativeRedis->auth([$config['username'], $config['password']]); |
||
50 | } elseif (array_key_exists('password', $config) && $config['password'] !== '') { |
||
51 | $nativeRedis->auth($config['password']); // Per versioni Redis senza ACL |
||
52 | } |
||
53 | |||
54 | // Seleziono il database corretto (Per il cluster è sempre 0) |
||
55 | $database = (array_key_exists('database', $config) && $config['database'] !== '') ? (int) $config['database'] : 0; |
||
56 | $nativeRedis->select($database); |
||
57 | //$nativeRedis->setOption(\Redis::OPT_READ_TIMEOUT, -1); |
||
58 | |||
59 | return ['connection' => $nativeRedis, 'database' => $database]; |
||
60 | } |
||
68 |