-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathSchedule.php
137 lines (120 loc) · 3.55 KB
/
Schedule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace Cachet\Models;
use Cachet\Database\Factories\ScheduleFactory;
use Cachet\Enums\ScheduleStatusEnum;
use Cachet\Filament\Resources\ScheduleResource;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
class Schedule extends Model
{
use HasFactory, SoftDeletes;
protected $casts = [
'scheduled_at' => 'datetime',
'completed_at' => 'datetime',
];
protected $fillable = [
'name',
'message',
'scheduled_at',
'completed_at',
];
/**
* Get the status of the schedule.
*/
public function status(): Attribute
{
return Attribute::make(
get: function () {
$now = Carbon::now();
return match (true) {
$this->scheduled_at->gte($now) => ScheduleStatusEnum::upcoming,
$this->completed_at === null => ScheduleStatusEnum::in_progress,
default => ScheduleStatusEnum::complete,
};
}
);
}
/**
* Get the components affected by this schedule.
*/
public function components(): BelongsToMany
{
return $this->belongsToMany(
Component::class,
'schedule_components',
);
}
/**
* Get the updates for this schedule.
*/
public function updates(): MorphMany
{
return $this->morphMany(Update::class, 'updateable')->chaperone();
}
/**
* Render the Markdown message.
*/
public function formattedMessage(): string
{
return Str::of($this->message)->markdown();
}
/**
* Scope schedules that are incomplete.
*/
public function scopeIncomplete(Builder $query): Builder
{
return $query->whereDate('scheduled_at', '>=', Carbon::now())
->whereNull('completed_at');
}
/**
* Scope schedules that are in progress.
*/
public function scopeInProgress(Builder $query): Builder
{
return $query->whereDate('scheduled_at', '<=', Carbon::now())
->where(function (Builder $query) {
$query->whereDate('completed_at', '>=', Carbon::now())
->orWhereNull('completed_at');
});
}
/**
* Scopes schedules to those in the future.
*/
public function scopeInTheFuture(Builder $query): Builder
{
return $query->whereDate('scheduled_at', '>=', Carbon::now());
}
/**
* Scopes schedules to those scheduled in the past.
*/
public function scopeInThePast(Builder $query): Builder
{
return $query->where('completed_at', '<=', Carbon::now());
}
/**
* Get the URL to the schedule page within the dashboard.
*/
public function filamentDashboardEditUrl(): string
{
return ScheduleResource::getUrl(name: 'edit', parameters: ['record' => $this->id]);
}
public function timestamp(): Attribute
{
return Attribute::get(fn () => $this->completed_at ?: $this->scheduled_at);
}
/**
* Create a new factory instance for the model.
*/
protected static function newFactory(): Factory
{
return ScheduleFactory::new();
}
}