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

YiiSentryConfig::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
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 11
    public function getUserConfig(): array
29
    {
30 11
        $config = $this->config;
31
32 11
        return empty($config) ? [] : $config;
33
    }
34
35 11
    public function getOptions(): array
36
    {
37 11
        if (empty($this->config['options'])) {
38
            return [];
39
        }
40 11
        return is_array($this->config['options'])
41 11
                ? $this->config['options']
42 11
                : throw  new InvalidArgumentException('options must be an array');
43
    }
44
45 13
    public function getTracing(): array
46
    {
47 13
        if (empty($this->config['tracing'])) {
48 2
            return [];
49
        }
50 11
        return is_array($this->config['tracing'])
51 11
                ? $this->config['tracing']
52 11
                : throw  new InvalidArgumentException('tracing must be an array');
53
    }
54
55 11
    public function getLogLevel(): ?string
56
    {
57 11
        return isset($this->config['log_level']) ? (string)$this->config['log_level'] : null;
58
    }
59
60 13
    public function getIntegrations(): array
61
    {
62 13
        if (empty($this->config['integrations'])) {
63 13
            return [];
64
        }
65
        return is_array($this->config['integrations'])
66
                ? $this->config['integrations']
67
                : throw  new InvalidArgumentException('integrations must be an array');
68
    }
69
70
    /**
71
     * Checks if the config is set in such a way that performance tracing could be enabled.
72
     *
73
     * Because of `traces_sampler` being dynamic we can never be 100% confident but that is also not important.
74
     */
75 11
    public function couldHavePerformanceTracingEnabled(): bool
76
    {
77 11
        $config = $this->getUserConfig();
78
79 11
        return !empty($config['options']['traces_sample_rate'])
80 11
            || !empty($config['options']['traces_sampler']);
81
    }
82
}
83