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

TelemetryConfig::getDefaultDriver()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 1
b 0
f 0
ccs 3
cts 4
cp 0.75
cc 2
nc 2
nop 0
crap 2.0625
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