Completed
Push — master ( 3a9f70...7b04fe )
by Philip
01:59
created

Redis   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A storeMeasurement() 0 3 1
A getMeasurements() 0 10 3
1
<?php
2
3
/*
4
 * This file is part of the PHPProm package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PHPProm\Storage;
13
14
class Redis implements StorageInterface {
15
16
    protected $redis;
17
18
    public function __construct($host, $password = null, $port = 6379, $prefix = 'PHPProm:', $dbIndex = null) {
19
        $this->redis = new \Redis();
20
        $this->redis->connect($host, $port);
21
        if ($password !== null) {
22
            $this->redis->auth($password);
23
        }
24
        if ($dbIndex !== null) {
25
            $this->redis->select($dbIndex);
26
        }
27
        $this->redis->setOption(\Redis::OPT_PREFIX, $prefix);
28
    }
29
30
    public function storeMeasurement($prefix, $key, $value) {
31
        $this->redis->set($prefix.':'.$key, $value);
32
    }
33
34
    public function getMeasurements($prefix, array $keys) {
35
        $measurements = [];
36
        $prefixedKeys = array_map(function($key) use ($prefix) {
37
            return $prefix.':'.$key;
38
        }, $keys);
39
        foreach ($this->redis->mget($prefixedKeys) as $i => $value) {
40
            $measurements[$keys[$i]] = $value !== false ? (float)$value : 'Nan';
41
        }
42
        return $measurements;
43
    }
44
}
45