Passed
Push — master ( a6e6a6...57f60d )
by Aleksei
28:26 queued 14:41
created

TelemetryBootloader::getTracer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Telemetry\Bootloader;
6
7
use Psr\Container\ContainerInterface;
8
use Spiral\Boot\Bootloader\Bootloader;
9
use Spiral\Boot\EnvironmentInterface;
10
use Spiral\Config\ConfiguratorInterface;
11
use Spiral\Config\Patch\Append;
12
use Spiral\Core\BinderInterface;
13
use Spiral\Core\Config\Proxy;
14
use Spiral\Core\Container\Autowire;
15
use Spiral\Telemetry\Clock\SystemClock;
16
use Spiral\Telemetry\ClockInterface;
17
use Spiral\Telemetry\Config\TelemetryConfig;
18
use Spiral\Telemetry\ConfigTracerFactoryProvider;
19
use Spiral\Telemetry\Exception\TracerException;
20
use Spiral\Telemetry\LogTracerFactory;
21
use Spiral\Telemetry\NullTracerFactory;
22
use Spiral\Telemetry\TracerFactoryInterface;
23
use Spiral\Telemetry\TracerFactoryProviderInterface;
24
use Spiral\Telemetry\TracerInterface;
25
26
final class TelemetryBootloader extends Bootloader
27
{
28
    protected const SINGLETONS = [
29
        TracerFactoryInterface::class => [self::class, 'initFactory'],
30
        TracerFactoryProviderInterface::class => ConfigTracerFactoryProvider::class,
31
        ClockInterface::class => SystemClock::class,
32
    ];
33
34 387
    public function __construct(
35
        private readonly ConfiguratorInterface $config,
36 387
    ) {}
37
38 387
    public function init(BinderInterface $binder, EnvironmentInterface $env): void
39
    {
40 387
        $binder->bind(TracerInterface::class, new Proxy(
41 387
            TracerInterface::class,
42 387
            false,
43 387
            static fn(ContainerInterface $container): TracerInterface => $container
44 387
                ->get(TracerFactoryInterface::class)
45 387
                ->make(),
46 387
        ));
47 387
        $this->initConfig($env);
48
    }
49
50
    /**
51
     * @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...
52
     */
53 1
    public function registerTracer(string $name, string|TracerFactoryInterface|Autowire $driver): void
54
    {
55 1
        $this->config->modify(
56 1
            TelemetryConfig::CONFIG,
57 1
            new Append('drivers', $name, $driver),
58 1
        );
59
    }
60
61
    /**
62
     * @throws TracerException
63
     */
64 66
    public function initFactory(
65
        TracerFactoryProviderInterface $tracerProvider,
66
    ): TracerFactoryInterface {
67 66
        return $tracerProvider->getTracerFactory();
68
    }
69
70 387
    private function initConfig(EnvironmentInterface $env): void
71
    {
72 387
        $this->config->setDefaults(
73 387
            TelemetryConfig::CONFIG,
74 387
            [
75 387
                'default' => $env->get('TELEMETRY_DRIVER', 'null'),
76 387
                'drivers' => [
77 387
                    'null' => NullTracerFactory::class,
78 387
                    'log' => LogTracerFactory::class,
79 387
                ],
80 387
            ],
81 387
        );
82
    }
83
}
84