EventHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\ServerMonitor\Notifications;
4
5
use Illuminate\Config\Repository;
6
use Illuminate\Contracts\Events\Dispatcher;
7
use Spatie\ServerMonitor\Events\CheckFailed;
8
use Spatie\ServerMonitor\Events\CheckRestored;
9
use Spatie\ServerMonitor\Events\CheckSucceeded;
10
use Spatie\ServerMonitor\Events\CheckWarning;
11
use Spatie\ServerMonitor\Events\Event;
12
13
class EventHandler
14
{
15
    /** @var \Illuminate\Config\Repository */
16
    protected $config;
17
18
    public function __construct(Repository $config)
19
    {
20
        $this->config = $config;
21
    }
22
23
    public function subscribe(Dispatcher $events)
24
    {
25
        $events->listen($this->allEventClasses(), function ($event) {
26
            $notification = $this->determineNotification($event);
27
28
            if (! $notification) {
29
                return;
30
            }
31
32
            if ($this->concernsSuccess($event)) {
33
                $notification->getCheck()->stopThrottlingFailedNotifications();
34
            }
35
36
            if ($notification->shouldSend()) {
37
                $notifiable = $this->determineNotifiable();
38
39
                $notifiable->setEvent($event);
40
41
                $notifiable->notify($notification);
42
            }
43
44
            if (! $this->concernsSuccess($event) && ! $notification->getCheck()->isThrottlingFailedNotifications()) {
45
                $notification->getCheck()->startThrottlingFailedNotifications();
46
            }
47
        });
48
    }
49
50
    protected function determineNotifiable()
51
    {
52
        $notifiableClass = $this->config->get('server-monitor.notifications.notifiable');
53
54
        return app($notifiableClass);
55
    }
56
57
    protected function determineNotification($event): ?BaseNotification
58
    {
59
        $eventName = class_basename($event);
60
61
        $notificationClass = collect($this->config->get('server-monitor.notifications.notifications'))
62
            ->filter(function (array $notificationChannels) {
63
                return count($notificationChannels);
64
            })
65
            ->keys()
66
            ->first(function ($notificationClass) use ($eventName) {
67
                $notificationName = class_basename($notificationClass);
68
69
                return $notificationName === $eventName;
70
            });
71
72
        if ($notificationClass) {
73
            return app($notificationClass)->setEvent($event);
74
        }
75
76
        return null;
77
    }
78
79
    protected function allEventClasses(): array
80
    {
81
        return [
82
            CheckSucceeded::class,
83
            CheckRestored::class,
84
            CheckWarning::class,
85
            CheckFailed::class,
86
        ];
87
    }
88
89
    protected function concernsSuccess(Event $event): bool
90
    {
91
        return in_array(get_class($event), [
92
            CheckSucceeded::class,
93
            CheckRestored::class,
94
        ]);
95
    }
96
}
97