1 | <?php |
||
11 | class Notifier extends Component { |
||
12 | protected $config; |
||
13 | protected $log; |
||
14 | protected $messageCb; |
||
15 | protected $subjectCb; |
||
16 | protected $drivers; |
||
17 | |||
18 | /** |
||
19 | * You can provide a Monolog Logger instance to use in the constructor |
||
20 | * @param Logger|null $log Logger instance to use |
||
21 | */ |
||
22 | 48 | public function __construct(Logger $log = null) { |
|
31 | |||
32 | /** |
||
33 | * Transforms a value into a closure that returns itself when called |
||
34 | * @param callable|string $cb The value that you want to wrap in a closure |
||
35 | * @return callable |
||
36 | */ |
||
37 | 12 | private function wrapValueInClosure($cb) { |
|
44 | |||
45 | /** |
||
46 | * Set a string or a closure to be called that will generate the message body for the notification |
||
47 | * @param callable|string $cb A closure or string that will be set for the message |
||
48 | */ |
||
49 | 6 | public function setMessage($cb) |
|
54 | |||
55 | /** |
||
56 | * Returns the result of the message closure |
||
57 | * @param Exception $e The Exception instance that you want to build the message around |
||
58 | * @return string The message string |
||
59 | */ |
||
60 | 12 | public function getMessage(Exception $e) { |
|
71 | |||
72 | /** |
||
73 | * Set a string or a closure to be called that will generate the subject line for the notification |
||
74 | * @param callable|string $cb A closure or string that will be set for the subject line |
||
75 | */ |
||
76 | 6 | public function setSubject($cb) |
|
81 | |||
82 | /** |
||
83 | * Returns the result of the subject closure |
||
84 | * @param Exception $e The Exception instance that you want to build the subject around |
||
85 | * @return string The subject string |
||
86 | */ |
||
87 | 12 | public function getSubject(Exception $e) { |
|
94 | |||
95 | /** |
||
96 | * Pushes on another Monolog Handler |
||
97 | * @param HandlerInterface $handler The handler instance to add on |
||
98 | * @return Notifier Returns this |
||
99 | */ |
||
100 | 6 | public function pushHandler(HandlerInterface $handler) { |
|
104 | |||
105 | /** |
||
106 | * Triggers the Monolog Logger instance to log an error to all handlers |
||
107 | * @param Exception $e The exception to use |
||
108 | * @param array $context Additional information that you would like to pass to Monolog |
||
109 | * @return bool |
||
110 | */ |
||
111 | 9 | public function send(Exception $e, array $context = []) { |
|
112 | |||
113 | 9 | if($this->shouldntHandle($e)){ |
|
114 | 3 | return false; |
|
115 | } |
||
116 | |||
117 | 6 | $factory = new MonologHandlerFactory(); |
|
118 | 6 | $drivers = $this->drivers; |
|
119 | |||
120 | 6 | $message = $this->getMessage($e); |
|
121 | 6 | $subject = $this->getSubject($e); |
|
122 | |||
123 | try{ |
||
124 | 6 | foreach ($drivers as $driver) { |
|
125 | 6 | $handler = $factory->create($driver, $subject); |
|
126 | 6 | $this->log->pushHandler($handler); |
|
127 | 6 | } |
|
128 | |||
129 | 6 | $context = $this->buildContext($context); |
|
130 | |||
131 | 6 | $this->log->addCritical($message, $context); |
|
132 | |||
133 | 3 | return true; |
|
134 | 3 | } catch (Exception $e) { |
|
135 | 3 | throw new NotifierFailedException($e->getMessage(), $e->getCode(), $e); |
|
136 | } |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Builds a context array to pass to Monolog |
||
141 | * @param array $context Additional information that you would like to pass to Monolog |
||
142 | * @return array The modified context array |
||
143 | */ |
||
144 | 6 | protected function buildContext(array $context = []){ |
|
150 | } |