Passed
Pull Request — master (#19)
by
unknown
04:05 queued 01:18
created

YiiSentryConfig   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 80.65%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 69
ccs 25
cts 31
cp 0.8065
rs 10
c 1
b 0
f 0
wmc 17

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A getUserConfig() 0 5 2
A hasDsnSet() 0 5 1
A getTracing() 0 7 3
A getIntegrations() 0 7 3
A couldHavePerformanceTracingEnabled() 0 6 2
A getOptions() 0 7 3
A getLogLevel() 0 3 2
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