Completed
Push — master ( 3c7a07...1bc614 )
by Philip
01:56
created

PrometheusExport   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeader() 0 7 2
A getMetric() 0 8 1
A getExport() 0 8 2
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;
13
14
use PHPProm\Storage\AbstractStorage;
15
16
class PrometheusExport {
17
18
    protected function getHeader($type, $metric, $label) {
19
        $header = '';
20
        if ($label !== null) {
21
            $header .= '# '.$type.' '.$metric.' '.$label."\n";
22
        }
23
        return $header;
24
    }
25
26
    public function getMetric($metric, $label, array $labelsToValues, $help = null, $type = null) {
27
        $result  = $this->getHeader('HELP', $metric, $help);
28
        $result .= $this->getHeader('TYPE', $metric, $type);
29
        $result .= implode("\n", array_map(function($value, $labelValue) use ($metric, $label) {
30
            return $metric.'{'.$label.'="'.$labelValue.'"} '.$value;
31
        }, $labelsToValues, array_keys($labelsToValues)));
32
        return $result."\n";
33
    }
34
35
    public function getExport(AbstractStorage $storage, $keys) {
36
        $export = '';
37
        foreach ($storage->getAvailableMetrics() as $availableMetric) {
38
            $measurements = $storage->getMeasurements($availableMetric['storagePrefix'], $keys, $availableMetric['defaultValue']);
39
            $export .= $this->getMetric($availableMetric['metric'], $availableMetric['label'], $measurements, $availableMetric['help'], $availableMetric['type']);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
40
        }
41
        return $export;
42
    }
43
44
}