1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tylercd100\LERN; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
7
|
|
|
use Tylercd100\LERN\Models\ExceptionModel; |
8
|
|
|
use Tylercd100\LERN\Notifications\Notifier; |
9
|
|
|
use Monolog\Handler\HandlerInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The master class |
13
|
|
|
*/ |
14
|
|
|
class LERN |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
private $exception; |
18
|
|
|
private $notifier; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Notifier|null $notifier Notifier instance |
22
|
|
|
*/ |
23
|
21 |
|
public function __construct(Notifier $notifier = null) |
24
|
|
|
{ |
25
|
21 |
|
if (empty($notifier)) { |
26
|
9 |
|
$notifier = new Notifier(); |
27
|
9 |
|
} |
28
|
21 |
|
$this->notifier = $notifier; |
29
|
21 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Will execute record and notify methods |
33
|
|
|
* @param Exception $e The exception to use |
34
|
|
|
* @return ExceptionModel the recorded Eloquent Model |
35
|
|
|
*/ |
36
|
3 |
|
public function handle(Exception $e) |
37
|
|
|
{ |
38
|
3 |
|
$this->exception = $e; |
39
|
3 |
|
$this->notify($e); |
40
|
3 |
|
return $this->record($e); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Stores the exception in the database |
45
|
|
|
* @param Exception $e The exception to use |
46
|
|
|
* @return ExceptionModel the recorded Eloquent Model |
47
|
|
|
*/ |
48
|
3 |
|
public function record(Exception $e) |
49
|
|
|
{ |
50
|
3 |
|
$this->exception = $e; |
51
|
|
|
$opts = [ |
52
|
3 |
|
'class' => get_class($e), |
53
|
3 |
|
'file' => $e->getFile(), |
54
|
3 |
|
'line' => $e->getLine(), |
55
|
3 |
|
'code' => $e->getCode(), |
56
|
3 |
|
'message' => $e->getMessage(), |
57
|
3 |
|
'trace' => $e->getTraceAsString(), |
58
|
3 |
|
]; |
59
|
|
|
|
60
|
3 |
|
if ($e instanceof HttpExceptionInterface) { |
61
|
|
|
$opts['status_code'] = $e->getStatusCode(); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
return ExceptionModel::create($opts); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Will send the exception to all monolog handlers |
69
|
|
|
* @param Exception $e The exception to use |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
3 |
|
public function notify(Exception $e) |
73
|
|
|
{ |
74
|
3 |
|
$this->exception = $e; |
75
|
3 |
|
$this->notifier->send($e); |
76
|
3 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Pushes on another Monolog Handler |
80
|
|
|
* @param HandlerInterface $handler The handler instance to add on |
81
|
|
|
* @return Notifier Returns this |
82
|
|
|
*/ |
83
|
3 |
|
public function pushHandler(HandlerInterface $handler) { |
84
|
3 |
|
$this->notifier->pushHandler($handler); |
85
|
3 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get Notifier |
90
|
|
|
* @return Notifier |
91
|
|
|
*/ |
92
|
3 |
|
public function getNotifier() |
93
|
|
|
{ |
94
|
3 |
|
return $this->notifier; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set Notifier |
99
|
|
|
* @param Notifier $notifier A Notifier instance to use |
100
|
|
|
*/ |
101
|
3 |
|
public function setNotifier(Notifier $notifier) |
102
|
|
|
{ |
103
|
3 |
|
$this->notifier = $notifier; |
104
|
3 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set a string or a closure to be called that will generate the message body for the notification |
109
|
|
|
* @param function|string $cb This closure function will be passed an Exception and must return a string |
110
|
|
|
*/ |
111
|
3 |
|
public function setMessage($cb) |
112
|
|
|
{ |
113
|
3 |
|
$this->notifier->setMessage($cb); |
114
|
3 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Set a string or a closure to be called that will generate the subject line for the notification |
119
|
|
|
* @param function|string $cb This closure function will be passed an Exception and must return a string |
120
|
|
|
*/ |
121
|
3 |
|
public function setSubject($cb) |
122
|
|
|
{ |
123
|
3 |
|
$this->notifier->setSubject($cb); |
124
|
3 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
} |