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

ConfigTracerFactoryProvider::resolve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 13
rs 10
c 0
b 0
f 0
ccs 5
cts 7
cp 0.7143
cc 3
nc 3
nop 1
crap 3.2098
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