Completed
Push — develop ( 28a887...d46380 )
by Stan
02:57
created

Summary::__construct()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

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