Passed
Push — develop ( d53231...ea980c )
by Stan
02:32
created

Metric::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Krenor\Prometheus\Contracts;
4
5
use Tightenco\Collect\Support\Collection;
6
use Krenor\Prometheus\Exceptions\LabelException;
7
8
abstract class Metric
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $namespace;
14
15
    /**
16
     * @var string
17
     */
18
    protected $name;
19
20
    /**
21
     * @var string
22
     */
23
    protected $description;
24
25
    /**
26
     * @var string[]
27
     */
28
    protected $labels = [];
29
30
    /**
31
     * @var Storage
32
     */
33
    protected static $storage;
34
35
    /**
36
     * @return string
37
     */
38
    abstract public function type(): string;
39
40
    /**
41
     * Metric constructor.
42
     *
43
     * @throws LabelException
44
     */
45 56
    public function __construct()
46
    {
47 56
        foreach ($this->labels as $label) {
48 56
            if (!preg_match('/^(?![_]{2})[a-zA-Z_][a-zA-Z0-9_]*$/', $label)) {
49 56
                throw new LabelException("The label `{$label}` contains invalid characters.");
50
            }
51
        }
52 56
    }
53
54
    /**
55
     * @return string
56
     */
57 48
    final public function key(): string
58
    {
59 48
        return "{$this->namespace()}_{$this->name()}";
60
    }
61
62
    /**
63
     * @return string
64
     */
65 48
    public function namespace(): string
66
    {
67 48
        return $this->namespace;
68
    }
69
70
    /**
71
     * @return string
72
     */
73 48
    public function name(): string
74
    {
75 48
        return $this->name;
76
    }
77
78
    /**
79
     * @return string
80
     */
81 25
    public function description(): string
82
    {
83 25
        return $this->description;
84
    }
85
86
    /**
87
     * @return Collection
88
     */
89 51
    public function labels(): Collection
90
    {
91 51
        return new Collection($this->labels);
92
    }
93
94
    /**
95
     * @param Storage $storage
96
     */
97 25
    public static function storeUsing(Storage $storage): void
98
    {
99 25
        static::$storage = $storage;
100 25
    }
101
102
    /**
103
     * @return Storage
104
     */
105 25
    public static function storage(): Storage
106
    {
107 25
        return static::$storage;
108
    }
109
}
110