-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArrayRegistry.php
50 lines (38 loc) · 1.24 KB
/
ArrayRegistry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
declare(strict_types=1);
namespace Zlodes\PrometheusClient\Registry;
use Zlodes\PrometheusClient\Exception\MetricAlreadyRegisteredException;
use Zlodes\PrometheusClient\Exception\MetricHasWrongTypeException;
use Zlodes\PrometheusClient\Exception\MetricNotFoundException;
use Zlodes\PrometheusClient\Metric\Metric;
final class ArrayRegistry implements Registry
{
/** @var array<non-empty-string, Metric> */
private array $metrics = [];
/**
* @return $this
*
* @throws MetricAlreadyRegisteredException
*/
public function registerMetric(Metric $metric): self
{
$name = $metric->name;
if (array_key_exists($name, $this->metrics)) {
throw new MetricAlreadyRegisteredException($name);
}
$this->metrics[$name] = $metric;
return $this;
}
public function getAll(): array
{
return $this->metrics;
}
public function getMetric(string $name, string $class): Metric
{
$metric = $this->metrics[$name] ?? throw new MetricNotFoundException("Metric $name is not registered");
if (is_a($metric, $class) === false) {
throw new MetricHasWrongTypeException($class, $metric::class);
}
return $metric;
}
}