Completed
Push — 2.x ( 406d2e )
by Cy
02:17
created

VersionedRedisSentinelManager::resolveConnection()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 8.9197
cc 4
eloc 11
nc 6
nop 1
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
        $options = Arr::get($this->config, 'options', [ ]);
39
40
        if ($name == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $name of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
41
            $name = 'default';
42
        }
43
44
        if (isset($this->config[$name])) {
45
            return $this->connector()->connect($this->config[$name], $options);
46
        }
47
48
        if (isset($this->config['clusters']['name'])) {
49
            throw new InvalidArgumentException(
50
                'Redis Sentinel connections do not support Redis Cluster.'
51
            );
52
        }
53
54
        throw new InvalidArgumentException(
55
            'The Redis Sentinel connection [' . $name . '] is not defined.'
56
        );
57
    }
58
59
    /**
60
     * Get the appropriate Connector instance for the current client driver
61
     *
62
     * @return Connectors\PredisConnector The Connector instance for the
63
     * current driver
64
     *
65
     * @throws InvalidArgumentException If the current client driver is not
66
     * supported
67
     */
68
    protected function connector()
69
    {
70
        switch ($this->driver) {
71
            case 'predis':
72
                return new Connectors\PredisConnector();
73
        }
74
75
        throw new InvalidArgumentException(
76
            'Unsupported Redis Sentinel client driver [' . $this->driver . ']. '
77
            . 'The monospice/laravel-redis-sentinel-drivers package currently '
78
            . 'supports only the "predis" client. Support for the "phpredis" '
79
            . 'client will be added in the future.'
80
        );
81
    }
82
}
83