|
1
|
|
|
<?php |
|
2
|
|
|
namespace Kevincobain2000\LaravelAlertNotifications\Dispatcher; |
|
3
|
|
|
|
|
4
|
|
|
use Exception; |
|
5
|
|
|
use DateTime; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\Mail; |
|
8
|
|
|
use Kevincobain2000\LaravelAlertNotifications\Mail\ExceptionOccurredMail; |
|
9
|
|
|
|
|
10
|
|
|
use Kevincobain2000\LaravelAlertNotifications\MicrosoftTeams\Teams; |
|
11
|
|
|
use Kevincobain2000\LaravelAlertNotifications\MicrosoftTeams\ExceptionOccurredCard; |
|
12
|
|
|
|
|
13
|
|
|
use Kevincobain2000\LaravelAlertNotifications\Slack\Slack; |
|
14
|
|
|
use Kevincobain2000\LaravelAlertNotifications\Slack\ExceptionOccurredPayload; |
|
15
|
|
|
|
|
16
|
|
|
use Kevincobain2000\LaravelAlertNotifications\Dispatcher\ThrottleControl; |
|
17
|
|
|
|
|
18
|
|
|
class AlertDispatcher |
|
19
|
|
|
{ |
|
20
|
|
|
public $exception; |
|
21
|
|
|
public $dontAlertExceptions; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(Exception $exception, array $dontAlertExceptions = []) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->exception = $exception; |
|
26
|
|
|
$this->dontAlertExceptions = $dontAlertExceptions; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
3 |
|
public function notify() |
|
30
|
|
|
{ |
|
31
|
3 |
|
if ($this->shouldAlert()) { |
|
32
|
3 |
|
return $this->dispatch(); |
|
33
|
|
|
} |
|
34
|
|
|
return false; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
3 |
|
protected function shouldAlert() |
|
38
|
|
|
{ |
|
39
|
3 |
|
if (!config('laravel_alert_notifications.throttle_enabled')) { |
|
40
|
|
|
return true; |
|
41
|
|
|
} |
|
42
|
3 |
|
if ($this->isDonotAlertException()) { |
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
3 |
|
return ! ThrottleControl::isThrottled($this->exception); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
3 |
|
protected function dispatch() |
|
50
|
|
|
{ |
|
51
|
3 |
|
if ($this->shouldMail()) { |
|
52
|
3 |
|
Mail::send(new ExceptionOccurredMail($this->exception)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
if ($this->shouldMicrosoftTeams($this->exception)) { |
|
|
|
|
|
|
56
|
3 |
|
Teams::send(new ExceptionOccurredCard($this->exception)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if ($this->shouldSlack($this->exception)) { |
|
|
|
|
|
|
60
|
|
|
Slack::send(new ExceptionOccurredPayload($this->exception)); |
|
61
|
|
|
} |
|
62
|
|
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
3 |
|
protected function isDonotAlertException(): bool |
|
66
|
|
|
{ |
|
67
|
3 |
|
return in_array(get_class($this->exception), $this->dontAlertExceptions); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
3 |
|
protected function shouldMail(): bool |
|
71
|
|
|
{ |
|
72
|
3 |
|
return config('laravel_alert_notifications.mail.enabled') |
|
73
|
3 |
|
&& config('laravel_alert_notifications.mail.toAddress') |
|
74
|
3 |
|
&& config('laravel_alert_notifications.mail.fromAddress'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
3 |
|
protected function shouldMicrosoftTeams(): bool |
|
78
|
|
|
{ |
|
79
|
3 |
|
return config('laravel_alert_notifications.microsoft_teams.enabled') |
|
80
|
3 |
|
&& config('laravel_alert_notifications.microsoft_teams.webhook'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function shouldSlack(): bool |
|
84
|
|
|
{ |
|
85
|
|
|
return config('laravel_alert_notifications.slack.enabled') |
|
86
|
|
|
&& config('laravel_alert_notifications.slack.webhook'); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.