1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
7
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
8
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2024 Mykhailo Shtanko [email protected] |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please view the LICENSE.MD |
13
|
|
|
* file that was distributed with this source code. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace FRZB\Component\MetricsPower\Helper; |
17
|
|
|
|
18
|
|
|
use Fp\Collections\ArrayList; |
19
|
|
|
use FRZB\Component\MetricsPower\Attribute\LoggerOptions; |
20
|
|
|
use FRZB\Component\MetricsPower\Attribute\Metrical; |
21
|
|
|
use FRZB\Component\MetricsPower\Attribute\OptionsInterface; |
22
|
|
|
use FRZB\Component\MetricsPower\Attribute\PrometheusOptions; |
23
|
|
|
use FRZB\Component\MetricsPower\Traits\WithPrivateEmptyConstructor; |
24
|
|
|
use JetBrains\PhpStorm\Immutable; |
25
|
|
|
|
26
|
|
|
/** @internal */ |
27
|
|
|
#[Immutable] |
28
|
|
|
final class MetricalHelper |
29
|
|
|
{ |
30
|
|
|
use WithPrivateEmptyConstructor; |
31
|
|
|
|
32
|
|
|
public static function isMetrical(object|string $target): bool |
33
|
|
|
{ |
34
|
|
|
return AttributeHelper::hasAttribute($target, Metrical::class); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function getMetrical(object|string $target): array |
38
|
|
|
{ |
39
|
|
|
return AttributeHelper::getAttributes($target, Metrical::class); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function getFirstMetrical(object|string $target): ?Metrical |
43
|
|
|
{ |
44
|
|
|
return ArrayList::collect(self::getMetrical($target)) |
45
|
|
|
->firstElement() |
46
|
|
|
->get(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** @return array<OptionsInterface> */ |
50
|
|
|
public static function getOptions(object|string $target): array |
51
|
|
|
{ |
52
|
|
|
return ArrayList::collect(self::getMetrical($target)) |
53
|
|
|
->map(static fn (Metrical $metrical) => $metrical->options) |
54
|
|
|
->flatten() |
55
|
|
|
->toList(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function getFirstOptions(object|string $target): ?OptionsInterface |
59
|
|
|
{ |
60
|
|
|
return ArrayList::collect(self::getMetrical($target)) |
61
|
|
|
->map(static fn (Metrical $metrical) => $metrical->options) |
62
|
|
|
->flatten() |
63
|
|
|
->firstElement() |
64
|
|
|
->getOrElse(fn() => new LoggerOptions()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public static function getCounterName(PrometheusOptions $options): string |
68
|
|
|
{ |
69
|
|
|
return str_replace('-', '_', "{$options->topic}-{$options->name}"); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|