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

ConfigTracerFactoryProvider::getTracerFactory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
cc 2
nc 2
nop 1
crap 2.032
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