1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Spatie\UptimeMonitor\Exceptions\InvalidConfiguration; |
7
|
|
|
use Spatie\UptimeMonitor\Models\Enums\SslCertificateStatus; |
8
|
|
|
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus; |
9
|
|
|
use Spatie\UptimeMonitor\Models\Monitor; |
10
|
|
|
|
11
|
|
|
class MonitorRepository |
12
|
|
|
{ |
13
|
|
|
public static function getEnabled(): Collection |
14
|
|
|
{ |
15
|
|
|
return self::query() |
16
|
|
|
->get() |
17
|
|
|
->sortByHost(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public static function getDisabled(): Collection |
21
|
|
|
{ |
22
|
|
|
$modelClass = static::determineMonitorModel(); |
23
|
|
|
|
24
|
|
|
return $modelClass::where('enabled', false)->get(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function getForUptimeCheck(): MonitorCollection |
28
|
|
|
{ |
29
|
|
|
$monitors = self::query() |
30
|
|
|
->get() |
31
|
|
|
->filter(function (Monitor $monitor) { |
32
|
|
|
return $monitor->shouldCheckUptime(); |
33
|
|
|
}) |
34
|
|
|
->sortByHost(); |
35
|
|
|
|
36
|
|
|
return new MonitorCollection($monitors); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function getForSslCheck(): Collection |
40
|
|
|
{ |
41
|
|
|
return self::query() |
42
|
|
|
->where('check_ssl_certificate', true) |
43
|
|
|
->get() |
44
|
|
|
->sortByHost(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function getHealthy(): Collection |
48
|
|
|
{ |
49
|
|
|
return self::query() |
50
|
|
|
->get() |
51
|
|
|
->filter(function (Monitor $monitor) { |
52
|
|
|
return $monitor->isHealthy(); |
53
|
|
|
}) |
54
|
|
|
->sortByHost(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function getFailing(): Collection |
58
|
|
|
{ |
59
|
|
|
return self::query() |
60
|
|
|
->where('uptime_status', UptimeStatus::DOWN) |
61
|
|
|
->get() |
62
|
|
|
->sortByHost(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function getWithSslProblems(): Collection |
66
|
|
|
{ |
67
|
|
|
return self::query() |
68
|
|
|
->where('check_ssl_certificate', true) |
69
|
|
|
->where('ssl_certificate_status', SslCertificateStatus::INVALID) |
70
|
|
|
->get() |
71
|
|
|
->sortByHost(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public static function getUnhealthy(): Collection |
75
|
|
|
{ |
76
|
|
|
return self::query() |
77
|
|
|
->get() |
78
|
|
|
->reject(function (Monitor $monitor) { |
79
|
|
|
return $monitor->isHealthy(); |
80
|
|
|
}) |
81
|
|
|
->sortByHost(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public static function getUnchecked(): Collection |
85
|
|
|
{ |
86
|
|
|
return self::query() |
87
|
|
|
->where('uptime_status', UptimeStatus::NOT_YET_CHECKED) |
88
|
|
|
->get() |
89
|
|
|
->sortByHost(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string|\Spatie\Url\Url $url |
94
|
|
|
* |
95
|
|
|
* @return \Spatie\UptimeMonitor\Models\Monitor |
96
|
|
|
*/ |
97
|
|
|
public static function findByUrl($url) |
98
|
|
|
{ |
99
|
|
|
$model = static::determineMonitorModel(); |
100
|
|
|
|
101
|
|
|
return $model::where('url', (string) $url)->first(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected static function query() |
105
|
|
|
{ |
106
|
|
|
$modelClass = static::determineMonitorModel(); |
107
|
|
|
|
108
|
|
|
return $modelClass::enabled(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected static function determineMonitorModel(): string |
112
|
|
|
{ |
113
|
|
|
$monitorModel = config('laravel-uptime-monitor.monitor_model') ?? Monitor::class; |
114
|
|
|
|
115
|
|
|
if (! is_a($monitorModel, Monitor::class, true)) { |
116
|
|
|
throw InvalidConfiguration::modelIsNotValid($monitorModel); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $monitorModel; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|