Passed
Push — develop ( 578d35...1edb03 )
by Nikolay
13:40 queued 12s
created

SentryConf::configure()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 25
rs 9.4555
cc 5
nc 9
nop 0
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\Core\System\Configs;
21
22
use MikoPBX\Common\Models\PbxSettings;
23
use MikoPBX\Common\Providers\ConfigProvider;
24
use Phalcon\Di\Injectable;
25
26
/**
27
 * Class SentryConf
28
 *
29
 * Represents the Sentry configuration.
30
 *
31
 * @package MikoPBX\Core\System\Configs
32
 */
33
class SentryConf extends Injectable
34
{
35
36
    public const CONF_FILE = '/var/etc/sentry.conf';
37
38
    /**
39
     * Sets up the Sentry conf file.
40
     *
41
     * @return void
42
     */
43
    public function configure(): void
44
    {
45
        if (PbxSettings::getValueByKey('SendMetrics') === '1') {
46
            touch(self::CONF_FILE);
47
        } elseif (file_exists(self::CONF_FILE)) {
48
            unlink(self::CONF_FILE);
49
            return;
50
        }
51
        $sentryConfig = $this->getDI()->getShared(ConfigProvider::SERVICE_NAME)->path('sentry');
52
53
        // Set up options for the Sentry client
54
        $options = [
55
            'dsn'         => $sentryConfig->dsn,
56
            'environment' => $sentryConfig->enviroment??'development',
57
            'traces_sample_rate' =>($sentryConfig->enviroment !== 'development') ? 0.05: 1.0,
58
        ];
59
60
        // Set 'release' option if /etc/version file exists
61
        if (file_exists('/etc/version')) {
62
            $pbxVersion    = str_replace("\n", "", file_get_contents('/etc/version', false));
63
            $options['release']="mikopbx@{$pbxVersion}";
64
        }
65
        $conf = json_encode($options,JSON_PRETTY_PRINT);
66
67
        file_put_contents(self::CONF_FILE, $conf);
68
    }
69
}