Passed
Push — main ( 72c551...3e794e )
by Fractal
02:18
created

MetricalHelper::getFirstOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
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