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 \Tylercd100\LERN\Components\Notifier |
22
|
|
|
*/ |
23
|
|
|
private $notifier; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \Tylercd100\LERN\Components\Recorder |
27
|
|
|
*/ |
28
|
|
|
private $recorder; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param \Tylercd100\LERN\Components\Notifier|null $notifier Notifier instance |
32
|
|
|
* @param \Tylercd100\LERN\Components\Recorder|null $recorder Recorder instance |
33
|
|
|
*/ |
34
|
21 |
|
public function __construct(Notifier $notifier = null, Recorder $recorder = null) |
35
|
|
|
{ |
36
|
21 |
|
if (empty($notifier)) { |
37
|
9 |
|
$notifier = new Notifier(); |
38
|
9 |
|
} |
39
|
21 |
|
$this->notifier = $notifier; |
40
|
|
|
|
41
|
21 |
|
if (empty($recorder)) { |
42
|
21 |
|
$recorder = new Recorder(); |
43
|
21 |
|
} |
44
|
21 |
|
$this->recorder = $recorder; |
45
|
21 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Will execute record and notify methods |
49
|
|
|
* @param Exception $e The exception to use |
50
|
|
|
* @return ExceptionModel the recorded Eloquent Model |
51
|
|
|
*/ |
52
|
3 |
|
public function handle(Exception $e) |
53
|
|
|
{ |
54
|
3 |
|
$this->exception = $e; |
55
|
3 |
|
$this->notify($e); |
56
|
3 |
|
return $this->record($e); |
57
|
3 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Stores the exception in the database |
61
|
|
|
* @param Exception $e The exception to use |
62
|
|
|
* @return \Tylercd100\LERN\Models\ExceptionModel The recorded Exception as an Eloquent Model |
63
|
|
|
*/ |
64
|
3 |
|
public function record(Exception $e) |
65
|
|
|
{ |
66
|
3 |
|
$this->exception = $e; |
67
|
3 |
|
return $this->recorder->record($e); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Will send the exception to all monolog handlers |
72
|
|
|
* @param Exception $e The exception to use |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
3 |
|
public function notify(Exception $e) |
76
|
|
|
{ |
77
|
3 |
|
$this->exception = $e; |
78
|
3 |
|
$this->notifier->send($e); |
79
|
3 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Pushes on another Monolog Handler |
83
|
|
|
* @param HandlerInterface $handler The handler instance to add on |
84
|
|
|
*/ |
85
|
3 |
|
public function pushHandler(HandlerInterface $handler) { |
86
|
3 |
|
$this->notifier->pushHandler($handler); |
87
|
3 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get Notifier |
92
|
|
|
* @return \Tylercd100\LERN\Components\Notifier |
93
|
|
|
*/ |
94
|
3 |
|
public function getNotifier() |
95
|
|
|
{ |
96
|
3 |
|
return $this->notifier; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set Notifier |
101
|
|
|
* @param \Tylercd100\LERN\Components\Notifier $notifier A Notifier instance to use |
102
|
|
|
* @return \Tylercd100\LERN\LERN |
103
|
|
|
*/ |
104
|
3 |
|
public function setNotifier(Notifier $notifier) |
105
|
|
|
{ |
106
|
3 |
|
$this->notifier = $notifier; |
107
|
3 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get Recorder |
112
|
|
|
* @return \Tylercd100\LERN\Components\Recorder |
113
|
|
|
*/ |
114
|
|
|
public function getRecorder() |
115
|
|
|
{ |
116
|
|
|
return $this->recorder; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set Recorder |
121
|
|
|
* @param \Tylercd100\LERN\Components\Recorder $recorder A Recorder instance to use |
122
|
|
|
* @return \Tylercd100\LERN\LERN |
123
|
|
|
*/ |
124
|
|
|
public function setRecorder(Recorder $recorder) |
125
|
|
|
{ |
126
|
|
|
$this->recorder = $recorder; |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set a string or a closure to be called that will generate the message body for the notification |
132
|
|
|
* @param function|string $cb This closure function will be passed an Exception and must return a string |
133
|
|
|
*/ |
134
|
3 |
|
public function setMessage($cb) |
135
|
|
|
{ |
136
|
3 |
|
$this->notifier->setMessage($cb); |
137
|
3 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set a string or a closure to be called that will generate the subject line for the notification |
142
|
|
|
* @param function|string $cb This closure function will be passed an Exception and must return a string |
143
|
|
|
*/ |
144
|
3 |
|
public function setSubject($cb) |
145
|
|
|
{ |
146
|
3 |
|
$this->notifier->setSubject($cb); |
147
|
3 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
} |