Test Failed
Push — main ( d2d042...ea42f2 )
by Bingo
13:21
created

MetricsRegistry   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 24
c 1
b 0
f 0
dl 0
loc 67
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getTelemetryMeters() 0 3 1
A createDbMeter() 0 4 1
A getDbMeterByName() 0 6 2
A createMeter() 0 6 1
A markOccurrence() 0 10 4
A clearTelemetryMetrics() 0 4 2
A getDbMeters() 0 3 1
A markTelemetryOccurrence() 0 3 1
1
<?php
2
3
namespace Jabe\Engine\Impl\Metrics;
4
5
class MetricsRegistry
6
{
7
    protected $dbMeters = [];
8
    protected $telemetryMeters = [];
9
10
    public function getDbMeterByName(string $name): ?Meter
11
    {
12
        if (array_key_exists($key, $this->dbMeters)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $key seems to be never defined.
Loading history...
13
            return $this->dbMeters[$name];
14
        }
15
        return null;
16
    }
17
18
    public function getDbMeters(): array
19
    {
20
        return $this->dbMeters;
21
    }
22
23
    public function getTelemetryMeters(): array
24
    {
25
        return $this->telemetryMeters;
26
    }
27
28
    public function clearTelemetryMetrics(): void
29
    {
30
        foreach ($this->telemetryMeters as $name => $meter) {
31
            $meter->getAndClear();
32
        }
33
    }
34
35
    public function markOccurrence(string $name, int $times = 1, array $meters = null): void
36
    {
37
        if ($meters !== null) {
38
            $meters[$name]->markTimes($times);
39
        } else {
40
            if (array_key_exists($name, $this->dbMeters)) {
41
                $this->dbMeters[$name]->markTimes($times);
42
            }
43
            if (array_key_exists($name, $this->telemetryMeters)) {
44
                $this->telemetryMeters[$name]->markTimes($times);
45
            }
46
        }
47
    }
48
49
    public function markTelemetryOccurrence(string $name, int $times): void
50
    {
51
        $this->markOccurrence($this->telemetryMeters, $name, $times);
0 ignored issues
show
Bug introduced by
$this->telemetryMeters of type array is incompatible with the type string expected by parameter $name of Jabe\Engine\Impl\Metrics...istry::markOccurrence(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        $this->markOccurrence(/** @scrutinizer ignore-type */ $this->telemetryMeters, $name, $times);
Loading history...
Bug introduced by
$name of type string is incompatible with the type integer expected by parameter $times of Jabe\Engine\Impl\Metrics...istry::markOccurrence(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        $this->markOccurrence($this->telemetryMeters, /** @scrutinizer ignore-type */ $name, $times);
Loading history...
Bug introduced by
$times of type integer is incompatible with the type array|null expected by parameter $meters of Jabe\Engine\Impl\Metrics...istry::markOccurrence(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        $this->markOccurrence($this->telemetryMeters, $name, /** @scrutinizer ignore-type */ $times);
Loading history...
52
    }
53
54
    /**
55
     * Creates a meter for both database and telemetry collection.
56
     */
57
    public function createMeter(string $name): void
58
    {
59
        $dbMeter = new Meter($name);
60
        $this->dbMeters[$name] = $dbMeter;
61
        $telemetryMeter = new Meter($name);
62
        $this->telemetryMeters[$name] = $telemetryMeter;
63
    }
64
65
    /**
66
     * Creates a meter only for database collection.
67
     */
68
    public function createDbMeter(string $name): void
69
    {
70
        $dbMeter = new Meter($name);
71
        $$this->dbMeters[$name] = $dbMeter;
72
    }
73
}
74