Passed
Push — develop ( cefedc...7e0ea6 )
by Stan
07:15
created

Summary   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
dl 0
loc 68
ccs 17
cts 19
cp 0.8947
rs 10
c 5
b 0
f 0
eloc 21
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A type() 0 3 1
A quantiles() 0 3 1
A __construct() 0 17 6
A track() 0 3 1
A observe() 0 5 1
1
<?php
2
3
namespace Krenor\Prometheus\Metrics;
4
5
use Tightenco\Collect\Support\Collection;
6
use Krenor\Prometheus\Exceptions\LabelException;
7
use Krenor\Prometheus\Contracts\Types\Observable;
8
use Krenor\Prometheus\Exceptions\PrometheusException;
9
use Krenor\Prometheus\Metrics\Concerns\TracksExecutionTime;
10
11
abstract class Summary extends Metric implements Observable
12
{
13
    use TracksExecutionTime;
14
15
    protected array $quantiles = [
16
        .01,
17
        .05,
18
        .5,
19
        .9,
20
        .99,
21
    ];
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 13
    public function __construct()
27
    {
28 13
        parent::__construct();
29
30 13
        foreach ($this->labels as $label) {
31 11
            if (preg_match('/^quantile$/', $label)) {
32 1
                throw new LabelException('The label `quantile` is used internally to designate summary quantiles.');
33
            }
34
        }
35
36 12
        foreach ($this->quantiles as $quantile) {
37 12
            if ($quantile < 0 || $quantile > 1) {
38 1
                throw new PrometheusException('Quantiles have to be in the range between 0 and 1.');
39
            }
40
        }
41
42 11
        sort($this->quantiles);
43 11
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 4
    public function type(): string
49
    {
50 4
        return 'summary';
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     *
56
     * @return self
57
     */
58 4
    public function observe(float $value, array $labels = []): self
59
    {
60 4
        static::$storage->observe($this, $value, $labels);
61
62 4
        return $this;
63
    }
64
65
    /**
66
     * @return Collection
67
     */
68 8
    public function quantiles(): Collection
69
    {
70 8
        return new Collection($this->quantiles);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    protected function track(float $value, array $labels = []): void
77
    {
78
        $this->observe($value, $labels);
79
    }
80
}
81