Completed
Push — master ( 07b006...66391b )
by Pulkit
03:51
created

AlertDispatcher::shouldMicrosoftTeams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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 3
    public function __construct(Exception $exception, array $dontAlertExceptions = [])
24
    {
25 3
        $this->exception = $exception;
26 3
        $this->dontAlertExceptions = $dontAlertExceptions;
27 3
    }
28
29
    public function notify()
30
    {
31
        if ($this->shouldAlert()) {
32
            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
    protected function dispatch()
50
    {
51
        if ($this->shouldMail()) {
52
            Mail::send(new ExceptionOccurredMail($this->exception));
53
        }
54
55
        if ($this->shouldMicrosoftTeams($this->exception)) {
0 ignored issues
show
Unused Code introduced by
The call to Kevincobain2000\LaravelA...:shouldMicrosoftTeams() has too many arguments starting with $this->exception. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        if ($this->/** @scrutinizer ignore-call */ shouldMicrosoftTeams($this->exception)) {

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.

Loading history...
56
            Teams::send(new ExceptionOccurredCard($this->exception));
57
        }
58
59
        if ($this->shouldSlack($this->exception)) {
0 ignored issues
show
Unused Code introduced by
The call to Kevincobain2000\LaravelA...spatcher::shouldSlack() has too many arguments starting with $this->exception. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        if ($this->/** @scrutinizer ignore-call */ shouldSlack($this->exception)) {

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.

Loading history...
60
            Slack::send(new ExceptionOccurredPayload($this->exception));
61
        }
62
        return true;
63
    }
64
65 6
    protected function isDonotAlertException(): bool
66
    {
67 6
        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 3
    protected function shouldSlack(): bool
84
    {
85 3
        return config('laravel_alert_notifications.slack.enabled')
86 3
            && config('laravel_alert_notifications.slack.webhook');
87
    }
88
}
89