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