Database::setUpdateSentinels()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace RedisSentinel\Laravel;
4
5
use Illuminate\Support\Arr;
6
use Predis\Client;
7
use Illuminate\Redis\Database as LaravelRedisDatabase;
8
use Predis\Connection\Aggregate\SentinelReplication;
9
use Predis\Connection\ConnectionInterface;
10
11
class Database extends LaravelRedisDatabase
12
{
13
    /**
14
     * Create an array of single connection or sentinel clients
15
     *
16
     * @param  array  $servers
17
     * @param  array  $options
18
     * @return array
19
     */
20
    protected function createSingleClients(array $servers, array $options = [])
21
    {
22
        $clients = [];
23
24
        foreach ($servers as $key => $server) {
25
            $options = array_merge($options, Arr::pull($server, 'options'));
26
27
            $clients[$key] = new Client($server, $options);
28
29
            $this->setUpdateSentinels(($clients[$key])->getConnection(), $options);
30
        }
31
32
        return $clients;
33
    }
34
35
36
    /**
37
     * Sets the update sentinels flag on the connection if configured and possible.
38
     *
39
     * @param $connection ConnectionInterface
40
     * @param $options array
41
     */
42
    private function setUpdateSentinels($connection, $options)
43
    {
44
        if (isset($options['update_sentinels'])
45
                && boolval($options['update_sentinels']) === true
46
                && $connection instanceof SentinelReplication) {
47
48
            // is not defined on the ConnectionInterface so make sure we've got the right thing.
49
            $connection->setUpdateSentinels(true);
50
        }
51
    }
52
}
53