Completed
Push — master ( d38aea...aecaa2 )
by Russell
04:32 queued 11s
created

SentrySeverity::process_severity()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * Class: SentrySeverity.
5
 *
6
 * @author  Russell Michell 2019 <[email protected]>
7
 * @package phptek/sentry
8
 */
9
10
namespace PhpTek\Sentry\Adaptor;
11
12
use Sentry\Severity;
13
14
/**
15
 * SentrySeverity provides static methods that process or refine incoming severities
16
 * as integers (from PHP's severity constants, or as strings from userland config).
17
 */
18
class SentrySeverity
19
{
20
        /**
21
     * Maps PHP's internal error-types into those suited to {@link Severity}.
22
     *
23
     * @param  mixed int|string $severity The incoming level from userland code or
24
     *                                    PHP itself.
25
     * @return string
26
     */
27
    public static function process_severity($severity) : string
28
    {
29
        // Stringified PHP severities out of \backtrace() like "notice"
30
        if (is_string($severity)) {
31
            $level = self::from_error($severity);
32
        // De-facto PHP severities as constants (ints) like E_NOTICE
33
        } else if (is_numeric($severity)) {
34
            $level = Severity::fromError($severity);
35
        } else {
36
            // "Other"
37
            $level = Severity::ERROR;
38
        }
39
40
        return strtolower($level);
41
    }
42
43
    /**
44
     * Almost an exact replica of {@link Severity::fromError()}, except we're
45
     * dealing with string values passed to us from upstream processes.
46
     * 
47
     * @param  string $severity An incoming severity.
48
     * @return string
49
     */
50
    public static function from_error(string $severity) : string
51
    {
52
        $severity = strtolower($severity);
53
        
54
        switch ($severity) {
55
            case 'deprecated':
56
            case 'user_deprecated':
57
            case 'warning':
58
            case 'user_warning':
59
                return Severity::WARNING;
60
            case 'error':
61
            case 'parse':
62
            case 'coreerror':
63
            case 'corwarning';
64
            case 'compilerrror':
65
            case 'compilewarning':
66
                return Severity::FATAL;
67
            case 'recoverablerror':
68
            case 'user_error':
69
                return Severity::ERROR;
70
            case 'notice':
71
            case 'user_notice':
72
            case 'strict':
73
                return Severity::INFO;
74
            default:
75
                // It's an error until proven otherwise
76
                return Severity::ERROR;
77
        }
78
    }
79
    
80
}
81