-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEvent.php
More file actions
55 lines (47 loc) · 1.61 KB
/
Copy pathEvent.php
File metadata and controls
55 lines (47 loc) · 1.61 KB
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
<?php
namespace App\Models;
use App\Enum\EventStateEnum;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Event extends Model
{
use HasFactory;
public $timestamps = false;
protected $guarded = [];
protected $appends = ['state'];
protected function casts()
{
return [
'starts_at' => 'date',
'ends_at' => 'date',
'preorder_starts_at' => 'datetime',
'preorder_ends_at' => 'datetime',
'order_ends_at' => 'datetime',
'mass_printed_at' => 'datetime',
];
}
public static function getActiveEvent(): Event|null
{
return self::where('ends_at', '>', now())->orderBy('starts_at')->first();
}
public function state(): Attribute
{
return new Attribute(get: function ($value) {
// If Preorder is in the future, then countdown
if ($this->preorder_starts_at > now()) {
return EventStateEnum::COUNTDOWN;
}
// If Date is between Preorder dates, then preorder
if ($this->preorder_starts_at < now() && $this->preorder_ends_at > now()) {
return EventStateEnum::PREORDER;
}
// If Date is between Preorder and Order dates, then late
if ($this->preorder_ends_at < now() && $this->order_ends_at > now()) {
return EventStateEnum::LATE;
}
// If Date is after Order date, then closed
return EventStateEnum::CLOSED;
});
}
}