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

PredisConnector::setSentinelConnectionOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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