Check::hasStatus()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\ServerMonitor\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9
use Spatie\ServerMonitor\CheckDefinitions\CheckDefinition;
10
use Spatie\ServerMonitor\Events\CheckRestored;
11
use Spatie\ServerMonitor\Exceptions\InvalidCheckDefinition;
12
use Spatie\ServerMonitor\Models\Concerns\HandlesCheckResult;
13
use Spatie\ServerMonitor\Models\Concerns\HasCustomProperties;
14
use Spatie\ServerMonitor\Models\Concerns\HasProcess;
15
use Spatie\ServerMonitor\Models\Concerns\ThrottlesFailingNotifications;
16
use Spatie\ServerMonitor\Models\Enums\CheckStatus;
17
use Spatie\ServerMonitor\Models\Presenters\CheckPresenter;
18
use Symfony\Component\Process\Process;
19
20
class Check extends Model
21
{
22
    use CheckPresenter,
23
        HasCustomProperties,
24
        ThrottlesFailingNotifications,
25
        HasProcess,
26
        HandlesCheckResult;
27
28
    public $guarded = [];
29
30
    public $casts = [
31
        'custom_properties' => 'array',
32
        'last_run_output' => 'array',
33
    ];
34
35
    public $dates = [
36
        'last_ran_at', 'next_check_at', 'started_throttling_failing_notifications_at',
37
    ];
38
39
    public function host(): BelongsTo
40
    {
41
        return $this->belongsTo(config('server-monitor.host_model', Host::class));
42
    }
43
44
    public function scopeHealthy($query)
45
    {
46
        return $query->where('status', CheckStatus::SUCCESS);
47
    }
48
49
    public function scopeUnhealthy($query)
50
    {
51
        return $query->where('status', '!=', CheckStatus::SUCCESS);
52
    }
53
54
    public function scopeEnabled(Builder $query)
55
    {
56
        $query->where('enabled', 1);
57
    }
58
59
    public function shouldRun(): bool
60
    {
61
        if (! $this->enabled) {
62
            return false;
63
        }
64
65
        if (is_null($this->last_ran_at)) {
66
            return true;
67
        }
68
69
        return ! $this->last_ran_at
70
            ->addMinutes($this->next_run_in_minutes)
71
            ->isFuture();
72
    }
73
74
    public function getDefinition(): CheckDefinition
75
    {
76
        if (! $definitionClass = config("server-monitor.checks.{$this->type}")) {
77
            throw InvalidCheckDefinition::unknownCheckType($this);
78
        }
79
80
        if (! class_exists($definitionClass)) {
81
            throw InvalidCheckDefinition::definitionClassDoesNotExist($this, $definitionClass);
82
        }
83
84
        return app($definitionClass)->setCheck($this);
85
    }
86
87
    public function handleFinishedProcess()
88
    {
89
        $originalStatus = $this->status;
90
91
        $this->getDefinition()->determineResult($this->getProcess());
92
93
        $this->scheduleNextRun();
94
95
        if ($this->shouldFireRestoredEvent($originalStatus, $this->status)) {
96
            event(new CheckRestored($this));
97
        }
98
99
        return $this;
100
    }
101
102
    protected function shouldFireRestoredEvent(?string $originalStatus, ?string $newStatus)
103
    {
104
        if (! in_array($originalStatus, [CheckStatus::FAILED, CheckStatus::WARNING])) {
105
            return false;
106
        }
107
108
        return $newStatus === CheckStatus::SUCCESS;
109
    }
110
111
    protected function scheduleNextRun()
112
    {
113
        $this->last_ran_at = Carbon::now();
114
115
        $this->next_run_in_minutes = $this->getDefinition()->performNextRunInMinutes();
116
        $this->save();
117
118
        return $this;
119
    }
120
121
    public function hasStatus(string $status): bool
122
    {
123
        return $this->status === $status;
124
    }
125
126
    public function storeProcessOutput(Process $process)
127
    {
128
        $this->last_run_output = [
129
            'output' => $process->getOutput(),
130
            'error_output' => $process->getErrorOutput(),
131
            'exit_code' => $process->getExitCode(),
132
            'exit_code_text' => $process->getExitCodeText(),
133
        ];
134
135
        $this->save();
136
    }
137
}
138