Completed
Branch 2.x (1080d0)
by Cy
04:46
created

VersionedRedisSentinelManager   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 60
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveConnection() 0 19 4
A connector() 0 14 2
1
<?php
2
3
namespace Monospice\LaravelRedisSentinel\Manager;
4
5
use Illuminate\Redis\RedisManager;
6
use Illuminate\Support\Arr;
7
use InvalidArgumentException;
8
use Monospice\LaravelRedisSentinel\Connectors;
9
10
/**
11
 * Contains common functionality for the RedisSentinelManager implementations
12
 * for differing Laravel versions.
13
 *
14
 * @category Package
15
 * @package  Monospice\LaravelRedisSentinel
16
 * @author   Cy Rossignol <[email protected]>
17
 * @license  See LICENSE file
18
 * @link     http://github.com/monospice/laravel-redis-sentinel-drivers
19
 */
20
abstract class VersionedRedisSentinelManager extends RedisManager
21
{
22
    /**
23
     * Get the Redis Connection instance represented by the specified name
24
     *
25
     * @param string|null $name The name of the connection as defined in the
26
     * configuration
27
     *
28
     * @return \Illuminate\Redis\Connections\PredisConnection The configured
29
     * Redis Connection instance
30
     *
31
     * @throws InvalidArgumentException If attempting to initialize a Redis
32
     * Cluster connection
33
     * @throws InvalidArgumentException If the specified connection is not
34
     * defined in the configuration
35
     */
36
    protected function resolveConnection($name = null)
37
    {
38
        $name = $name ?: 'default';
39
        $options = Arr::get($this->config, 'options', [ ]);
40
41
        if (isset($this->config[$name])) {
42
            return $this->connector()->connect($this->config[$name], $options);
43
        }
44
45
        if (isset($this->config['clusters']['name'])) {
46
            throw new InvalidArgumentException(
47
                'Redis Sentinel connections do not support Redis Cluster.'
48
            );
49
        }
50
51
        throw new InvalidArgumentException(
52
            'The Redis Sentinel connection [' . $name . '] is not defined.'
53
        );
54
    }
55
56
    /**
57
     * Get the appropriate Connector instance for the current client driver
58
     *
59
     * @return Connectors\PredisConnector The Connector instance for the
60
     * current driver
61
     *
62
     * @throws InvalidArgumentException If the current client driver is not
63
     * supported
64
     */
65
    protected function connector()
66
    {
67
        switch ($this->driver) {
68
            case 'predis':
69
                return new Connectors\PredisConnector();
70
        }
71
72
        throw new InvalidArgumentException(
73
            'Unsupported Redis Sentinel client driver [' . $this->driver . ']. '
74
            . 'The monospice/laravel-redis-sentinel-drivers package currently '
75
            . 'supports only the "predis" client. Support for the "phpredis" '
76
            . 'client will be added in the future.'
77
        );
78
    }
79
}
80