1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tylercd100\LERN\Components; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Monolog\Handler\HandlerInterface; |
7
|
|
|
use Monolog\Logger; |
8
|
|
|
use Tylercd100\LERN\Factories\MonologHandlerFactory; |
9
|
|
|
|
10
|
|
|
class Notifier { |
11
|
|
|
protected $config; |
12
|
|
|
protected $log; |
13
|
|
|
protected $messageCb; |
14
|
|
|
protected $subjectCb; |
15
|
|
|
protected $drivers; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* You can provide a Monolog Logger instance to use in the constructor |
19
|
|
|
* @param Logger|null $log Logger instance to use |
20
|
|
|
*/ |
21
|
39 |
|
public function __construct(Logger $log = null) { |
22
|
39 |
|
if ($log === null) { |
23
|
39 |
|
$log = new Logger(config('lern.notify.channel')); |
24
|
39 |
|
} |
25
|
|
|
|
26
|
39 |
|
$this->log = $log; |
27
|
39 |
|
$this->config = config('lern.notify'); |
28
|
39 |
|
$this->drivers = $this->config['drivers']; |
29
|
39 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Transforms a value into a closure that returns itself when called |
33
|
|
|
* @param callable|string $cb The value that you want to wrap in a closure |
34
|
|
|
* @return callable |
35
|
|
|
*/ |
36
|
12 |
|
private function wrapValueInClosure($cb) { |
37
|
12 |
|
if (is_callable($cb)) { |
38
|
6 |
|
return $cb; |
39
|
|
|
} else { |
40
|
|
|
return function() use ($cb) { return $cb; }; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Set a string or a closure to be called that will generate the message body for the notification |
46
|
|
|
* @param callable|string $cb A closure or string that will be set for the message |
47
|
|
|
*/ |
48
|
6 |
|
public function setMessage($cb) |
49
|
|
|
{ |
50
|
6 |
|
$this->messageCb = $this->wrapValueInClosure($cb); |
51
|
6 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns the result of the message closure |
56
|
|
|
* @param Exception $e The Exception instance that you want to build the message around |
57
|
|
|
* @return string The message string |
58
|
|
|
*/ |
59
|
9 |
|
public function getMessage(Exception $e) { |
60
|
9 |
|
if (is_callable($this->messageCb)) { |
61
|
6 |
|
return $this->messageCb->__invoke($e); |
|
|
|
|
62
|
|
|
} else { |
63
|
3 |
|
$msg = get_class($e) . " was thrown! \n" . $e->getMessage(); |
64
|
3 |
|
if ($this->config['includeExceptionStackTrace'] === true) { |
65
|
3 |
|
$msg .= "\n\n" . $e->getTraceAsString(); |
66
|
3 |
|
} |
67
|
3 |
|
return $msg; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set a string or a closure to be called that will generate the subject line for the notification |
73
|
|
|
* @param callable|string $cb A closure or string that will be set for the subject line |
74
|
|
|
*/ |
75
|
6 |
|
public function setSubject($cb) |
76
|
|
|
{ |
77
|
6 |
|
$this->subjectCb = $this->wrapValueInClosure($cb); |
78
|
6 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns the result of the subject closure |
83
|
|
|
* @param Exception $e The Exception instance that you want to build the subject around |
84
|
|
|
* @return string The subject string |
85
|
|
|
*/ |
86
|
9 |
|
public function getSubject(Exception $e) { |
87
|
9 |
|
if (is_callable($this->subjectCb)) { |
88
|
6 |
|
return $this->subjectCb->__invoke($e); |
|
|
|
|
89
|
|
|
} else { |
90
|
3 |
|
return get_class($e); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Pushes on another Monolog Handler |
96
|
|
|
* @param HandlerInterface $handler The handler instance to add on |
97
|
|
|
* @return Notifier Returns this |
98
|
|
|
*/ |
99
|
3 |
|
public function pushHandler(HandlerInterface $handler) { |
100
|
3 |
|
$this->log->pushHandler($handler); |
101
|
3 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Triggers the Monolog Logger instance to log an error to all handlers |
106
|
|
|
* @param Exception $e The exception to use |
107
|
|
|
* @param array $context Additional information that you would like to pass to Monolog |
108
|
|
|
* @return Notifier Returns this |
109
|
|
|
*/ |
110
|
3 |
|
public function send(Exception $e, array $context = []) { |
111
|
3 |
|
$factory = new MonologHandlerFactory(); |
112
|
3 |
|
$drivers = $this->drivers; |
113
|
|
|
|
114
|
3 |
|
$message = $this->getMessage($e); |
115
|
3 |
|
$subject = $this->getSubject($e); |
116
|
|
|
|
117
|
3 |
|
foreach ($drivers as $driver) { |
118
|
3 |
|
$handler = $factory->create($driver, $subject); |
119
|
3 |
|
$this->log->pushHandler($handler); |
120
|
3 |
|
} |
121
|
|
|
|
122
|
3 |
|
$context = $this->buildContext($context); |
123
|
|
|
|
124
|
3 |
|
var_dump($context); |
|
|
|
|
125
|
|
|
|
126
|
3 |
|
$this->log->addCritical($message, $context); |
127
|
|
|
|
128
|
3 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Builds a context array to pass to Monolog |
133
|
|
|
* @param array $context Additional information that you would like to pass to Monolog |
134
|
|
|
* @return array The modified context array |
135
|
|
|
*/ |
136
|
3 |
|
protected function buildContext(array $context = []){ |
137
|
3 |
|
if(in_array('pushover', $this->drivers)){ |
138
|
3 |
|
$context['sound'] = $this->config['pushover']['sound']; |
139
|
3 |
|
} |
140
|
3 |
|
return $context; |
141
|
|
|
} |
142
|
|
|
} |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.