1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor\Models\Presenters; |
4
|
|
|
|
5
|
|
|
use Spatie\UptimeMonitor\Models\Enums\CertificateStatus; |
6
|
|
|
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus; |
7
|
|
|
|
8
|
|
|
trait MonitorPresenter |
9
|
|
|
{ |
10
|
|
|
public function getUptimeStatusAsEmojiAttribute(): string |
11
|
|
|
{ |
12
|
|
|
if ($this->uptime_status === UptimeStatus::UP) { |
|
|
|
|
13
|
|
|
return '✅'; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
if ($this->uptime_status === UptimeStatus::DOWN) { |
17
|
|
|
return '❌'; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
return ''; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function getCertificateStatusAsEmojiAttribute(): string |
24
|
|
|
{ |
25
|
|
|
if ($this->certificate_status === CertificateStatus::VALID) { |
|
|
|
|
26
|
|
|
return '✅'; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if ($this->certificate_status === CertificateStatus::INVALID) { |
30
|
|
|
return '❌'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return ''; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function formattedLastUpdatedStatusChangeDate(string $format = ''): string |
37
|
|
|
{ |
38
|
|
|
return $this->formatDate('uptime_status_last_change_date', $format); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function formattedCertificateExpirationDate(string $format = ''): string |
42
|
|
|
{ |
43
|
|
|
return $this->formatDate('certificate_expiration_date', $format); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getChunkedLastFailureReasonAttribute(): string |
47
|
|
|
{ |
48
|
|
|
if ($this->uptime_check_failure_reason == '') { |
|
|
|
|
49
|
|
|
return ''; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return chunk_split($this->uptime_check_failure_reason, 30, "\n"); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getChunkedLastCertificateCheckFailureReasonAttribute(): string |
56
|
|
|
{ |
57
|
|
|
if ($this->certificate_check_failure_reason == '') { |
|
|
|
|
58
|
|
|
return ''; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return chunk_split($this->certificate_check_failure_reason, 60, "\n"); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function formatDate(string $attributeName, string $format = ''): string |
65
|
|
|
{ |
66
|
|
|
if (! $this->$attributeName) { |
67
|
|
|
return ''; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($format === 'forHumans') { |
71
|
|
|
return $this->$attributeName->diffForHumans(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($format === '') { |
75
|
|
|
$format = config('uptime-monitor.notifications.date_format'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->$attributeName->format($format); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: