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