|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor\Notifications; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository; |
|
6
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
|
7
|
|
|
use Spatie\UptimeMonitor\Events\CertificateCheckFailed; |
|
8
|
|
|
use Spatie\UptimeMonitor\Events\CertificateCheckSucceeded; |
|
9
|
|
|
use Spatie\UptimeMonitor\Events\CertificateExpiresSoon; |
|
10
|
|
|
use Spatie\UptimeMonitor\Events\UptimeCheckFailed; |
|
11
|
|
|
use Spatie\UptimeMonitor\Events\UptimeCheckRecovered; |
|
12
|
|
|
use Spatie\UptimeMonitor\Events\UptimeCheckSucceeded; |
|
13
|
|
|
|
|
14
|
|
|
class EventHandler |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var \Illuminate\Config\Repository */ |
|
17
|
|
|
protected $config; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(Repository $config) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->config = $config; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function subscribe(Dispatcher $events) |
|
25
|
|
|
{ |
|
26
|
|
|
$events->listen($this->allEventClasses(), function ($event) { |
|
27
|
|
|
$notification = $this->determineNotification($event); |
|
28
|
|
|
|
|
29
|
|
|
if (! $notification) { |
|
30
|
|
|
return; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if ($notification->isStillRelevant()) { |
|
34
|
|
|
$notifiable = $this->determineNotifiable(); |
|
35
|
|
|
|
|
36
|
|
|
$notifiable->notify($notification); |
|
37
|
|
|
} |
|
38
|
|
|
}); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
protected function determineNotifiable() |
|
42
|
|
|
{ |
|
43
|
|
|
$notifiableClass = $this->config->get('uptime-monitor.notifications.notifiable'); |
|
44
|
|
|
|
|
45
|
|
|
return app($notifiableClass); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function determineNotification($event) |
|
49
|
|
|
{ |
|
50
|
|
|
$eventName = class_basename($event); |
|
51
|
|
|
|
|
52
|
|
|
$notificationClass = collect($this->config->get('uptime-monitor.notifications.notifications')) |
|
53
|
|
|
->filter(function (array $notificationChannels) { |
|
54
|
|
|
return count($notificationChannels); |
|
55
|
|
|
}) |
|
56
|
|
|
->keys() |
|
57
|
|
|
->first(function ($notificationClass) use ($eventName) { |
|
58
|
|
|
$notificationName = class_basename($notificationClass); |
|
59
|
|
|
|
|
60
|
|
|
return $notificationName === $eventName; |
|
61
|
|
|
}); |
|
62
|
|
|
|
|
63
|
|
|
if ($notificationClass) { |
|
64
|
|
|
return app($notificationClass)->setEvent($event); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function allEventClasses(): array |
|
69
|
|
|
{ |
|
70
|
|
|
return [ |
|
71
|
|
|
UptimeCheckFailed::class, |
|
72
|
|
|
UptimeCheckSucceeded::class, |
|
73
|
|
|
UptimeCheckRecovered::class, |
|
74
|
|
|
CertificateCheckSucceeded::class, |
|
75
|
|
|
CertificateCheckFailed::class, |
|
76
|
|
|
CertificateExpiresSoon::class, |
|
77
|
|
|
]; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|