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

YiiSentryConfig   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 80.65%

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserConfig() 0 5 2
A hasDsnSet() 0 5 1
A getIntegrations() 0 7 3
A __construct() 0 2 1
A couldHavePerformanceTracingEnabled() 0 6 2
A getTracing() 0 7 3
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
     * @return bool
19
     */
20
    public function hasDsnSet(): bool
21
    {
22
        $config = $this->getUserConfig();
23
24
        return !empty($config['options']['dsn']);
25
    }
26
27
    /**
28
     * Retrieve the user configuration.
29
     *
30
     * @return array
31
     */
32 13
    public function getUserConfig(): array
33
    {
34 13
        $config = $this->config;
35
36 13
        return empty($config) ? [] : $config;
37
    }
38
39 11
    public function getOptions(): array
40
    {
41 11
        return empty($this->config['options'])
42
            ? []
43 11
            : (is_array($this->config['options'])
44 11
                ? $this->config['options']
45 11
                : throw  new InvalidArgumentException('options must be an array'));
46
    }
47
48 13
    public function getTracing(): array
49
    {
50 13
        return empty($this->config['tracing'])
51 2
            ? []
52 11
            : (is_array($this->config['tracing'])
53 11
                ? $this->config['tracing']
54 13
                : throw  new InvalidArgumentException('tracing must be an array'));
55
    }
56
57 11
    public function getLogLevel(): ?string
58
    {
59 11
        return isset($this->config['log_level']) ? (string)$this->config['log_level'] : null;
60
    }
61
62
    /**
63
     * @return array
64
     */
65 13
    public function getIntegrations(): array
66
    {
67 13
        return empty($this->config['integrations'])
68 13
            ? []
69
            : (is_array($this->config['integrations'])
70
                ? $this->config['integrations']
71 13
                : throw  new InvalidArgumentException('integrations must be an array'));
72
    }
73
74
    /**
75
     * Checks if the config is set in such a way that performance tracing could be enabled.
76
     *
77
     * Because of `traces_sampler` being dynamic we can never be 100% confident but that is also not important.
78
     *
79
     * @return bool
80
     */
81 13
    public function couldHavePerformanceTracingEnabled(): bool
82
    {
83 13
        $config = $this->getUserConfig();
84
85 13
        return !empty($config['options']['traces_sample_rate'])
86 13
            || !empty($config['options']['traces_sampler']);
87
    }
88
}
89