1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor\Notifications\Notifications; |
4
|
|
|
|
5
|
|
|
use Illuminate\Notifications\Messages\MailMessage; |
6
|
|
|
use Illuminate\Notifications\Messages\SlackAttachment; |
7
|
|
|
use Illuminate\Notifications\Messages\SlackMessage; |
8
|
|
|
use Spatie\UptimeMonitor\Events\UptimeCheckRecovered as MonitorRecoveredEvent; |
9
|
|
|
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus; |
10
|
|
|
use Spatie\UptimeMonitor\Notifications\BaseNotification; |
11
|
|
|
|
12
|
|
|
class UptimeCheckRecovered extends BaseNotification |
13
|
|
|
{ |
14
|
|
|
/** @var \Spatie\UptimeMonitor\Events\UptimeCheckRecovered */ |
15
|
|
|
public $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
|
|
|
|
30
|
|
|
foreach ($this->getMonitorProperties() as $name => $value) { |
31
|
|
|
$mailMessage->line($name.': '.$value); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $mailMessage; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function toSlack($notifiable) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
return (new SlackMessage) |
40
|
|
|
->success() |
41
|
|
|
->content($this->getMessageText()) |
42
|
|
|
->attachment(function (SlackAttachment $attachment) { |
43
|
|
|
$attachment->fields($this->getMonitorProperties()); |
44
|
|
|
}); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getMonitorProperties($extraProperties = []): array |
48
|
|
|
{ |
49
|
|
|
$extraProperties = [ |
50
|
|
|
'Back online since' => $this->event->monitor->formattedLastUpdatedStatusChangeDate, |
51
|
|
|
'Offline period length' => $this->event, |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
if ($failureStartDate = $this->event->uptimeCheckStartedFailingOnDate) { |
55
|
|
|
$extraProperties['Offline period length'] = $failureStartDate->diffForHumans(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return parent::getMonitorProperties($extraProperties); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function isStillRelevant(): bool |
62
|
|
|
{ |
63
|
|
|
return $this->event->monitor->uptime_status == UptimeStatus::UP; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function setEvent(MonitorRecoveredEvent $event) |
67
|
|
|
{ |
68
|
|
|
$this->event = $event; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getMessageText(): string |
74
|
|
|
{ |
75
|
|
|
return "{$this->event->monitor->url} has recovered{$this->getLocationDescription()}."; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.