|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor\Notifications\Notifications; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Notifications\Messages\MailMessage; |
|
7
|
|
|
use Illuminate\Notifications\Messages\SlackAttachment; |
|
8
|
|
|
use Illuminate\Notifications\Messages\SlackMessage; |
|
9
|
|
|
use Spatie\UptimeMonitor\Events\UptimeCheckRecovered as MonitorRecoveredEvent; |
|
10
|
|
|
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus; |
|
11
|
|
|
use Spatie\UptimeMonitor\Notifications\BaseNotification; |
|
12
|
|
|
|
|
13
|
|
|
class UptimeCheckRecovered extends BaseNotification |
|
14
|
|
|
{ |
|
15
|
|
|
public MonitorRecoveredEvent $event; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Get the mail representation of the notification. |
|
19
|
|
|
* |
|
20
|
|
|
* @param mixed $notifiable |
|
21
|
|
|
* @return \Illuminate\Notifications\Messages\MailMessage |
|
22
|
|
|
*/ |
|
23
|
|
|
public function toMail($notifiable) |
|
24
|
|
|
{ |
|
25
|
|
|
$mailMessage = (new MailMessage) |
|
26
|
|
|
->success() |
|
27
|
|
|
->subject($this->getMessageText()) |
|
28
|
|
|
->line($this->getMessageText()) |
|
29
|
|
|
->line($this->getLocationDescription()); |
|
30
|
|
|
|
|
31
|
|
|
foreach ($this->getMonitorProperties() as $name => $value) { |
|
32
|
|
|
$mailMessage->line($name.': '.$value); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return $mailMessage; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function toSlack($notifiable) |
|
39
|
|
|
{ |
|
40
|
|
|
return (new SlackMessage) |
|
41
|
|
|
->success() |
|
42
|
|
|
->attachment(function (SlackAttachment $attachment) { |
|
43
|
|
|
$attachment |
|
44
|
|
|
->title($this->getMessageText()) |
|
45
|
|
|
->fallback($this->getMessageText()) |
|
46
|
|
|
->footer($this->getLocationDescription()) |
|
47
|
|
|
->timestamp(Carbon::now()); |
|
48
|
|
|
}); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getMonitorProperties($extraProperties = []): array |
|
52
|
|
|
{ |
|
53
|
|
|
$extraProperties = [ |
|
54
|
|
|
"Downtime: {$this->event->downtimePeriod->duration()}" => $this->event->downtimePeriod->toText(), |
|
55
|
|
|
]; |
|
56
|
|
|
|
|
57
|
|
|
return parent::getMonitorProperties($extraProperties); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function isStillRelevant(): bool |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->getMonitor()->uptime_status == UptimeStatus::UP; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function setEvent(MonitorRecoveredEvent $event): self |
|
66
|
|
|
{ |
|
67
|
|
|
$this->event = $event; |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getMessageText(): string |
|
73
|
|
|
{ |
|
74
|
|
|
return "{$this->getMonitor()->url} has recovered after {$this->event->downtimePeriod->duration()}"; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|