1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Spatie\UptimeMonitor\Exceptions\InvalidConfiguration; |
8
|
|
|
use Spatie\UptimeMonitor\Models\Enums\CertificateStatus; |
9
|
|
|
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus; |
10
|
|
|
use Spatie\UptimeMonitor\Models\Monitor; |
11
|
|
|
|
12
|
|
|
class MonitorRepository |
13
|
|
|
{ |
14
|
|
|
public static function getEnabled(): Collection |
15
|
|
|
{ |
16
|
|
|
$monitors = self::query()->get(); |
17
|
|
|
|
18
|
|
|
return MonitorCollection::make($monitors)->sortByHost(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public static function getDisabled(): Collection |
22
|
|
|
{ |
23
|
|
|
$modelClass = static::determineMonitorModel(); |
24
|
|
|
|
25
|
|
|
$monitors = $modelClass::where('uptime_check_enabled', false) |
26
|
|
|
->where('certificate_check_enabled', false) |
27
|
|
|
->get(); |
28
|
|
|
|
29
|
|
|
return MonitorCollection::make($monitors)->sortByHost(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function getForUptimeCheck(): MonitorCollection |
33
|
|
|
{ |
34
|
|
|
$monitors = self::query()->get()->filter->shouldCheckUptime(); |
35
|
|
|
|
36
|
|
|
return MonitorCollection::make($monitors)->sortByHost(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function getForCertificateCheck(): Collection |
40
|
|
|
{ |
41
|
|
|
$monitors = self::query() |
42
|
|
|
->where('certificate_check_enabled', true) |
43
|
|
|
->get(); |
44
|
|
|
|
45
|
|
|
return MonitorCollection::make($monitors)->sortByHost(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function getHealthy(): Collection |
49
|
|
|
{ |
50
|
|
|
$monitors = self::query()->get()->filter->isHealthy(); |
51
|
|
|
|
52
|
|
|
return MonitorCollection::make($monitors)->sortByHost(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
public static function getWithFailingUptimeCheck(): Collection |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$monitors = self::query() |
58
|
|
|
->where('uptime_check_enabled', true) |
59
|
|
|
->where('uptime_status', UptimeStatus::DOWN) |
60
|
|
|
->get(); |
61
|
|
|
|
62
|
|
|
return MonitorCollection::make($monitors)->sortByHost(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
public static function getWithFailingCertificateCheck(): Collection |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$monitors = self::query() |
68
|
|
|
->where('certificate_check_enabled', true) |
69
|
|
|
->where('certificate_status', CertificateStatus::INVALID) |
70
|
|
|
->get(); |
71
|
|
|
|
72
|
|
|
return MonitorCollection::make($monitors)->sortByHost(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function getUnhealthy(): Collection |
76
|
|
|
{ |
77
|
|
|
$monitors = self::query()->get()->reject->isHealthy(); |
78
|
|
|
|
79
|
|
|
return MonitorCollection::make($monitors)->sortByHost(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public static function getUnchecked(): Collection |
83
|
|
|
{ |
84
|
|
|
$modelClass = static::determineMonitorModel(); |
85
|
|
|
|
86
|
|
|
$monitors = $modelClass |
87
|
|
|
::where(function (Builder $query) { |
88
|
|
|
$query |
89
|
|
|
->where('uptime_check_enabled', true) |
90
|
|
|
->where('uptime_status', UptimeStatus::NOT_YET_CHECKED); |
91
|
|
|
}) |
92
|
|
|
->orWhere(function (Builder $query) { |
93
|
|
|
$query |
94
|
|
|
->where('certificate_check_enabled', true) |
95
|
|
|
->where('certificate_status', CertificateStatus::NOT_YET_CHECKED); |
96
|
|
|
}) |
97
|
|
|
->get(); |
98
|
|
|
|
99
|
|
|
return MonitorCollection::make($monitors)->sortByHost(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string|\Spatie\Url\Url $url |
104
|
|
|
* |
105
|
|
|
* @return \Spatie\UptimeMonitor\Models\Monitor |
106
|
|
|
*/ |
107
|
|
|
public static function findByUrl($url) |
108
|
|
|
{ |
109
|
|
|
$model = static::determineMonitorModel(); |
110
|
|
|
|
111
|
|
|
return $model::where('url', (string) $url)->first(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected static function query() |
115
|
|
|
{ |
116
|
|
|
$modelClass = static::determineMonitorModel(); |
117
|
|
|
|
118
|
|
|
return $modelClass::enabled(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected static function determineMonitorModel(): string |
122
|
|
|
{ |
123
|
|
|
$monitorModel = config('uptime-monitor.monitor_model') ?? Monitor::class; |
124
|
|
|
|
125
|
|
|
if (! is_a($monitorModel, Monitor::class, true)) { |
126
|
|
|
throw InvalidConfiguration::modelIsNotValid($monitorModel); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $monitorModel; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.