Passed
Pull Request — master (#816)
by butschster
06:28
created

TelemetryBootloader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 19
dl 0
loc 60
rs 10
c 1
b 0
f 0
ccs 13
cts 13
cp 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 3 1
A registerTracer() 0 5 1
A __construct() 0 3 1
A getTracer() 0 4 1
A initConfig() 0 9 1
A initFactory() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Telemetry\Bootloader;
6
7
use Spiral\Boot\Bootloader\Bootloader;
8
use Spiral\Boot\EnvironmentInterface;
9
use Spiral\Config\ConfiguratorInterface;
10
use Spiral\Config\Patch\Append;
11
use Spiral\Core\Container\Autowire;
12
use Spiral\Telemetry\Clock\SystemClock;
13
use Spiral\Telemetry\ClockInterface;
14
use Spiral\Telemetry\Config\TelemetryConfig;
15
use Spiral\Telemetry\ConfigTracerFactoryProvider;
16
use Spiral\Telemetry\Exception\TracerException;
17
use Spiral\Telemetry\LogTracer;
18
use Spiral\Telemetry\LogTracerFactory;
19
use Spiral\Telemetry\NullTracer;
20
use Spiral\Telemetry\NullTracerFactory;
21
use Spiral\Telemetry\TracerFactoryInterface;
22
use Spiral\Telemetry\TracerInterface;
23
use Spiral\Telemetry\TracerFactoryProviderInterface;
24
25
final class TelemetryBootloader extends Bootloader
26
{
27
    protected const SINGLETONS = [
28
        TracerFactoryInterface::class => [self::class, 'initFactory'],
29
        TracerFactoryProviderInterface::class => ConfigTracerFactoryProvider::class,
30
        ClockInterface::class => SystemClock::class,
31
    ];
32
33
    protected const BINDINGS = [
34
        TracerInterface::class => [self::class, 'getTracer'],
35
    ];
36
37 260
    public function __construct(
38
        private readonly ConfiguratorInterface $config
39
    ) {
40
    }
41
42 260
    public function init(EnvironmentInterface $env): void
43
    {
44 260
        $this->initConfig($env);
45
    }
46
47
    /**
48
     * @param class-string<TracerFactoryInterface>|TracerFactoryInterface|Autowire $driver
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<TracerFacto...ctoryInterface|Autowire at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<TracerFactoryInterface>|TracerFactoryInterface|Autowire.
Loading history...
49
     */
50 1
    public function registerTracer(string $name, string|TracerFactoryInterface|Autowire $driver): void
51
    {
52 1
        $this->config->modify(
53
            TelemetryConfig::CONFIG,
54 1
            new Append('drivers', $name, $driver)
55
        );
56
    }
57
58
    /**
59
     * @throws TracerException
60
     */
61 260
    public function initFactory(
62
        TracerFactoryProviderInterface $tracerProvider
63
    ): TracerFactoryInterface {
64 260
        return $tracerProvider->getTracerFactory();
65
    }
66
67
    /**
68
     * @throws TracerException
69
     */
70 260
    public function getTracer(
71
        TracerFactoryInterface $tracerFactory
72
    ): TracerInterface {
73 260
        return $tracerFactory->make();
74
    }
75
76 260
    private function initConfig(EnvironmentInterface $env): void
77
    {
78 260
        $this->config->setDefaults(
79
            TelemetryConfig::CONFIG,
80
            [
81 260
                'default' => $env->get('TELEMETRY_DRIVER', 'null'),
82
                'drivers' => [
83
                    'null' => NullTracerFactory::class,
84
                    'log' => LogTracerFactory::class,
85
                ],
86
            ]
87
        );
88
    }
89
}
90