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

ConfigTracerFactoryProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 0
Metric Value
wmc 6
eloc 12
dl 0
loc 35
rs 10
c 0
b 0
f 0
ccs 10
cts 13
cp 0.7692

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 13 3
A getTracerFactory() 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 ConfigTracerFactoryProvider implements TracerFactoryProviderInterface
12
{
13
    /** @var TracerFactoryInterface[] */
14
    private array $drivers = [];
15
16 263
    public function __construct(
17
        private readonly TelemetryConfig $config,
18
        private readonly FactoryInterface $factory
19
    ) {
20
    }
21
22 263
    public function getTracerFactory(?string $name = null): TracerFactoryInterface
23
    {
24 263
        $name ??= $this->config->getDefaultDriver();
25
26 263
        if (isset($this->drivers[$name])) {
27
            return $this->drivers[$name];
28
        }
29
30 263
        return $this->drivers[$name] = $this->resolve($name);
31
    }
32
33 263
    private function resolve(string $name): TracerFactoryInterface
34
    {
35 263
        $config = $this->config->getDriverConfig($name);
36
37 262
        if ($config instanceof TracerFactoryInterface) {
38
            return $config;
39
        }
40
41 262
        if ($config instanceof Autowire) {
42
            return $config->resolve($this->factory);
43
        }
44
45 262
        return $this->factory->make($config);
46
    }
47
}
48