Completed
Pull Request — master (#2)
by Sam
06:02
created

NewRelicExceptionHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 67
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A render() 0 4 1
A renderForConsole() 0 4 1
A report() 0 6 2
A logException() 0 6 2
1
<?php
2
3
namespace Nord\Lumen\NewRelic;
4
5
use Exception;
6
use Illuminate\Contracts\Debug\ExceptionHandler;
7
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8
9
/**
10
 * Class NewRelicExceptionHandler
11
 * @package Nord\Lumen\NewRelic
12
 */
13
class NewRelicExceptionHandler implements ExceptionHandler
14
{
15
16
    /**
17
     * @var array list of class names of exceptions that should not be reported to New Relic. Defaults to the
18
     *            NotFoundHttpException class used for 404 requests.
19
     */
20
    protected $ignoredExceptions = [
21
        NotFoundHttpException::class,
22
    ];
23
24
25
    /**
26
     * NewRelicExceptionHandler constructor.
27
     *
28
     * @param array $ignoredExceptions (optional)
29
     */
30
    public function __construct(array $ignoredExceptions = [])
31
    {
32
        if (!empty($ignoredExceptions)) {
33
            $this->ignoredExceptions = $ignoredExceptions;
34
        }
35
    }
36
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function report(Exception $e)
42
    {
43
        if (!in_array(get_class($e), $this->ignoredExceptions)) {
44
            $this->logException($e);
45
        }
46
    }
47
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function render($request, Exception $e)
53
    {
54
55
    }
56
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function renderForConsole($output, Exception $e)
62
    {
63
64
    }
65
66
67
    /**
68
     * Logs the exception to New Relic (if the extension is loaded)
69
     *
70
     * @param Exception $e
71
     */
72
    protected function logException(Exception $e)
73
    {
74
        if (extension_loaded('newrelic')) {
75
            newrelic_notice_error($e->getMessage(), $e);
76
        }
77
    }
78
79
}
80