Test Failed
Pull Request — master (#816)
by butschster
06:13 queued 32s
created

TelemetryBootloader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 18
c 1
b 0
f 0
dl 0
loc 51
rs 10

5 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
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\ConfigTracerProvider;
16
use Spiral\Telemetry\Exception\TracerException;
17
use Spiral\Telemetry\LogTracer;
18
use Spiral\Telemetry\NullTracer;
19
use Spiral\Telemetry\TracerFactory;
20
use Spiral\Telemetry\TracerFactoryInterface;
21
use Spiral\Telemetry\TracerInterface;
22
use Spiral\Telemetry\TracerProviderInterface;
23
24
final class TelemetryBootloader extends Bootloader
25
{
26
    protected const SINGLETONS = [
27
        TracerFactoryInterface::class => TracerFactory::class,
28
        TracerProviderInterface::class => ConfigTracerProvider::class,
29
        ClockInterface::class => SystemClock::class,
30
    ];
31
32
    protected const BINDINGS = [
33
        TracerInterface::class => [self::class, 'getTracer'],
34
    ];
35
36
    public function __construct(
37
        private readonly ConfiguratorInterface $config
38
    ) {
39
    }
40
41
    public function init(EnvironmentInterface $env): void
42
    {
43
        $this->initConfig($env);
44
    }
45
46
    /**
47
     * @param class-string<TracerInterface>|Autowire $driver
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<TracerInterface>|Autowire at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<TracerInterface>|Autowire.
Loading history...
48
     */
49
    public function registerTracer(string $name, string|Autowire $driver): void
50
    {
51
        $this->config->modify(
52
            TelemetryConfig::CONFIG,
53
            new Append('drivers', $name, $driver)
54
        );
55
    }
56
57
    /**
58
     * @throws TracerException
59
     */
60
    public function getTracer(
61
        TracerProviderInterface $tracerProvider
62
    ): TracerInterface {
63
        return $tracerProvider->getTracer();
64
    }
65
66
    private function initConfig(EnvironmentInterface $env): void
67
    {
68
        $this->config->setDefaults(
69
            TelemetryConfig::CONFIG,
70
            [
71
                'default' => $env->get('TELEMETRY_DRIVER', 'null'),
72
                'drivers' => [
73
                    'null' => NullTracer::class,
74
                    'log' => LogTracer::class,
75
                ],
76
            ]
77
        );
78
    }
79
}
80