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