Completed
Push — master ( 243903...9a7d92 )
by ARCANEDEV
9s
created

src/Exceptions/Handler.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    /** @var  \Arcanedev\LaravelTracker\Contracts\Tracker */
20
    private $manager;
21
22
    /** @var  \Illuminate\Contracts\Debug\ExceptionHandler */
23
    private $illuminateHandler;
24
25
    private $originalExceptionHandler;
26
27
    private $originalErrorHandler;
28
29
    /* ------------------------------------------------------------------------------------------------
30
     |  Constructor
31
     | ------------------------------------------------------------------------------------------------
32
     */
33
    /**
34
     * Handler constructor.
35
     *
36
     * @param  \Arcanedev\LaravelTracker\Contracts\Tracker   $tracker
37
     * @param  \Illuminate\Contracts\Debug\ExceptionHandler  $illuminateHandler
38
     */
39 234
    public function __construct(Tracker $tracker, ExceptionHandlerContract $illuminateHandler)
40
    {
41 234
        $this->manager                  = $tracker;
42 234
        $this->illuminateHandler        = $illuminateHandler;
43 234
        $this->originalExceptionHandler = set_exception_handler([$this, 'trackException']);
44 234
        $this->originalErrorHandler     = set_error_handler([$this, 'handleError']);
45 234
    }
46
47
    /* ------------------------------------------------------------------------------------------------
48
     |  Main Functions
49
     | ------------------------------------------------------------------------------------------------
50
     */
51
    /**
52
     * Report or log an exception.
53
     *
54
     * @param  \Exception  $e
55
     */
56 4
    public function report(Exception $e)
57
    {
58
        try {
59 4
            $this->manager->trackException($e);
60
        }
61 2
        catch (Exception $exception) {
62
            // ignore
63
        }
64
65 4
        $this->illuminateHandler->report($e);
66 4
    }
67
68
    /**
69
     * Render an exception into an HTTP response.
70
     *
71
     * @param  \Illuminate\Http\Request  $request
72
     * @param  \Exception                $e
73
     *
74
     * @return \Symfony\Component\HttpFoundation\Response
75
     */
76 4
    public function render($request, Exception $e)
77
    {
78 4
        return $this->illuminateHandler->render($request, $e);
79
    }
80
81
    /**
82
     * Render an exception to the console.
83
     *
84
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
85
     * @param  \Exception                                         $e
86
     */
87
    public function renderForConsole($output, Exception $e)
88
    {
89
        $this->illuminateHandler->renderForConsole($output, $e);
90
    }
91
92
    /**
93
     * Track the exception.
94
     *
95
     * @param  Exception  $exception
96
     *
97
     * @return mixed
98
     */
99
    public function trackException(Exception $exception)
100
    {
101
        try {
102
            $this->manager->trackException($exception);
103
        }
104
        catch (Exception $e) {
105
            // Ignore Tracker exceptions
106
        }
107
108
        // Call Laravel Exception Handler
109
        return call_user_func($this->originalExceptionHandler, $exception);
110
    }
111
112
    /**
113
     * Handle the error.
114
     *
115
     * @param  int     $err_severity
116
     * @param  string  $err_msg
117
     * @param  mixed   $err_file
118
     * @param  mixed   $err_line
119
     * @param  array   $err_context
120
     *
121
     * @return mixed
122
     */
123
    public function handleError($err_severity, $err_msg, $err_file, $err_line, array $err_context)
0 ignored issues
show
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...
124
    {
125
        try {
126
            $this->manager->trackException(
127
                ExceptionFactory::make($err_severity, $err_msg)
128
            );
129
        }
130
        catch (Exception $e) {
131
            // Ignore Tracker exceptions
132
        }
133
134
        // Call Laravel Exception Handler
135
        return call_user_func($this->originalErrorHandler, $err_severity, $err_msg, $err_file, $err_line);
136
    }
137
}
138