Files
KanbanGit/app/Actions/Notifications/UpdateProjectNotificationAction.php
2026-04-24 20:02:22 +00:00

99 lines
2.9 KiB
PHP

<?php
namespace App\Actions\Notifications;
use App\Models\NotificationAction;
use App\Models\Project;
use Illuminate\Support\Arr;
class UpdateProjectNotificationAction
{
public function __construct(
private readonly NormalizeNotificationFilters $normalizeNotificationFilters,
) {}
/**
* @param array{
* channel?: mixed,
* channel_configuration?: array<string, mixed>|null,
* delivery_url?: string|null,
* enabled?: bool,
* event_type?: mixed,
* filters?: mixed,
* message_template?: string|null,
* name?: string
* } $validated
*/
public function handle(
Project $project,
NotificationAction $notificationAction,
array $validated,
): NotificationAction {
$notificationAction = $this->resolveProjectNotificationAction($project, $notificationAction);
$attributes = [];
if (array_key_exists('name', $validated)) {
$attributes['name'] = trim((string) Arr::get($validated, 'name'));
}
if (array_key_exists('enabled', $validated)) {
$attributes['enabled'] = (bool) Arr::get($validated, 'enabled');
}
if (array_key_exists('event_type', $validated)) {
$attributes['event_type'] = Arr::get($validated, 'event_type');
}
if (array_key_exists('channel', $validated)) {
$attributes['channel'] = Arr::get($validated, 'channel');
}
if (array_key_exists('channel_configuration', $validated)) {
$attributes['channel_configuration'] = Arr::get($validated, 'channel_configuration') ?? [];
}
if (array_key_exists('message_template', $validated)) {
$attributes['message_template'] = $this->normalizeMessageTemplate(
Arr::get($validated, 'message_template'),
);
}
if (array_key_exists('filters', $validated)) {
$attributes['filters'] = $this->normalizeNotificationFilters->handle(
Arr::get($validated, 'filters'),
);
}
$deliveryUrl = Arr::get($validated, 'delivery_url');
if (is_string($deliveryUrl) && trim($deliveryUrl) !== '') {
$attributes['delivery_url'] = trim($deliveryUrl);
}
$notificationAction->update($attributes);
return $notificationAction->fresh();
}
private function normalizeMessageTemplate(mixed $messageTemplate): ?string
{
if (! is_string($messageTemplate)) {
return null;
}
$messageTemplate = trim($messageTemplate);
return $messageTemplate === '' ? null : $messageTemplate;
}
private function resolveProjectNotificationAction(
Project $project,
NotificationAction $notificationAction,
): NotificationAction {
return $project->notificationActions()
->whereKey($notificationAction->id)
->firstOrFail();
}
}