1 | <?php |
||
17 | class Notifier extends Component |
||
18 | { |
||
19 | protected $config; |
||
20 | protected $log; |
||
21 | protected $messageCb; |
||
22 | protected $subjectCb; |
||
23 | protected $contextCb; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $absolutelyDontHandle = [ |
||
29 | \Tylercd100\LERN\Exceptions\NotifierFailedException::class, |
||
30 | ]; |
||
31 | |||
32 | /** |
||
33 | * You can provide a Monolog Logger instance to use in the constructor |
||
34 | * @param Logger|null $log Logger instance to use |
||
35 | */ |
||
36 | 36 | public function __construct(Logger $log = null) |
|
45 | |||
46 | /** |
||
47 | * Transforms a value into a closure that returns itself when called |
||
48 | * @param callable|string $cb The value that you want to wrap in a closure |
||
49 | * @return callable |
||
50 | */ |
||
51 | private function wrapValueInClosure($cb) |
||
52 | { |
||
53 | if (is_callable($cb)) { |
||
54 | return $cb; |
||
55 | } else { |
||
56 | return function () use ($cb) { return $cb; }; |
||
57 | } |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Set a string or a closure to be called that will generate the message body for the notification |
||
62 | * @param callable|string $cb A closure or string that will be set for the message |
||
63 | * @return $this |
||
64 | */ |
||
65 | public function setMessage($cb) |
||
66 | { |
||
67 | $this->messageCb = $this->wrapValueInClosure($cb); |
||
68 | return $this; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Returns the result of the message closure |
||
73 | * @param Exception $e The Exception instance that you want to build the message around |
||
74 | * @return string The message string |
||
75 | */ |
||
76 | 3 | public function getMessage(Exception $e) |
|
77 | { |
||
78 | 3 | $msg = $this->getMessageViaView($e); |
|
79 | |||
80 | 3 | if ($msg === false) { |
|
81 | $msg = $this->getMessageViaCallback($e); |
||
82 | } |
||
83 | |||
84 | 3 | if ($msg === false) { |
|
85 | $msg = $this->getMessageViaDefault($e); |
||
86 | } |
||
87 | |||
88 | 3 | return $msg; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Gets a basic Exception message |
||
93 | * @param Exception $e The Exception instance that you want to build the message around |
||
94 | * @return String Returns the message string |
||
95 | */ |
||
96 | public function getMessageViaDefault(Exception $e) |
||
97 | { |
||
98 | $msg = get_class($e)." was thrown! \n".$e->getMessage(); |
||
99 | if ($this->config['includeExceptionStackTrace'] === true) { |
||
100 | $msg .= "\n\n".$e->getTraceAsString(); |
||
101 | } |
||
102 | return $msg; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Gets the Exception message using a callback if it is set |
||
107 | * @param Exception $e The Exception instance that you want to build the message around |
||
108 | * @return String|false Returns the message string or false |
||
109 | */ |
||
110 | public function getMessageViaCallback(Exception $e) |
||
111 | { |
||
112 | if (is_callable($this->messageCb)) { |
||
113 | return $this->messageCb->__invoke($e); |
||
114 | } |
||
115 | return false; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Gets the Exception message using a Laravel view file |
||
120 | * @param Exception $e The Exception instance that you want to build the message around |
||
121 | * @return String|false Returns the message string or false |
||
122 | */ |
||
123 | 3 | public function getMessageViaView(Exception $e) |
|
124 | { |
||
125 | 3 | $path = @$this->config["view"]; |
|
126 | 3 | if (!empty($path) && View::exists($path)) { |
|
127 | 3 | return View::make($path, [ |
|
128 | 3 | "exception" => $e, |
|
129 | 3 | "url" => Request::url(), |
|
130 | 3 | "method" => Request::method(), |
|
131 | 3 | "input" => Input::all(), |
|
132 | 3 | "user" => Auth::user(), |
|
133 | 3 | ])->render(); |
|
134 | } |
||
135 | return false; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Set a string or a closure to be called that will generate the subject line for the notification |
||
140 | * @param callable|string $cb A closure or string that will be set for the subject line |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function setSubject($cb) |
||
144 | { |
||
145 | $this->subjectCb = $this->wrapValueInClosure($cb); |
||
146 | return $this; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Returns the result of the subject closure |
||
151 | * @param Exception $e The Exception instance that you want to build the subject around |
||
152 | * @return string The subject string |
||
153 | */ |
||
154 | 3 | public function getSubject(Exception $e) |
|
155 | { |
||
156 | 3 | if (is_callable($this->subjectCb)) { |
|
157 | return $this->subjectCb->__invoke($e); |
||
158 | } else { |
||
159 | 3 | return get_class($e); |
|
160 | } |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Set an array or a closure to be called that will generate the context array for the notification |
||
165 | * @param callable|array $cb A closure or array that will be set for the context |
||
166 | * @return $this |
||
167 | */ |
||
168 | public function setContext($cb) |
||
169 | { |
||
170 | $this->contextCb = $this->wrapValueInClosure($cb); |
||
171 | return $this; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Returns the result of the context closure |
||
176 | * @param Exception $e The Exception instance that you want to build the context around |
||
177 | * @return array The context array |
||
178 | */ |
||
179 | 3 | public function getContext(Exception $e, $context = []) |
|
180 | { |
||
181 | //This needs a better solution. How do I set specific context needs for different drivers? |
||
182 | 3 | if (in_array('pushover', $this->config['drivers'])) { |
|
183 | 3 | $context['sound'] = $this->config['pushover']['sound']; |
|
184 | } |
||
185 | |||
186 | // Call the callback or return the default |
||
187 | 3 | if (is_callable($this->contextCb)) { |
|
188 | return $this->contextCb->__invoke($e, $context); |
||
189 | } else { |
||
190 | 3 | return $context; |
|
191 | } |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Get the log level |
||
196 | * @return string |
||
197 | */ |
||
198 | 3 | public function getLogLevel() |
|
202 | |||
203 | /** |
||
204 | * Set the log level |
||
205 | * @param string $level The log level |
||
206 | * @return \Tylercd100\LERN\LERN |
||
207 | */ |
||
208 | 3 | public function setLogLevel($level) |
|
213 | |||
214 | /** |
||
215 | * Pushes on another Monolog Handler |
||
216 | * @param HandlerInterface $handler The handler instance to add on |
||
217 | * @return Notifier Returns this |
||
218 | */ |
||
219 | public function pushHandler(HandlerInterface $handler) |
||
220 | { |
||
221 | $this->log->pushHandler($handler); |
||
222 | return $this; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Triggers the Monolog Logger instance to log an error to all handlers |
||
227 | * @param Exception $e The exception to use |
||
228 | * @param array $context Additional information that you would like to pass to Monolog |
||
229 | * @return bool |
||
230 | * @throws NotifierFailedException |
||
231 | */ |
||
232 | 3 | public function send(Exception $e, array $context = []) |
|
259 | } |
||
260 |