Passed
Pull Request — master (#19)
by
unknown
02:56
created

YiiSentryConfig::getTracing()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 4
nop 0
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Sentry;
6
7
use InvalidArgumentException;
8
9
final class YiiSentryConfig
10
{
11 13
    public function __construct(protected array $config)
12
    {
13
    }
14
15
    /**
16
     * Check if a DSN was set in the config.
17
     */
18
    public function hasDsnSet(): bool
19
    {
20
        $config = $this->getUserConfig();
21
22
        return !empty($config['options']['dsn']);
23
    }
24
25
    /**
26
     * Retrieve the user configuration.
27
     */
28 13
    public function getUserConfig(): array
29
    {
30 13
        $config = $this->config;
31
32 13
        return empty($config) ? [] : $config;
33
    }
34
35 11
    public function getOptions(): array
36
    {
37 11
        return empty($this->config['options'])
38
            ? []
39 11
            : (is_array($this->config['options'])
40 11
                ? $this->config['options']
41 11
                : throw  new InvalidArgumentException('options must be an array'));
42
    }
43
44 13
    public function getTracing(): array
45
    {
46 13
        return empty($this->config['tracing'])
47 2
            ? []
48 11
            : (is_array($this->config['tracing'])
49 11
                ? $this->config['tracing']
50 13
                : throw  new InvalidArgumentException('tracing must be an array'));
51
    }
52
53 11
    public function getLogLevel(): ?string
54
    {
55 11
        return isset($this->config['log_level']) ? (string)$this->config['log_level'] : null;
56
    }
57
58 13
    public function getIntegrations(): array
59
    {
60 13
        return empty($this->config['integrations'])
61 13
            ? []
62
            : (is_array($this->config['integrations'])
63
                ? $this->config['integrations']
64 13
                : throw  new InvalidArgumentException('integrations must be an array'));
65
    }
66
67
    /**
68
     * Checks if the config is set in such a way that performance tracing could be enabled.
69
     *
70
     * Because of `traces_sampler` being dynamic we can never be 100% confident but that is also not important.
71
     */
72 13
    public function couldHavePerformanceTracingEnabled(): bool
73
    {
74 13
        $config = $this->getUserConfig();
75
76 13
        return !empty($config['options']['traces_sample_rate'])
77 13
            || !empty($config['options']['traces_sampler']);
78
    }
79
}
80