1 | <?php |
||
15 | class Notifier extends Component |
||
16 | { |
||
17 | protected $config; |
||
18 | protected $log; |
||
19 | protected $messageCb; |
||
20 | protected $subjectCb; |
||
21 | protected $contextCb; |
||
22 | |||
23 | /** |
||
24 | * You can provide a Monolog Logger instance to use in the constructor |
||
25 | * @param Logger|null $log Logger instance to use |
||
26 | */ |
||
27 | 75 | public function __construct(Logger $log = null) |
|
28 | { |
||
29 | 75 | if ($log === null) { |
|
30 | 75 | $log = new Logger(config('lern.notify.channel')); |
|
31 | } |
||
32 | |||
33 | 75 | $this->config = config('lern.notify'); |
|
34 | 75 | $this->log = $log; |
|
35 | 75 | } |
|
36 | |||
37 | /** |
||
38 | * Transforms a value into a closure that returns itself when called |
||
39 | * @param callable|string $cb The value that you want to wrap in a closure |
||
40 | * @return callable |
||
41 | */ |
||
42 | 15 | private function wrapValueInClosure($cb) |
|
50 | |||
51 | /** |
||
52 | * Set a string or a closure to be called that will generate the message body for the notification |
||
53 | * @param callable|string $cb A closure or string that will be set for the message |
||
54 | * @return $this |
||
55 | */ |
||
56 | 6 | public function setMessage($cb) |
|
61 | |||
62 | /** |
||
63 | * Returns the result of the message closure |
||
64 | * @param Exception $e The Exception instance that you want to build the message around |
||
65 | * @return string The message string |
||
66 | */ |
||
67 | 27 | public function getMessage(Exception $e) |
|
68 | { |
||
69 | 27 | $msg = $this->getMessageViaView($e); |
|
70 | |||
71 | 27 | if ($msg === false) { |
|
72 | 9 | $msg = $this->getMessageViaCallback($e); |
|
73 | } |
||
74 | |||
75 | 27 | if ($msg === false) { |
|
76 | 3 | $msg = $this->getMessageViaDefault($e); |
|
77 | } |
||
78 | |||
79 | 27 | return $msg; |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * Gets a basic Exception message |
||
84 | * @param Exception $e The Exception instance that you want to build the message around |
||
85 | * @return String Returns the message string |
||
86 | */ |
||
87 | 3 | public function getMessageViaDefault(Exception $e) |
|
88 | { |
||
89 | 3 | $msg = get_class($e)." was thrown! \n".$e->getMessage(); |
|
90 | 3 | if ($this->config['includeExceptionStackTrace'] === true) { |
|
91 | 3 | $msg .= "\n\n".$e->getTraceAsString(); |
|
92 | } |
||
93 | 3 | return $msg; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Gets the Exception message using a callback if it is set |
||
98 | * @param Exception $e The Exception instance that you want to build the message around |
||
99 | * @return String|false Returns the message string or false |
||
100 | */ |
||
101 | 9 | public function getMessageViaCallback(Exception $e) |
|
108 | |||
109 | /** |
||
110 | * Gets the Exception message using a Laravel view file |
||
111 | * @param Exception $e The Exception instance that you want to build the message around |
||
112 | * @return String|false Returns the message string or false |
||
113 | */ |
||
114 | 27 | public function getMessageViaView(Exception $e) |
|
128 | |||
129 | /** |
||
130 | * Set a string or a closure to be called that will generate the subject line for the notification |
||
131 | * @param callable|string $cb A closure or string that will be set for the subject line |
||
132 | * @return $this |
||
133 | */ |
||
134 | 6 | public function setSubject($cb) |
|
139 | |||
140 | /** |
||
141 | * Returns the result of the subject closure |
||
142 | * @param Exception $e The Exception instance that you want to build the subject around |
||
143 | * @return string The subject string |
||
144 | */ |
||
145 | 18 | public function getSubject(Exception $e) |
|
153 | |||
154 | /** |
||
155 | * Set an array or a closure to be called that will generate the context array for the notification |
||
156 | * @param callable|array $cb A closure or array that will be set for the context |
||
157 | * @return $this |
||
158 | */ |
||
159 | 3 | public function setContext($cb) |
|
164 | |||
165 | /** |
||
166 | * Returns the result of the context closure |
||
167 | * @param Exception $e The Exception instance that you want to build the context around |
||
168 | * @return array The context array |
||
169 | */ |
||
170 | 15 | public function getContext(Exception $e, $context = []) |
|
184 | |||
185 | /** |
||
186 | * Get the log level |
||
187 | * @return string |
||
188 | */ |
||
189 | 3 | public function getLogLevel() |
|
193 | |||
194 | /** |
||
195 | * Set the log level |
||
196 | * @param string $level The log level |
||
197 | * @return \Tylercd100\LERN\LERN |
||
198 | */ |
||
199 | 3 | public function setLogLevel($level) |
|
200 | { |
||
201 | 3 | $this->config['log_level'] = $level; |
|
202 | 3 | return $this; |
|
203 | } |
||
204 | |||
205 | /** |
||
206 | * Pushes on another Monolog Handler |
||
207 | * @param HandlerInterface $handler The handler instance to add on |
||
208 | * @return Notifier Returns this |
||
209 | */ |
||
210 | 6 | public function pushHandler(HandlerInterface $handler) |
|
215 | |||
216 | /** |
||
217 | * Triggers the Monolog Logger instance to log an error to all handlers |
||
218 | * @param Exception $e The exception to use |
||
219 | * @param array $context Additional information that you would like to pass to Monolog |
||
220 | * @return bool |
||
221 | * @throws NotifierFailedException |
||
222 | */ |
||
223 | 15 | public function send(Exception $e, array $context = []) |
|
248 | } |
||
249 |