Completed
Push — 2.x ( 341981...b9f40f )
by Cy
01:39
created

PredisConnector::makePredisClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 3
1
<?php
2
3
namespace Monospice\LaravelRedisSentinel\Connectors;
4
5
use Illuminate\Support\Arr;
6
use Monospice\LaravelRedisSentinel\Connections\PredisConnection;
7
use Predis\Client;
8
9
/**
10
 * Initializes Predis Client instances for Redis Sentinel connections
11
 *
12
 * @category Package
13
 * @package  Monospice\LaravelRedisSentinel
14
 * @author   Cy Rossignol <[email protected]>
15
 * @license  See LICENSE file
16
 * @link     http://github.com/monospice/laravel-redis-sentinel-drivers
17
 */
18
class PredisConnector
19
{
20
    /**
21
     * Configuration options specific to Sentinel connection operation
22
     *
23
     * We cannot pass these options as an array to the Predis client.
24
     * Instead, we'll set them on the connection directly using methods
25
     * provided by the SentinelReplication class of the Predis package.
26
     *
27
     * @var array
28
     */
29
    protected $sentinelKeys = [
30
        'sentinel_timeout' => null,
31
        'retry_wait' => null,
32
        'retry_limit' => null,
33
        'update_sentinels' => null,
34
    ];
35
36
    /**
37
     * Create a new Redis Sentinel connection from the provided configuration
38
     * options
39
     *
40
     * @param array $server  The client configuration for the connection
41
     * @param array $options The global client options shared by all Sentinel
42
     * connections
43
     *
44
     * @return PredisConnection The Sentinel connection containing a configured
45
     * Predis Client
46
     */
47
    public function connect(array $server, array $options = [ ])
48
    {
49
        // Merge the global options shared by all Sentinel connections with
50
        // connection-specific options
51
        $clientOpts = array_merge($options, Arr::pull($server, 'options', [ ]));
52
53
        // Automatically set "replication" to "sentinel". This is the Sentinel
54
        // driver, after all.
55
        $clientOpts['replication'] = 'sentinel';
56
57
        // Extract the array of Sentinel connection options from the rest of
58
        // the client options
59
        $sentinelOpts = array_intersect_key($clientOpts, $this->sentinelKeys);
60
61
        // Filter the Sentinel connection options elements from the client
62
        // options array
63
        $clientOpts = array_diff_key($clientOpts, $this->sentinelKeys);
64
65
        return new PredisConnection(
66
            new Client($server, $clientOpts),
67
            $sentinelOpts
68
        );
69
    }
70
}
71