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

TelemetryConfig::getDriverConfig()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.1502

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 21
rs 9.6111
c 0
b 0
f 0
ccs 9
cts 11
cp 0.8182
cc 5
nc 4
nop 1
crap 5.1502
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Telemetry\Config;
6
7
use Spiral\Core\Container\Autowire;
8
use Spiral\Core\InjectableConfig;
9
use Spiral\Telemetry\Exception\InvalidArgumentException;
10
use Spiral\Telemetry\TracerFactoryInterface;
11
12
final class TelemetryConfig extends InjectableConfig
13
{
14
    public const CONFIG = 'telemetry';
15
16
    protected array $config = [
17
        'default' => 'null',
18
        'drivers' => [],
19
    ];
20
21
    /**
22
     * Get default trace driver
23
     *
24
     * @throws InvalidArgumentException
25
     */
26 263
    public function getDefaultDriver(): string
27
    {
28 263
        if (!\is_string($this->config['default'])) {
29
            throw new InvalidArgumentException('Default trace driver config value must be a string');
30
        }
31
32 263
        return $this->config['default'];
33
    }
34
35
    /**
36
     * @param non-empty-string $name
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
37
     * @return class-string<TracerFactoryInterface>|Autowire|TracerFactoryInterface
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<TracerFacto...|TracerFactoryInterface at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<TracerFactoryInterface>|Autowire|TracerFactoryInterface.
Loading history...
38
     * @throws InvalidArgumentException
39
     */
40 266
    public function getDriverConfig(string $name): string|Autowire|TracerFactoryInterface
41
    {
42 266
        if (!isset($this->config['drivers'][$name])) {
43 1
            throw new InvalidArgumentException(
44 1
                \sprintf('Config for telemetry driver `%s` is not defined.', $name)
45
            );
46
        }
47
48 265
        $driver = $this->config['drivers'][$name];
49
50 265
        if ($driver instanceof TracerFactoryInterface) {
51 1
            return $driver;
52
        }
53
54 264
        if (!\is_string($driver) && !$driver instanceof Autowire) {
55
            throw new InvalidArgumentException(
56
                \sprintf('Trace type value for `%s` must be a string or %s', $name, Autowire::class)
57
            );
58
        }
59
60 264
        return $driver;
61
    }
62
}
63