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

PrometheusExport::getExport()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 6
nc 2
nop 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
}