Handler::trackException()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 4
cp 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php namespace Arcanedev\LaravelTracker\Exceptions;
2
3
use Arcanedev\LaravelTracker\Contracts\Tracker;
4
use Exception;
5
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract;
6
7
/**
8
 * Class     Handler
9
 *
10
 * @package  Arcanedev\LaravelTracker\Exceptions
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class Handler implements ExceptionHandlerContract
14
{
15
    /* -----------------------------------------------------------------
16
     |  Properties
17
     | -----------------------------------------------------------------
18
     */
19
20
    /** @var  \Arcanedev\LaravelTracker\Contracts\Tracker */
21
    private $manager;
22
23
    /** @var  \Illuminate\Contracts\Debug\ExceptionHandler */
24
    private $illuminateHandler;
25
26
    private $originalExceptionHandler;
27
28
    private $originalErrorHandler;
29
30
    /* -----------------------------------------------------------------
31
     |  Constructor
32
     | -----------------------------------------------------------------
33
     */
34
35
    /**
36
     * Handler constructor.
37
     *
38
     * @param  \Arcanedev\LaravelTracker\Contracts\Tracker   $tracker
39
     * @param  \Illuminate\Contracts\Debug\ExceptionHandler  $illuminateHandler
40
     */
41 117
    public function __construct(Tracker $tracker, ExceptionHandlerContract $illuminateHandler)
42
    {
43 117
        $this->manager                  = $tracker;
44 117
        $this->illuminateHandler        = $illuminateHandler;
45 117
        $this->originalExceptionHandler = set_exception_handler([$this, 'trackException']);
46 117
        $this->originalErrorHandler     = set_error_handler([$this, 'handleError']);
47 117
    }
48
49
    /* -----------------------------------------------------------------
50
     |  Main Methods
51
     | -----------------------------------------------------------------
52
     */
53
54
    /**
55
     * Report or log an exception.
56
     *
57
     * @param  \Exception  $e
58
     */
59 3
    public function report(Exception $e)
60
    {
61
        try {
62 3
            $this->manager->trackException($e);
63
        }
64
        catch (Exception $exception) {
65
            // ignore
66
        }
67
68 3
        $this->illuminateHandler->report($e);
69 3
    }
70
71
    /**
72
     * Render an exception into an HTTP response.
73
     *
74
     * @param  \Illuminate\Http\Request  $request
75
     * @param  \Exception                $e
76
     *
77
     * @return \Symfony\Component\HttpFoundation\Response
78
     */
79 3
    public function render($request, Exception $e)
80
    {
81 3
        return $this->illuminateHandler->render($request, $e);
82
    }
83
84
    /**
85
     * Render an exception to the console.
86
     *
87
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
88
     * @param  \Exception                                         $e
89
     */
90
    public function renderForConsole($output, Exception $e)
91
    {
92
        $this->illuminateHandler->renderForConsole($output, $e);
93
    }
94
95
    /**
96
     * Track the exception.
97
     *
98
     * @param  Exception  $exception
99
     *
100
     * @return mixed
101
     */
102
    public function trackException(Exception $exception)
103
    {
104
        try {
105
            $this->manager->trackException($exception);
106
        }
107
        catch (Exception $e) {
108
            // Ignore Tracker exceptions
109
        }
110
111
        // Call Laravel Exception Handler
112
        return call_user_func($this->originalExceptionHandler, $exception);
113
    }
114
115
    /**
116
     * Handle the error.
117
     *
118
     * @param  int     $err_severity
119
     * @param  string  $err_msg
120
     * @param  mixed   $err_file
121
     * @param  mixed   $err_line
122
     * @param  array   $err_context
123
     *
124
     * @return mixed
125
     */
126
    public function handleError($err_severity, $err_msg, $err_file, $err_line, array $err_context)
0 ignored issues
show
Unused Code introduced by
The parameter $err_context is not used and could be removed.

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

Loading history...
127
    {
128
        try {
129
            $this->manager->trackException(
130
                ExceptionFactory::make($err_severity, $err_msg)
131
            );
132
        }
133
        catch (Exception $e) {
134
            // Ignore Tracker exceptions
135
        }
136
137
        // Call Laravel Exception Handler
138
        return call_user_func($this->originalErrorHandler, $err_severity, $err_msg, $err_file, $err_line);
139
    }
140
}
141