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

CriticalErrorsHandler::handleExceptionWithSyslog()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 2
nc 2
nop 2
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
declare(strict_types=1);
21
22
namespace MikoPBX\Common\Handlers;
23
24
use MikoPBX\Common\Providers\SentryErrorHandlerProvider;
25
use MikoPBX\Common\Providers\WhoopsErrorHandlerProvider;
26
use MikoPBX\Core\System\Util;
27
use Phalcon\Di;
28
use Throwable;
29
/**
30
 * Collects errors and send them to Sentry cloud for software improvement reasons
31
 */
32
class CriticalErrorsHandler
33
{
34
    /**
35
     * Captures an exception, sends it to the Sentry cloud, and writes syslog.
36
     *
37
     * @param Throwable $exception The exception to capture.
38
     * @param string $moduleTag The name of the current module.
39
     *
40
     * @return string Pretty error description
41
     */
42
    public static function handleExceptionWithSyslog(Throwable $exception, string $moduleTag=''): string
0 ignored issues
show
Unused Code introduced by
The parameter $moduleTag is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

42
    public static function handleExceptionWithSyslog(Throwable $exception, /** @scrutinizer ignore-unused */ string $moduleTag=''): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
    {
44
        // Sentry
45
        $di = Di::getDefault();
46
        $sentryErrorHandler = $di->get(SentryErrorHandlerProvider::SERVICE_NAME);
47
        if ($sentryErrorHandler){
48
            $sentryErrorHandler->captureException($exception);
49
        }
50
51
        // Whoops
52
        $message = WhoopsErrorHandlerProvider::makePrettyErrorDescription($exception, false);
53
        Util::sysLogMsg("EXCEPTION", $message, LOG_ERR);
54
        return $message;
55
    }
56
57
    /**
58
     * Handle an exception, sends it to the Sentry cloud, and shows error description to user.
59
     *
60
     * @param Throwable $exception The exception to capture.
61
     * @param string $moduleTag The name of the current module.
62
     */
63
    public static function handleException(Throwable $exception, string $moduleTag=''): void
0 ignored issues
show
Unused Code introduced by
The parameter $moduleTag is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

63
    public static function handleException(Throwable $exception, /** @scrutinizer ignore-unused */ string $moduleTag=''): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    {
65
        // Sentry
66
        $di = Di::getDefault();
67
        $sentryErrorHandler = $di->get(SentryErrorHandlerProvider::SERVICE_NAME);
68
        if ($sentryErrorHandler){
69
            $sentryErrorHandler->captureException($exception);
70
        }
71
72
        // Whoops
73
        $whoops = $di->get(WhoopsErrorHandlerProvider::SERVICE_NAME);
74
        if ($whoops){
75
            $whoops->handleException($exception);
76
        }
77
    }
78
79
}
80