Passed
Pull Request — master (#9)
by
unknown
04:32
created

AlertDispatcher::notify()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 1
b 0
f 1
ccs 3
cts 4
cp 0.75
cc 2
nc 2
nop 0
crap 2.0625
1
<?php
2
3
namespace Kevincobain2000\LaravelAlertNotifications\Dispatcher;
4
5
use Exception;
6
use Illuminate\Support\Facades\Mail;
7
use Kevincobain2000\LaravelAlertNotifications\Mail\ExceptionOccurredMail;
8
use Kevincobain2000\LaravelAlertNotifications\MicrosoftTeams\ExceptionOccurredCard;
9
use Kevincobain2000\LaravelAlertNotifications\MicrosoftTeams\Teams;
10
use Kevincobain2000\LaravelAlertNotifications\Slack\ExceptionOccurredPayload;
11
use Kevincobain2000\LaravelAlertNotifications\Slack\Slack;
12
13
class AlertDispatcher
14
{
15
    public $exception;
16
    public $exceptionContext;
17
    public $dontAlertExceptions;
18
    public $notificationLevel;
19
20 12
    public function __construct(
21
        Exception $exception,
22
        array $dontAlertExceptions = [],
23
        array $notificationLevelsMapping = [],
24
        array $exceptionContext = []
25
    ) {
26 12
        $this->exception           = $exception;
27 12
        $this->exceptionContext    = $exceptionContext;
28 12
        $this->dontAlertExceptions = $dontAlertExceptions;
29 12
        $this->notificationLevel   = $notificationLevelsMapping[get_class($exception)]
30 3
            ?? config('laravel_alert_notifications.default_notification_level');
31 12
    }
32
33 3
    public function notify()
34
    {
35 3
        if ($this->shouldAlert()) {
36
            return $this->dispatch();
37
        }
38
39 3
        return false;
40
    }
41
42 6
    protected function shouldAlert()
43
    {
44 6
        $levelsNotToNotify = config('laravel_alert_notifications.exclude_notification_levels') ?? [];
45 6
        if ($this->isDonotAlertException() || in_array($this->notificationLevel, $levelsNotToNotify)) {
46 3
            return false;
47
        }
48
49 3
        if (! config('laravel_alert_notifications.throttle_enabled')) {
50
            return true;
51
        }
52
53 3
        return ! ThrottleControl::isThrottled($this->exception);
54
    }
55
56
    protected function dispatch()
57
    {
58
        if ($this->shouldMail()) {
59
            Mail::send(new ExceptionOccurredMail($this->exception, $this->notificationLevel, $this->exceptionContext));
60
        }
61
62
        if ($this->shouldMicrosoftTeams()) {
63
            Teams::send(new ExceptionOccurredCard($this->exception, $this->exceptionContext));
64
        }
65
66
        if ($this->shouldSlack()) {
67
            Slack::send(new ExceptionOccurredPayload($this->exception, $this->exceptionContext));
68
        }
69
70
        return true;
71
    }
72
73 9
    protected function isDonotAlertException(): bool
74
    {
75 9
        return in_array(get_class($this->exception), $this->dontAlertExceptions);
76
    }
77
78 3
    protected function shouldMail(): bool
79
    {
80 3
        return config('laravel_alert_notifications.mail.enabled')
81 3
            && config('laravel_alert_notifications.mail.toAddress');
82
    }
83
84 3
    protected function shouldMicrosoftTeams(): bool
85
    {
86 3
        return config('laravel_alert_notifications.microsoft_teams.enabled')
87 3
            && config('laravel_alert_notifications.microsoft_teams.webhook');
88
    }
89
90 3
    protected function shouldSlack(): bool
91
    {
92 3
        return config('laravel_alert_notifications.slack.enabled')
93 3
            && config('laravel_alert_notifications.slack.webhook');
94
    }
95
}
96