1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tylercd100\LERN\Notifications; |
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 array $subject Title or Subject line for the notification |
16
|
|
|
* @return \Monolog\Handler\HandlerInterface A handler to use with a Monolog\Logger instance |
17
|
|
|
*/ |
18
|
|
|
public function create($driver, $subject = null) |
19
|
|
|
{ |
20
|
|
|
$this->config = config('lern.notify.' . $driver); |
21
|
|
|
if (is_array($this->config)) { |
22
|
|
|
return $this->{$driver}($subject); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Creates FleepHook Monolog Handler |
28
|
|
|
* @return \Monolog\Handler\FleepHookHandler A handler to use with a Monolog\Logger instance |
29
|
|
|
*/ |
30
|
|
|
protected function fleephook(){ |
31
|
|
|
return new \Monolog\Handler\FleepHookHandler( |
32
|
|
|
$this->config['token'], |
33
|
|
|
Logger::ERROR |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Creates HipChat Monolog Handler |
39
|
|
|
* @return \Monolog\Handler\HipChatHandler A handler to use with a Monolog\Logger instance |
40
|
|
|
*/ |
41
|
|
|
protected function hipchat(){ |
42
|
|
|
return new \Monolog\Handler\HipChatHandler( |
43
|
|
|
$this->config['token'], |
44
|
|
|
$this->config['room'], |
45
|
|
|
$this->config['name'], |
46
|
|
|
$this->config['notify'], |
47
|
|
|
Logger::ERROR |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Creates Flowdock Monolog Handler |
53
|
|
|
* @return \Monolog\Handler\FlowdockHandler A handler to use with a Monolog\Logger instance |
54
|
|
|
*/ |
55
|
|
|
protected function flowdock(){ |
56
|
|
|
return new \Monolog\Handler\FlowdockHandler( |
57
|
|
|
$this->config['token'], |
58
|
|
|
Logger::ERROR |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Creates Pushover Monolog Handler |
64
|
|
|
* @param array $subject Title or Subject line for the notification |
65
|
|
|
* @return \Monolog\Handler\PushoverHandler A handler to use with a Monolog\Logger instance |
66
|
|
|
*/ |
67
|
|
|
protected function pushover($subject) |
68
|
|
|
{ |
69
|
|
|
$this->checkSubject($subject); |
70
|
|
|
return new \Monolog\Handler\PushoverHandler( |
71
|
|
|
$this->config['token'], |
72
|
|
|
$this->config['user'], |
73
|
|
|
$subject, |
|
|
|
|
74
|
|
|
Logger::ERROR |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Creates Mail Monolog Handler |
80
|
|
|
* @param array $subject Title or Subject line for the notification |
81
|
|
|
* @return \Monolog\Handler\NativeMailerHandler A handler to use with a Monolog\Logger instance |
82
|
|
|
*/ |
83
|
|
|
protected function mail($subject) |
84
|
|
|
{ |
85
|
|
|
$this->checkSubject($subject); |
86
|
|
|
return new \Monolog\Handler\NativeMailerHandler( |
87
|
|
|
$this->config['to'], |
88
|
|
|
$subject, |
|
|
|
|
89
|
|
|
$this->config['from'] |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Creates Slack Monolog Handler |
95
|
|
|
* @return \Monolog\Handler\SlackHandler A handler to use with a Monolog\Logger instance |
96
|
|
|
*/ |
97
|
|
|
protected function slack() |
98
|
|
|
{ |
99
|
|
|
return new \Monolog\Handler\SlackHandler( |
100
|
|
|
$this->config['token'], |
101
|
|
|
$this->config['channel'], |
102
|
|
|
$this->config['username'] |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Validates that the subject is an unempty string |
108
|
|
|
* @param mixed $subject [description] |
109
|
|
|
* @return [type] [description] |
|
|
|
|
110
|
|
|
*/ |
111
|
|
|
private function checkSubject($subject) { |
112
|
|
|
if (empty($subject)) { |
113
|
|
|
throw new Exception('$subject must not be empty!'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (!is_string($subject)) { |
117
|
|
|
throw new Exception('$subject must be a string!'); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: