1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor\Models; |
4
|
|
|
|
5
|
|
|
use Spatie\Url\Url; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus; |
8
|
|
|
use Spatie\UptimeMonitor\Exceptions\CannotSaveMonitor; |
9
|
|
|
use Spatie\UptimeMonitor\Models\Enums\CertificateStatus; |
10
|
|
|
use Spatie\UptimeMonitor\Models\Traits\SupportsUptimeCheck; |
11
|
|
|
use Spatie\UptimeMonitor\Models\Presenters\MonitorPresenter; |
12
|
|
|
use Spatie\UptimeMonitor\Models\Traits\SupportsCertificateCheck; |
13
|
|
|
|
14
|
|
|
class Monitor extends Model |
15
|
|
|
{ |
16
|
|
|
use SupportsUptimeCheck; |
17
|
|
|
use SupportsCertificateCheck; |
18
|
|
|
use MonitorPresenter; |
19
|
|
|
|
20
|
|
|
protected $guarded = []; |
21
|
|
|
|
22
|
|
|
protected $appends = ['raw_url']; |
23
|
|
|
|
24
|
|
|
protected $dates = [ |
25
|
|
|
'uptime_last_check_date', |
26
|
|
|
'uptime_status_last_change_date', |
27
|
|
|
'uptime_check_failed_event_fired_on_date', |
28
|
|
|
'certificate_expiration_date', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
protected $casts = [ |
32
|
|
|
'uptime_check_enabled' => 'boolean', |
33
|
|
|
'certificate_check_enabled' => 'boolean', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
public function getUptimeCheckAdditionalHeadersAttribute($additionalHeaders) |
37
|
|
|
{ |
38
|
|
|
return $additionalHeaders |
39
|
|
|
? json_decode($additionalHeaders, true) |
40
|
|
|
: []; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function setUptimeCheckAdditionalHeadersAttribute(array $additionalHeaders) |
44
|
|
|
{ |
45
|
|
|
$this->attributes['uptime_check_additional_headers'] = json_encode($additionalHeaders); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function scopeEnabled($query) |
49
|
|
|
{ |
50
|
|
|
return $query |
51
|
|
|
->where('uptime_check_enabled', true) |
52
|
|
|
->orWhere('certificate_check_enabled', true); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return \Spatie\Url\Url|null |
57
|
|
|
*/ |
58
|
|
|
public function getUrlAttribute() |
59
|
|
|
{ |
60
|
|
|
if (! isset($this->attributes['url'])) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return Url::fromString($this->attributes['url']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getRawUrlAttribute() |
71
|
|
|
{ |
72
|
|
|
return (string) $this->url; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function boot() |
76
|
|
|
{ |
77
|
|
|
parent::boot(); |
78
|
|
|
|
79
|
|
|
static::saving(function (Monitor $monitor) { |
80
|
|
|
if (static::alreadyExists($monitor)) { |
81
|
|
|
throw CannotSaveMonitor::alreadyExists($monitor); |
82
|
|
|
} |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function isHealthy() |
87
|
|
|
{ |
88
|
|
|
if ($this->uptime_check_enabled && in_array($this->uptime_status, [UptimeStatus::DOWN, UptimeStatus::NOT_YET_CHECKED])) { |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($this->certificate_check_enabled && $this->certificate_status === CertificateStatus::INVALID) { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function enable() |
103
|
|
|
{ |
104
|
|
|
$this->uptime_check_enabled = true; |
105
|
|
|
|
106
|
|
|
if ($this->url->getScheme() === 'https') { |
107
|
|
|
$this->certificate_check_enabled = true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->save(); |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function disable() |
119
|
|
|
{ |
120
|
|
|
$this->uptime_check_enabled = false; |
121
|
|
|
$this->certificate_check_enabled = false; |
122
|
|
|
|
123
|
|
|
$this->save(); |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected static function alreadyExists(self $monitor): bool |
129
|
|
|
{ |
130
|
|
|
$query = static::where('url', $monitor->url); |
131
|
|
|
|
132
|
|
|
if ($monitor->exists) { |
133
|
|
|
$query->where('id', '<>', $monitor->id); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return (bool) $query->first(); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|