Files
KanbanGit/app/Actions/Notifications/BuildTicketAssigneeChangedPayload.php
T

64 lines
1.9 KiB
PHP

<?php
namespace App\Actions\Notifications;
use App\Enums\NotificationEventType;
use App\Models\Project;
use App\Models\Ticket;
use App\Models\TicketStatus;
use App\Models\User;
use Carbon\CarbonImmutable;
class BuildTicketAssigneeChangedPayload
{
public function handle(
Project $project,
Ticket $ticket,
?User $actor,
?int $fromAssigneeId,
?string $fromAssigneeName,
?int $toAssigneeId,
?string $toAssigneeName,
CarbonImmutable $occurredAt,
): TicketStatusChangedPayload {
$ticket->loadMissing('ticketStatus.boardColumn');
return new TicketStatusChangedPayload(
eventType: NotificationEventType::TicketAssigneeChanged,
projectId: $project->id,
projectKey: $project->key,
projectName: $project->name,
ticketId: $ticket->id,
ticketKey: $ticket->ticket_key,
ticketTitle: $ticket->title,
ticketUrl: route('projects.tickets.show', [$project, $ticket]),
actorId: $actor?->id,
actorName: $actor?->name,
fromStatusId: null,
fromStatusLabel: null,
toStatusId: $ticket->ticket_status_id,
toStatusLabel: $this->statusLabel($ticket->ticketStatus),
occurredAt: $occurredAt,
fromAssigneeId: $fromAssigneeId,
fromAssigneeName: $fromAssigneeName,
toAssigneeId: $toAssigneeId,
toAssigneeName: $toAssigneeName,
);
}
private function statusLabel(?TicketStatus $ticketStatus): string
{
if ($ticketStatus === null) {
return 'Unknown status';
}
$ticketStatus->loadMissing('boardColumn');
if ($ticketStatus->boardColumn === null || $ticketStatus->boardColumn->name === $ticketStatus->name) {
return $ticketStatus->name;
}
return "{$ticketStatus->boardColumn->name} / {$ticketStatus->name}";
}
}