Completed
Push — master ( 434343...7ded52 )
by Freek
02:04
created

EventHandler::determineNotifiable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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\CheckWarning;
9
use Spatie\ServerMonitor\Events\CheckRestored;
10
use Spatie\ServerMonitor\Events\CheckSucceeded;
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->notify($notification);
40
            }
41
42
            if (! $this->concernsSuccess($event)) {
43
                $notification->getCheck()->startThrottlingFailedNotifications();
44
            }
45
        });
46
    }
47
48
    protected function determineNotifiable()
49
    {
50
        $notifiableClass = $this->config->get('server-monitor.notifications.notifiable');
51
52
        return app($notifiableClass);
53
    }
54
55
    protected function determineNotification($event): ?BaseNotification
56
    {
57
        $eventName = class_basename($event);
58
59
        $notificationClass = collect($this->config->get('server-monitor.notifications.notifications'))
60
            ->filter(function (array $notificationChannels) {
61
                return count($notificationChannels);
62
            })
63
            ->keys()
64
            ->first(function ($notificationClass) use ($eventName) {
65
                $notificationName = class_basename($notificationClass);
66
67
                return $notificationName === $eventName;
68
            });
69
70
        if ($notificationClass) {
71
            return app($notificationClass)->setEvent($event);
72
        }
73
74
        return null;
75
    }
76
77
    protected function allEventClasses(): array
78
    {
79
        return [
80
            CheckSucceeded::class,
81
            CheckRestored::class,
82
            CheckWarning::class,
83
            CheckFailed::class,
84
        ];
85
    }
86
87
    protected function concernsSuccess(Event $event): bool
88
    {
89
        return in_array(get_class($event), [
90
            CheckSucceeded::class,
91
            CheckRestored::class,
92
        ]);
93
    }
94
}
95