Completed
Pull Request — master (#11)
by
unknown
09:06
created

Handler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 73.08%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 125
ccs 19
cts 26
cp 0.7308
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A report() 0 11 2
A render() 0 4 1
A renderForConsole() 0 4 1
A trackException() 0 12 2
A handleError() 0 14 2
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 117
    public function __construct(Tracker $tracker, ExceptionHandlerContract $illuminateHandler)
40
    {
41 117
        $this->manager                  = $tracker;
42 117
        $this->illuminateHandler        = $illuminateHandler;
43 117
        $this->originalExceptionHandler = set_exception_handler([$this, 'trackException']);
44 117
        $this->originalErrorHandler     = set_error_handler([$this, 'handleError']);
45 117
    }
46
47
    /* ------------------------------------------------------------------------------------------------
48
     |  Main Functions
49
     | ------------------------------------------------------------------------------------------------
50
     */
51
    /**
52
     * Report or log an exception.
53
     *
54
     * @param  \Exception  $e
55
     */
56 3
    public function report(Exception $e)
57
    {
58
        try {
59 3
            $this->manager->trackException($e);
60
        }
61 1
        catch (Exception $exception) {
62
            // ignore
63
        }
64
65 3
        $this->illuminateHandler->report($e);
66 3
    }
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 3
    public function render($request, Exception $e)
77
    {
78 3
        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 105
    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...
124
    {
125
        try {
126 105
            $this->manager->trackException(
127 105
                ExceptionFactory::make($err_severity, $err_msg)
128 35
            );
129
        }
130 105
        catch (Exception $e) {
131
            // Ignore Tracker exceptions
132
        }
133
134
        // Call Laravel Exception Handler
135 105
        return call_user_func($this->originalErrorHandler, $err_severity, $err_msg, $err_file, $err_line);
136
    }
137
}
138