1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tylercd100\LERN\Factories; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Monolog\Logger; |
7
|
|
|
|
8
|
|
|
class MonologHandlerFactory { |
9
|
|
|
|
10
|
|
|
protected $config; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Creates a handler for a specified driver |
14
|
|
|
* @param string $driver Lowercase driver string that is also in the config/lern.php file |
15
|
|
|
* @param string $subject Title or Subject line for the notification |
16
|
|
|
* @return \Monolog\Handler\HandlerInterface A handler to use with a Monolog\Logger instance |
17
|
|
|
*/ |
18
|
24 |
|
public function create($driver, $subject = null) |
19
|
|
|
{ |
20
|
24 |
|
$this->config = config('lern.notify.' . $driver); |
21
|
24 |
|
if (is_array($this->config)) { |
22
|
21 |
|
return $this->{$driver}($subject); |
23
|
|
|
} else { |
24
|
3 |
|
throw new Exception("config must be an array! You may have chosen an unsupported monolog handler."); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Creates FleepHook Monolog Handler |
30
|
|
|
* @return \Monolog\Handler\FleepHookHandler A handler to use with a Monolog\Logger instance |
31
|
|
|
*/ |
32
|
6 |
|
protected function fleephook() { |
33
|
6 |
|
return new \Monolog\Handler\FleepHookHandler( |
34
|
6 |
|
$this->config['token'] |
35
|
6 |
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Creates HipChat Monolog Handler |
40
|
|
|
* @return \Monolog\Handler\HipChatHandler A handler to use with a Monolog\Logger instance |
41
|
|
|
*/ |
42
|
6 |
|
protected function hipchat() { |
43
|
6 |
|
return new \Monolog\Handler\HipChatHandler( |
44
|
6 |
|
$this->config['token'], |
45
|
6 |
|
$this->config['room'], |
46
|
6 |
|
$this->config['name'], |
47
|
6 |
|
$this->config['notify'], |
48
|
6 |
|
Logger::CRITICAL, |
49
|
6 |
|
true, |
50
|
6 |
|
true, |
51
|
6 |
|
'text', |
52
|
6 |
|
'api.hipchat.com', |
53
|
|
|
'v2' |
54
|
6 |
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Creates Flowdock Monolog Handler |
59
|
|
|
* @return \Monolog\Handler\FlowdockHandler A handler to use with a Monolog\Logger instance |
60
|
|
|
*/ |
61
|
6 |
|
protected function flowdock() { |
62
|
6 |
|
return new \Monolog\Handler\FlowdockHandler( |
63
|
6 |
|
$this->config['token'] |
64
|
6 |
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Creates Pushover Monolog Handler |
69
|
|
|
* @param string $subject Title or Subject line for the notification |
70
|
|
|
* @return \Monolog\Handler\PushoverHandler A handler to use with a Monolog\Logger instance |
71
|
|
|
*/ |
72
|
9 |
|
protected function pushover($subject) |
73
|
|
|
{ |
74
|
9 |
|
$this->checkSubject($subject); |
75
|
9 |
|
return new \Monolog\Handler\PushoverHandler( |
76
|
9 |
|
$this->config['token'], |
77
|
9 |
|
$this->config['user'], |
78
|
|
|
$subject |
79
|
9 |
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Creates Mail Monolog Handler |
84
|
|
|
* @param string $subject Title or Subject line for the notification |
85
|
|
|
* @return \Monolog\Handler\NativeMailerHandler A handler to use with a Monolog\Logger instance |
86
|
|
|
*/ |
87
|
15 |
|
protected function mail($subject) |
88
|
|
|
{ |
89
|
15 |
|
$this->checkSubject($subject); |
90
|
9 |
|
return new \Monolog\Handler\NativeMailerHandler( |
91
|
9 |
|
$this->config['to'], |
92
|
9 |
|
$subject, |
93
|
9 |
|
$this->config['from'] |
94
|
9 |
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Creates Slack Monolog Handler |
99
|
|
|
* @return \Monolog\Handler\SlackHandler A handler to use with a Monolog\Logger instance |
100
|
|
|
*/ |
101
|
12 |
|
protected function slack() |
102
|
|
|
{ |
103
|
12 |
|
return new \Monolog\Handler\SlackHandler( |
104
|
12 |
|
$this->config['token'], |
105
|
12 |
|
$this->config['channel'], |
106
|
12 |
|
$this->config['username'] |
107
|
12 |
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Creates Plivo Monolog Handler |
112
|
|
|
* @return \Tylercd100\Monolog\Handler\PlivoHandler A handler to use with a Monolog\Logger instance |
113
|
|
|
*/ |
114
|
6 |
|
protected function plivo() |
115
|
|
|
{ |
116
|
6 |
|
return new \Tylercd100\Monolog\Handler\PlivoHandler( |
117
|
6 |
|
$this->config['token'], |
118
|
6 |
|
$this->config['auth_id'], |
119
|
6 |
|
$this->config['from'], |
120
|
6 |
|
$this->config['to'] |
121
|
6 |
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Creates Twilio Monolog Handler |
126
|
|
|
* @return \Tylercd100\Monolog\Handler\TwilioHandler A handler to use with a Monolog\Logger instance |
127
|
|
|
*/ |
128
|
6 |
|
protected function twilio() |
129
|
|
|
{ |
130
|
6 |
|
return new \Tylercd100\Monolog\Handler\TwilioHandler( |
131
|
6 |
|
$this->config['secret'], |
132
|
6 |
|
$this->config['sid'], |
133
|
6 |
|
$this->config['from'], |
134
|
6 |
|
$this->config['to'] |
135
|
6 |
|
); |
136
|
|
|
} |
137
|
|
|
/** |
138
|
|
|
* Validates that the subject is an unempty string |
139
|
|
|
* @param mixed $subject The value to check |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
18 |
|
private function checkSubject($subject) { |
143
|
18 |
|
if (empty($subject)) { |
144
|
3 |
|
throw new Exception('$subject must not be empty!'); |
145
|
|
|
} |
146
|
|
|
|
147
|
15 |
|
if (!is_string($subject)) { |
148
|
3 |
|
throw new Exception('$subject must be a string!'); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |