Completed
Push — master ( 8fe011...a16b04 )
by Kevin
02:06
created

RedisCollection   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A parseDsn() 0 18 3
A getChecks() 0 4 1
1
<?php
2
3
namespace Liip\MonitorBundle\Check;
4
5
use ZendDiagnostics\Check\CheckCollectionInterface;
6
use ZendDiagnostics\Check\Redis;
7
8
/**
9
 * @author Kevin Bond <[email protected]>
10
 */
11
class RedisCollection implements CheckCollectionInterface
12
{
13
    private $checks = [];
14
15
    public function __construct(array $configs)
16
    {
17
        foreach ($configs as $name => $config) {
18
            if (isset($config['dsn'])) {
19
                $this->parseDsn($config);
20
            }
21
22
            $check = new Redis($config['host'], $config['port'], $config['password']);
23
            $check->setLabel(\sprintf('Redis "%s"', $name));
24
25
            $this->checks[\sprintf('redis_%s', $name)] = $check;
26
        }
27
    }
28
29
    private function parseDsn(array &$config)
30
    {
31
        $config = \array_merge($config, \parse_url($config['dsn']));
32
33
        if (isset($config['pass'])) {
34
            $config['password'] = $config['pass'];
35
            // Cleanup
36
            unset($config['pass']);
37
        } elseif (isset($config['user'])) {
38
            /*
39
             * since "redis://my-super-secret-password@redis-host:6379" is a valid redis
40
             * dsn but \parse_url does not understand this notation and extracts the auth as user,
41
             * we need to check for it.
42
             */
43
            $config['password'] = $config['user'];
44
            unset($config['user']);
45
        }
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getChecks()
52
    {
53
        return $this->checks;
54
    }
55
}
56