1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\ServerMonitor\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Spatie\ServerMonitor\Models\Enums\HostHealth; |
8
|
|
|
use Spatie\ServerMonitor\Models\Enums\CheckStatus; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
10
|
|
|
use Spatie\ServerMonitor\Models\Presenters\HostPresenter; |
11
|
|
|
use Spatie\ServerMonitor\Models\Concerns\HasCustomProperties; |
12
|
|
|
|
13
|
|
|
class Host extends Model |
14
|
|
|
{ |
15
|
|
|
use HostPresenter, HasCustomProperties; |
16
|
|
|
|
17
|
|
|
public $casts = [ |
18
|
|
|
'custom_properties' => 'array', |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
public $guarded = []; |
22
|
|
|
|
23
|
|
|
public function checks(): HasMany |
24
|
|
|
{ |
25
|
|
|
return $this->hasMany(config('server-monitor.check_model', Check::class)); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getEnabledChecksAttribute(): Collection |
29
|
|
|
{ |
30
|
|
|
return $this->checks()->enabled()->get(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function isHealthy(): bool |
34
|
|
|
{ |
35
|
|
|
return $this->status === HostHealth::HEALTHY; |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function isUnhealthy(): bool |
39
|
|
|
{ |
40
|
|
|
return $this->status === HostHealth::UNHEALTHY; |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function hasWarning(): bool |
44
|
|
|
{ |
45
|
|
|
return $this->status === HostHealth::WARNING; |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getStatusAttribute(): string |
49
|
|
|
{ |
50
|
|
|
if ($this->enabled_checks->count() === 0) { |
|
|
|
|
51
|
|
|
return HostHealth::WARNING; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($this->enabled_checks->contains->hasStatus(CheckStatus::FAILED)) { |
|
|
|
|
55
|
|
|
return HostHealth::UNHEALTHY; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($this->enabled_checks->every->hasStatus(CheckStatus::SUCCESS)) { |
|
|
|
|
59
|
|
|
return HostHealth::HEALTHY; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return HostHealth::WARNING; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function hasCheckType(string $type): bool |
66
|
|
|
{ |
67
|
|
|
return $this->checks->contains(function (Check $check) use ($type) { |
|
|
|
|
68
|
|
|
return $check->type === $type; |
|
|
|
|
69
|
|
|
}); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.