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 $dates = [ |
23
|
|
|
'uptime_last_check_date', |
24
|
|
|
'uptime_status_last_change_date', |
25
|
|
|
'uptime_check_failed_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 getUptimeCheckAdditionalHeadersAttribute($additionalHeaders) |
35
|
|
|
{ |
36
|
|
|
return $additionalHeaders |
37
|
|
|
? json_decode($additionalHeaders, true) |
38
|
|
|
: []; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setUptimeCheckAdditionalHeadersAttribute(array $additionalHeaders) |
42
|
|
|
{ |
43
|
|
|
$this->attributes['uptime_check_additional_headers'] = json_encode($additionalHeaders); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function scopeEnabled($query) |
47
|
|
|
{ |
48
|
|
|
return $query |
49
|
|
|
->where('uptime_check_enabled', true) |
50
|
|
|
->orWhere('certificate_check_enabled', true); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return \Spatie\Url\Url|null |
55
|
|
|
*/ |
56
|
|
|
public function getUrlAttribute() |
57
|
|
|
{ |
58
|
|
|
if (! isset($this->attributes['url'])) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return Url::fromString($this->attributes['url']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function boot() |
66
|
|
|
{ |
67
|
|
|
parent::boot(); |
68
|
|
|
|
69
|
|
|
static::saving(function (Monitor $monitor) { |
70
|
|
|
if (static::alreadyExists($monitor)) { |
71
|
|
|
throw CannotSaveMonitor::alreadyExists($monitor); |
72
|
|
|
} |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function isHealthy() |
77
|
|
|
{ |
78
|
|
|
if ($this->uptime_check_enabled && in_array($this->uptime_status, [UptimeStatus::DOWN, UptimeStatus::NOT_YET_CHECKED])) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($this->certificate_check_enabled && $this->certificate_status === CertificateStatus::INVALID) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
|
|
public function enable() |
93
|
|
|
{ |
94
|
|
|
$this->uptime_check_enabled = true; |
95
|
|
|
|
96
|
|
|
if ($this->url->getScheme() === 'https') { |
97
|
|
|
$this->certificate_check_enabled = true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->save(); |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function disable() |
109
|
|
|
{ |
110
|
|
|
$this->uptime_check_enabled = false; |
111
|
|
|
$this->certificate_check_enabled = false; |
112
|
|
|
|
113
|
|
|
$this->save(); |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected static function alreadyExists(Monitor $monitor): bool |
119
|
|
|
{ |
120
|
|
|
$query = static::where('url', $monitor->url); |
121
|
|
|
|
122
|
|
|
if ($monitor->exists) { |
123
|
|
|
$query->where('id', '<>', $monitor->id); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return (bool) $query->first(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|