1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Tylercd100\LERN;
|
4
|
|
|
|
5
|
|
|
use Exception;
|
6
|
|
|
use Monolog\Handler\HandlerInterface;
|
7
|
|
|
use Tylercd100\LERN\Components\Notifier;
|
8
|
|
|
use Tylercd100\LERN\Components\Recorder;
|
9
|
|
|
|
10
|
|
|
/**
|
11
|
|
|
* The master class
|
12
|
|
|
*/
|
13
|
|
|
class LERN
|
14
|
|
|
{
|
15
|
|
|
/**
|
16
|
|
|
* @var Exception
|
17
|
|
|
*/
|
18
|
|
|
private $exception;
|
19
|
|
|
|
20
|
|
|
/**
|
21
|
|
|
* @var Notifier
|
22
|
|
|
*/
|
23
|
|
|
private $notifier;
|
24
|
|
|
|
25
|
|
|
/**
|
26
|
|
|
* @var Recorder
|
27
|
|
|
*/
|
28
|
|
|
private $recorder;
|
29
|
|
|
|
30
|
|
|
/**
|
31
|
|
|
* @param Notifier|null $notifier Notifier instance
|
32
|
|
|
* @param Recorder|null $recorder Recorder instance
|
33
|
|
|
*/
|
34
|
27 |
|
public function __construct(Notifier $notifier = null, Recorder $recorder = null)
|
35
|
|
|
{
|
36
|
27 |
|
$this->notifier = $this->buildNotifier($notifier);
|
37
|
27 |
|
$this->recorder = $this->buildRecorder($recorder);
|
38
|
27 |
|
}
|
39
|
|
|
|
40
|
|
|
/**
|
41
|
|
|
* Will execute record and notify methods
|
42
|
|
|
* @param Exception $e The exception to use
|
43
|
|
|
* @return ExceptionModel the recorded Eloquent Model
|
44
|
|
|
*/
|
45
|
3 |
|
public function handle(Exception $e)
|
46
|
|
|
{
|
47
|
3 |
|
$this->exception = $e;
|
48
|
3 |
|
$this->notify($e);
|
49
|
3 |
|
return $this->record($e);
|
50
|
|
|
}
|
51
|
|
|
|
52
|
|
|
/**
|
53
|
|
|
* Stores the exception in the database
|
54
|
|
|
* @param Exception $e The exception to use
|
55
|
|
|
* @return \Tylercd100\LERN\Models\ExceptionModel|false The recorded Exception as an Eloquent Model
|
56
|
|
|
*/
|
57
|
3 |
|
public function record(Exception $e)
|
58
|
|
|
{
|
59
|
3 |
|
$this->exception = $e;
|
60
|
3 |
|
return $this->recorder->record($e);
|
61
|
|
|
}
|
62
|
|
|
|
63
|
|
|
/**
|
64
|
|
|
* Will send the exception to all monolog handlers
|
65
|
|
|
* @param Exception $e The exception to use
|
66
|
|
|
* @return void
|
67
|
|
|
*/
|
68
|
3 |
|
public function notify(Exception $e)
|
69
|
|
|
{
|
70
|
3 |
|
$this->exception = $e;
|
71
|
3 |
|
$this->notifier->send($e);
|
72
|
3 |
|
}
|
73
|
|
|
|
74
|
|
|
/**
|
75
|
|
|
* Pushes on another Monolog Handler
|
76
|
|
|
* @param HandlerInterface $handler The handler instance to add on
|
77
|
|
|
* @return $this
|
78
|
|
|
*/
|
79
|
3 |
|
public function pushHandler(HandlerInterface $handler) {
|
80
|
3 |
|
$this->notifier->pushHandler($handler);
|
81
|
3 |
|
return $this;
|
82
|
|
|
}
|
83
|
|
|
|
84
|
|
|
/**
|
85
|
|
|
* Get Notifier
|
86
|
|
|
* @return \Tylercd100\LERN\Components\Notifier
|
87
|
|
|
*/
|
88
|
3 |
|
public function getNotifier()
|
89
|
|
|
{
|
90
|
3 |
|
return $this->notifier;
|
91
|
|
|
}
|
92
|
|
|
|
93
|
|
|
/**
|
94
|
|
|
* Set Notifier
|
95
|
|
|
* @param \Tylercd100\LERN\Components\Notifier $notifier A Notifier instance to use
|
96
|
|
|
* @return \Tylercd100\LERN\LERN
|
97
|
|
|
*/
|
98
|
3 |
|
public function setNotifier(Notifier $notifier)
|
99
|
|
|
{
|
100
|
3 |
|
$this->notifier = $notifier;
|
101
|
3 |
|
return $this;
|
102
|
|
|
}
|
103
|
|
|
|
104
|
|
|
/**
|
105
|
|
|
* Get Recorder
|
106
|
|
|
* @return \Tylercd100\LERN\Components\Recorder
|
107
|
|
|
*/
|
108
|
3 |
|
public function getRecorder()
|
109
|
|
|
{
|
110
|
3 |
|
return $this->recorder;
|
111
|
|
|
}
|
112
|
|
|
|
113
|
|
|
/**
|
114
|
|
|
* Set Recorder
|
115
|
|
|
* @param \Tylercd100\LERN\Components\Recorder $recorder A Recorder instance to use
|
116
|
|
|
* @return \Tylercd100\LERN\LERN
|
117
|
|
|
*/
|
118
|
3 |
|
public function setRecorder(Recorder $recorder)
|
119
|
|
|
{
|
120
|
3 |
|
$this->recorder = $recorder;
|
121
|
3 |
|
return $this;
|
122
|
|
|
}
|
123
|
|
|
|
124
|
|
|
/**
|
125
|
|
|
* Set a string or a closure to be called that will generate the message body for the notification
|
126
|
|
|
* @param function|string $cb This closure function will be passed an Exception and must return a string
|
127
|
|
|
* @return $this
|
128
|
|
|
*/
|
129
|
3 |
|
public function setMessage($cb)
|
130
|
|
|
{
|
131
|
3 |
|
$this->notifier->setMessage($cb);
|
132
|
3 |
|
return $this;
|
133
|
|
|
}
|
134
|
|
|
|
135
|
|
|
/**
|
136
|
|
|
* Set a string or a closure to be called that will generate the subject line for the notification
|
137
|
|
|
* @param function|string $cb This closure function will be passed an Exception and must return a string
|
138
|
|
|
* @return $this
|
139
|
|
|
*/
|
140
|
3 |
|
public function setSubject($cb)
|
141
|
|
|
{
|
142
|
3 |
|
$this->notifier->setSubject($cb);
|
143
|
3 |
|
return $this;
|
144
|
|
|
}
|
145
|
|
|
|
146
|
|
|
/**
|
147
|
|
|
* Constructs a Notifier
|
148
|
|
|
*
|
149
|
|
|
* @param Notifier $notifier
|
150
|
|
|
* @return Notifier
|
151
|
|
|
*/
|
152
|
27 |
|
protected function buildNotifier(Notifier $notifier = null)
|
153
|
|
|
{
|
154
|
27 |
|
$class = config('lern.notify.class');
|
155
|
27 |
|
if (empty($notifier)) {
|
156
|
15 |
|
$notifier = new $class();
|
157
|
|
|
}
|
158
|
27 |
|
if ($notifier instanceof Notifier) {
|
159
|
27 |
|
return $notifier;
|
160
|
|
|
} else {
|
161
|
|
|
throw new NotifierFailedException("LERN was expecting an instance of ".Notifier::class);
|
162
|
|
|
}
|
163
|
|
|
}
|
164
|
|
|
|
165
|
|
|
/**
|
166
|
|
|
* Constructs a Recorder
|
167
|
|
|
*
|
168
|
|
|
* @param Recorder $recorder
|
169
|
|
|
* @return Recorder
|
170
|
|
|
*/
|
171
|
27 |
|
protected function buildRecorder(Recorder $recorder = null)
|
172
|
|
|
{
|
173
|
27 |
|
$class = config('lern.record.class');
|
174
|
27 |
|
if (empty($recorder)) {
|
175
|
27 |
|
$recorder = new $class();
|
176
|
|
|
}
|
177
|
27 |
|
if ($recorder instanceof Recorder) {
|
178
|
27 |
|
return $recorder;
|
179
|
|
|
} else {
|
180
|
|
|
throw new RecorderFailedException("LERN was expecting an instance of ".Recorder::class);
|
181
|
|
|
}
|
182
|
|
|
}
|
183
|
|
|
} |