|null} $attributes */ public function handle(array $attributes, User $actor): Project { return DB::transaction(function () use ($attributes, $actor): Project { $project = Project::query()->create(Arr::only($attributes, ['key', 'name'])); $baseTemplate = $this->createProjectBaseTemplate->handle($project); $project->forceFill([ 'project_template_id' => $baseTemplate->id, ])->save(); $project = $project->fresh() ?? $project; $this->initializeProjectTicketMetadata->handle($project); $this->permissionService->grant($actor, Permission::ProjectAdmin, $project); $creationMode = $attributes['creation_mode'] ?? 'scratch'; if ($creationMode === 'duplicate' && isset($attributes['source_project_id'])) { $sourceProject = Project::query()->findOrFail((int) $attributes['source_project_id']); $this->duplicateProjectConfiguration->handle( $sourceProject, $project, array_values($attributes['copy_areas'] ?? []), ); } if ($creationMode === 'template' && isset($attributes['source_template_id'])) { $template = ProjectTemplate::query()->findOrFail((int) $attributes['source_template_id']); $this->duplicateProjectConfiguration->apply( $project, $template->configuration_snapshot ?? [], ); } $baseTemplate->forceFill([ 'configuration_snapshot' => $this->duplicateProjectConfiguration->export( $project->fresh() ?? $project, DuplicateProjectConfiguration::copyableAreas(), ), ])->save(); return $project->fresh() ?? $project; }); } }