Completed
Branch 2.0.x (0e0238)
by Cy
05:42
created

RedisSentinelManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 55
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 18 3
A connector() 0 13 2
1
<?php
2
3
namespace Monospice\LaravelRedisSentinel;
4
5
use Illuminate\Redis\RedisManager;
6
use Illuminate\Support\Arr;
7
use InvalidArgumentException;
8
use Monospice\LaravelRedisSentinel\Connectors;
9
10
/**
11
 * Enables Laravel's Redis database driver to accept configuration options for
12
 * Redis Sentinel connections independently.
13
 *
14
 * By default, Laravel's Redis service permits a single set of configuration
15
 * options for all of the Redis connections passed to the Predis client. This
16
 * prevents us from declaring separate parameters for individual Redis services
17
 * managed by Sentinel. For example, we may wish to connect to a separate Redis
18
 * Sentinel service cluster set, or use a separate Redis database, for caching
19
 * queues, and sessions. This wrapper class enables us to declare parameters
20
 * for each connection in the "redis-sentinel" block of the database
21
 * configuration which it will use to configure individual clients.
22
 *
23
 * @category Package
24
 * @package  Monospice\LaravelRedisSentinel
25
 * @author   Cy Rossignol <[email protected]>
26
 * @license  See LICENSE file
27
 * @link     http://github.com/monospice/laravel-redis-sentinel-driver
28
 */
29
class RedisSentinelManager extends RedisManager
30
{
31
    /**
32
     * Get the Redis Connection instance represented by the specified name
33
     *
34
     * @param string $name The name of the connection as defined in the
35
     * configuration
36
     *
37
     * @return \Illuminate\Redis\Connections\Connection The configured Redis
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Illuminate\Redis\Connections\PredisConnection.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
38
     * Connection instance
39
     *
40
     * @throws InvalidArgumentException If attempting to initialize a Redis
41
     * Cluster connection
42
     * @throws InvalidArgumentException If the specified connection is not
43
     * defined in the configuration
44
     */
45
    protected function resolve($name)
46
    {
47
        $options = Arr::get($this->config, 'options', [ ]);
48
49
        if (isset($this->config[$name])) {
50
            return $this->connector()->connect($this->config[$name], $options);
51
        }
52
53
        if (isset($this->config['clusters']['name'])) {
54
            throw new InvalidArgumentException(
55
                'Redis Sentinel connections do not support Redis Cluster.'
56
            );
57
        }
58
59
        throw new InvalidArgumentException(
60
            "The Redis Sentinel connection [$name] is not defined."
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $name instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
61
        );
62
    }
63
64
    /**
65
     * Get the appropriate Connector instance for the current client driver
66
     *
67
     * @return Connectors\PredisConnector The Connector instance for the
68
     * current driver
69
     */
70
    protected function connector()
71
    {
72
        switch ($this->driver) {
73
            case 'predis':
74
                return new Connectors\PredisConnector();
75
        }
76
77
        throw new InvalidArgumentException(
78
            'The monospice/laravel-redis-sentinel-drivers package currently '
79
            . 'supports only the "predis" client. Support for the "phpredis" '
80
            . 'client will be added in the future.'
81
        );
82
    }
83
}
84