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

ConfigTracerProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 9 2
A getTracer() 0 9 2
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Telemetry;
6
7
use Spiral\Core\Container\Autowire;
8
use Spiral\Core\FactoryInterface;
9
use Spiral\Telemetry\Config\TelemetryConfig;
10
11
final class ConfigTracerProvider implements TracerProviderInterface
12
{
13
    /** @var TracerInterface[] */
14
    private array $drivers = [];
15
16
    public function __construct(
17
        private readonly TelemetryConfig $config,
18
        private readonly FactoryInterface $factory
19
    ) {
20
    }
21
22
    public function getTracer(?string $name = null): TracerInterface
23
    {
24
        $name ??= $this->config->getDefaultDriver();
25
26
        if (isset($this->drivers[$name])) {
27
            return $this->drivers[$name];
28
        }
29
30
        return $this->drivers[$name] = $this->resolve($name);
31
    }
32
33
    private function resolve(string $name): TracerInterface
34
    {
35
        $config = $this->config->geDriverConfig($name);
36
37
        if ($config instanceof Autowire) {
38
            return $config->resolve($this->factory);
39
        }
40
41
        return $this->factory->make($config);
42
    }
43
}
44