-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathComponentGroup.php
More file actions
95 lines (83 loc) · 2.5 KB
/
ComponentGroup.php
File metadata and controls
95 lines (83 loc) · 2.5 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
<?php
namespace Cachet\Models;
use Cachet\Concerns\HasVisibility;
use Cachet\Database\Factories\ComponentGroupFactory;
use Cachet\Enums\ComponentGroupVisibilityEnum;
use Cachet\Enums\ResourceVisibilityEnum;
use Cachet\Models\Concerns\ManagesConnections;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
/**
* @property int $id
* @property string $name
* @property ?Carbon $created_at
* @property ?Carbon $updated_at
* @property int $order
* @property ComponentGroupVisibilityEnum $collapsed
* @property ResourceVisibilityEnum $visible
* @property Collection<int, Component> $components
*
* @method static ComponentGroupFactory factory($count = null, $state = [])
*/
class ComponentGroup extends Model
{
/** @use HasFactory<ComponentGroupFactory> */
use HasFactory;
use ManagesConnections;
use HasVisibility;
/** @var array<string, string> */
protected $casts = [
'order' => 'int',
'collapsed' => ComponentGroupVisibilityEnum::class,
'visible' => ResourceVisibilityEnum::class,
];
/** @var list<string> */
protected $fillable = [
'name',
'order',
'collapsed',
'visible',
];
/**
* Get the components in the group.
*
* @return HasMany<Component, $this>
*/
public function components(): HasMany
{
return $this->hasMany(Component::class);
}
public function isCollapsible(): bool
{
return match ($this->collapsed) {
ComponentGroupVisibilityEnum::collapsed,
ComponentGroupVisibilityEnum::collapsed_unless_incident => true,
default => false,
};
}
public function isExpanded(): bool
{
return match ($this->collapsed) {
ComponentGroupVisibilityEnum::collapsed => false,
ComponentGroupVisibilityEnum::collapsed_unless_incident => $this->hasActiveIncident(),
ComponentGroupVisibilityEnum::expanded => true,
};
}
public function hasActiveIncident(): bool
{
return Incident::query()
->whereIn('component_id', $this->components->pluck('id'))
->exists();
}
/**
* Create a new factory instance for the model.
*/
protected static function newFactory(): Factory
{
return ComponentGroupFactory::new();
}
}