-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathUpdateComponent.php
More file actions
46 lines (39 loc) · 1.39 KB
/
UpdateComponent.php
File metadata and controls
46 lines (39 loc) · 1.39 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
<?php
namespace Cachet\Actions\Component;
use Cachet\Data\Requests\Component\UpdateComponentRequestData;
use Cachet\Models\Component;
use Cachet\Verbs\Events\Components\ComponentStatusChanged;
use Cachet\Verbs\Events\Components\ComponentUpdated;
class UpdateComponent
{
/**
* Handle the action.
*/
public function handle(Component $component, UpdateComponentRequestData $data): Component
{
$oldStatus = $component->status;
$hasStatusChange = $data->status !== null && $data->status !== $oldStatus;
// Fire status change event if status is changing
if ($hasStatusChange) {
ComponentStatusChanged::commit(
component_id: $component->id,
old_status: $oldStatus,
new_status: $data->status,
);
}
// Fire general update event for other changes
ComponentUpdated::commit(
component_id: $component->id,
name: $data->name,
status: $hasStatusChange ? null : $data->status, // Skip status if already handled
description: $data->description,
link: $data->link,
order: $data->order,
component_group_id: $data->componentGroupId,
enabled: $data->enabled,
);
// Refresh the original model with updated data
$component->refresh();
return $component;
}
}