1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor\Models; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Spatie\UptimeMonitor\Exceptions\CannotSaveSite; |
8
|
|
|
use Spatie\UptimeMonitor\Models\Enums\SslCertificateStatus; |
9
|
|
|
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus; |
10
|
|
|
use Spatie\UptimeMonitor\Models\Presenters\SitePresenter; |
11
|
|
|
use Spatie\UptimeMonitor\Models\Traits\SupportsSslCertificateCheck; |
12
|
|
|
use Spatie\UptimeMonitor\Models\Traits\SupportsUptimeCheck; |
13
|
|
|
use Spatie\Url\Url; |
14
|
|
|
|
15
|
|
|
class Site extends Model |
16
|
|
|
{ |
17
|
|
|
use SupportsUptimeCheck, |
18
|
|
|
SupportsSslCertificateCheck, |
19
|
|
|
SitePresenter; |
20
|
|
|
|
21
|
|
|
protected $guarded = []; |
22
|
|
|
|
23
|
|
|
protected $dates = [ |
24
|
|
|
'uptime_last_check_date', |
25
|
|
|
'uptime_status_last_change_date', |
26
|
|
|
'down_event_fired_on_date', |
27
|
|
|
'ssl_certificate_expiration_date', |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
protected $casts = [ |
31
|
|
|
'enabled' => 'boolean', |
32
|
|
|
'check_ssl_certificate' => 'boolean', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
public function scopeEnabled($query) |
37
|
|
|
{ |
38
|
|
|
return $query->where('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
|
|
|
static::saving(function (Site $site) { |
53
|
|
|
if (static::alreadyExists($site)) { |
54
|
|
|
throw CannotSaveSite::alreadyExists($site); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/* |
|
|
|
|
58
|
|
|
if (is_null($site->uptime_status_last_change_date)) { |
59
|
|
|
$site->uptime_status_last_change_date = Carbon::now(); |
60
|
|
|
|
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($site->getOriginal('uptime_status') != $site->uptime_status) { |
65
|
|
|
$site->uptime_status_last_change_date = Carbon::now(); |
66
|
|
|
} |
67
|
|
|
*/ |
68
|
|
|
}); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function isHealthy() |
72
|
|
|
{ |
73
|
|
|
if (in_array($this->uptime_status, [UptimeStatus::DOWN, UptimeStatus::NOT_YET_CHECKED])) { |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($this->check_ssl_certificate && $this->ssl_certificate_status === SslCertificateStatus::INVALID) { |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected static function alreadyExists(Site $site): bool |
85
|
|
|
{ |
86
|
|
|
$query = static::where('url', $site->url); |
87
|
|
|
|
88
|
|
|
if ($site->exists) { |
89
|
|
|
$query->where('id', '<>', $site->id); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return (bool)$query->first(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.