-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathComponent.php
More file actions
163 lines (146 loc) · 4.47 KB
/
Component.php
File metadata and controls
163 lines (146 loc) · 4.47 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace Cachet\Models;
use Cachet\Database\Factories\ComponentFactory;
use Cachet\Enums\ComponentStatusEnum;
use Cachet\Enums\ResourceOrderColumnEnum;
use Cachet\Events\Components\ComponentCreated;
use Cachet\Events\Components\ComponentDeleted;
use Cachet\Events\Components\ComponentUpdated;
use Carbon\Carbon;
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\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* @property int $id
* @property string $name
* @property ?string $description
* @property ?string $link
* @property ?ComponentStatusEnum $status
* @property ComponentStatusEnum $latest_status
* @property ?int $order
* @property ?int $component_group_id
* @property ?Carbon $created_at
* @property ?Carbon $updated_at
* @property ?Carbon $deleted_at
* @property bool $enabled
* @property array<string, mixed> $meta
* @property ?ComponentGroup $componentGroup
*
* @method static Builder<static>|static disabled()
* @method static Builder<static>|static enabled()
* @method static Builder<static>|static outage()
* @method static Builder<static>|static status(ComponentStatusEnum $status)
* @method static ComponentFactory factory($count = null, $state = [])
*/
class Component extends Model
{
/** @use HasFactory<ComponentFactory> */
use HasFactory;
use SoftDeletes;
/** @var array<string, string> */
protected $casts = [
'status' => ComponentStatusEnum::class,
'order' => 'int',
'enabled' => 'bool',
'meta' => 'json',
];
/** @var list<string> */
protected $fillable = [
'name',
'description',
'link',
'order',
'status',
'component_group_id',
'enabled',
'meta',
];
protected $dispatchesEvents = [
'created' => ComponentCreated::class,
'deleted' => ComponentDeleted::class,
'updated' => ComponentUpdated::class,
];
/**
* Get the group the component belongs to.
*/
public function group(): BelongsTo
{
return $this->belongsTo(ComponentGroup::class, 'component_group_id', 'id');
}
/**
* Get the incidents for the component.
*
* @return BelongsToMany<Incident, $this, IncidentComponent>
*/
public function incidents(): BelongsToMany
{
return $this->belongsToMany(Incident::class, 'incident_components')
->using(IncidentComponent::class)
->withTimestamps()
->withPivot('component_status');
}
/**
* Get the subscribers for this component.
*/
public function subscribers(): BelongsToMany
{
return $this->belongsToMany(Subscriber::class, 'subscriptions');
}
/**
* Scope to disabled components only.
*/
public function scopeDisabled(Builder $query): void
{
$query->where('enabled', false);
}
/**
* Scope to enabled components only.
*/
public function scopeEnabled(Builder $query): void
{
$query->where('enabled', true);
}
/**
* Scope to a specific status.
*/
public function scopeStatus(Builder $query, ComponentStatusEnum $status): void
{
$query->where('status', $status);
}
public function scopeOutage(Builder $query): void
{
$query->whereIn('status', ComponentStatusEnum::outage());
}
/**
* Get the latest status for the component.
*/
public function latestStatus(): Attribute
{
return Attribute::get(fn () => $this->incidents()->unresolved()->latest()->first()?->pivot->component_status ?? $this->status);
}
/**
* Determine how to order the component.
*/
public function orderableBy(ComponentGroup $group): mixed
{
return match ($group->order_column) {
ResourceOrderColumnEnum::Id => $this->id,
ResourceOrderColumnEnum::LastUpdated => $this->updated_at,
ResourceOrderColumnEnum::Name => $this->name,
ResourceOrderColumnEnum::Manual => $this->order,
default => $this->status->value,
};
}
/**
* Create a new factory instance for the model.
*/
protected static function newFactory(): Factory
{
return ComponentFactory::new();
}
}