1 | <?php |
||
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 | 27 | public function __construct(Notifier $notifier = null, Recorder $recorder = null) |
|
35 | { |
||
36 | 27 | if (empty($notifier)) { |
|
37 | 15 | $notifier = new Notifier(); |
|
38 | } |
||
39 | 27 | $this->notifier = $notifier; |
|
40 | |||
41 | 27 | if (empty($recorder)) { |
|
42 | 27 | $recorder = new Recorder(); |
|
43 | } |
||
44 | 27 | $this->recorder = $recorder; |
|
45 | 27 | } |
|
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 | } |
||
58 | |||
59 | /** |
||
60 | * Stores the exception in the database |
||
61 | * @param Exception $e The exception to use |
||
62 | * @return \Tylercd100\LERN\Models\ExceptionModel|false 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) |
|
80 | |||
81 | /** |
||
82 | * Pushes on another Monolog Handler |
||
83 | * @param HandlerInterface $handler The handler instance to add on |
||
84 | * @return $this |
||
85 | */ |
||
86 | 3 | public function pushHandler(HandlerInterface $handler) { |
|
87 | 3 | $this->notifier->pushHandler($handler); |
|
88 | 3 | return $this; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Get Notifier |
||
93 | * @return \Tylercd100\LERN\Components\Notifier |
||
94 | */ |
||
95 | 3 | public function getNotifier() |
|
99 | |||
100 | /** |
||
101 | * Set Notifier |
||
102 | * @param \Tylercd100\LERN\Components\Notifier $notifier A Notifier instance to use |
||
103 | * @return \Tylercd100\LERN\LERN |
||
104 | */ |
||
105 | 3 | public function setNotifier(Notifier $notifier) |
|
110 | |||
111 | /** |
||
112 | * Get Recorder |
||
113 | * @return \Tylercd100\LERN\Components\Recorder |
||
114 | */ |
||
115 | 3 | public function getRecorder() |
|
119 | |||
120 | /** |
||
121 | * Set Recorder |
||
122 | * @param \Tylercd100\LERN\Components\Recorder $recorder A Recorder instance to use |
||
123 | * @return \Tylercd100\LERN\LERN |
||
124 | */ |
||
125 | 3 | public function setRecorder(Recorder $recorder) |
|
130 | |||
131 | /** |
||
132 | * Set a string or a closure to be called that will generate the message body for the notification |
||
133 | * @param function|string $cb This closure function will be passed an Exception and must return a string |
||
134 | * @return $this |
||
135 | */ |
||
136 | 3 | public function setMessage($cb) |
|
141 | |||
142 | /** |
||
143 | * Set a string or a closure to be called that will generate the subject line for the notification |
||
144 | * @param function|string $cb This closure function will be passed an Exception and must return a string |
||
145 | * @return $this |
||
146 | */ |
||
147 | 3 | public function setSubject($cb) |
|
152 | |||
153 | } |