Files
KanbanGit/app/Actions/Tickets/CreateProjectTicket.php
T
2026-04-13 00:10:09 +02:00

65 lines
2.0 KiB
PHP

<?php
namespace App\Actions\Tickets;
use App\Models\Project;
use App\Models\Ticket;
use App\Models\User;
use Arr;
use Illuminate\Support\Facades\DB;
class CreateProjectTicket
{
public function __construct(
private readonly CreateTicketWorklogEntry $createTicketWorklogEntry,
) {}
/**
* @param array{
* acceptance_criteria: string|null,
* description: string|null,
* ticket_priority_id: int,
* ticket_type_id: int,
* title: string
* } $attributes
*/
public function handle(Project $project, array $attributes, ?User $actor = null): Ticket
{
return DB::transaction(function () use ($actor, $attributes, $project): Ticket {
$nextSequenceNumber = (int) Ticket::query()
->where('project_id', $project->id)
->max('sequence_number') + 1;
$ticket = Ticket::query()->create([
'acceptance_criteria' => $attributes['acceptance_criteria'],
'board_column_id' => null,
'description' => $attributes['description'],
'project_id' => $project->id,
'sequence_number' => $nextSequenceNumber,
'ticket_key' => "{$project->key}-{$nextSequenceNumber}",
'ticket_priority_id' => $attributes['ticket_priority_id'],
'ticket_type_id' => $attributes['ticket_type_id'],
'title' => $attributes['title'],
'story_points' => Arr::get($attributes, 'story_points'),
]);
if ($actor !== null) {
$this->createTicketWorklogEntry->handle(
$ticket,
$actor,
'created',
'Ticket was created in Backlog.',
[
'from' => null,
'to' => [
'label' => 'Backlog',
],
],
);
}
return $ticket;
});
}
}