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

Memcached   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 28
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A storeMeasurement() 0 3 1
A getMeasurements() 0 11 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 Memcached implements StorageInterface {
15
16
    protected $memcached;
17
18
    protected $prefix;
19
20
    public function __construct($host, $port = 11211, $prefix = 'PHPProm:', $weight = 0) {
21
        $this->memcached = new \Memcached();
22
        $this->memcached->addServer($host, $port, $weight);
23
        $this->prefix = $prefix;
24
    }
25
26
    public function storeMeasurement($prefix, $key, $value) {
27
        $this->memcached->set($this->prefix.$prefix.':'.$key, $value);
28
    }
29
30
    public function getMeasurements($prefix, array $keys) {
31
        $measurements = [];
32
        $prefixedKeys = array_map(function($key) use ($prefix) {
33
            return $this->prefix.$prefix.':'.$key;
34
        }, $keys);
35
        foreach ($this->memcached->getMulti($prefixedKeys) as $key => $value) {
36
            $unprefixedKey = substr($key, strlen($this->prefix) + strlen($prefix) + 1);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 16 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
37
            $measurements[$unprefixedKey] = $value !== false ? (float)$value : 'Nan';
38
        }
39
        return $measurements;
40
    }
41
}
42