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