Test Failed
Pull Request — master (#19)
by
unknown
04:55 queued 02:17
created

YiiSentryConfig::getUserConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 5
rs 10
c 1
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
    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
    public function getUserConfig(): array
29
    {
30
        $config = $this->config;
31
32
        return empty($config) ? [] : $config;
33
    }
34
35
    public function getOptions(): array
36
    {
37
        return empty($this->config['options'])
38
            ? []
39
            : (is_array($this->config['options'])
40
                ? $this->config['options']
41
                : throw  new InvalidArgumentException('options must be an array'));
42
    }
43
44
    public function getTracing(): array
45
    {
46
        return empty($this->config['tracing'])
47
            ? []
48
            : (is_array($this->config['tracing'])
49
                ? $this->config['tracing']
50
                : throw  new InvalidArgumentException('tracing must be an array'));
51
    }
52
53
    public function getLogLevel(): ?string
54
    {
55
        return isset($this->config['log_level']) ? (string)$this->config['log_level'] : null;
56
    }
57
58
    public function getIntegrations(): array
59
    {
60
        return empty($this->config['integrations'])
61
            ? []
62
            : (is_array($this->config['integrations'])
63
                ? $this->config['integrations']
64
                : 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
    public function couldHavePerformanceTracingEnabled(): bool
73
    {
74
        $config = $this->getUserConfig();
75
76
        return !empty($config['options']['traces_sample_rate'])
77
            || !empty($config['options']['traces_sampler']);
78
    }
79
}
80