Add notification action foundation
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Notifications;
|
||||
|
||||
use App\Models\NotificationAction;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class UpdateProjectNotificationAction
|
||||
{
|
||||
/**
|
||||
* @param array{
|
||||
* channel?: mixed,
|
||||
* channel_configuration?: array<string, mixed>|null,
|
||||
* delivery_url?: string|null,
|
||||
* enabled?: bool,
|
||||
* event_type?: 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'),
|
||||
);
|
||||
}
|
||||
|
||||
$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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user