36 lines
1.1 KiB
PHP
36 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Projects;
|
|
|
|
use App\Models\Project;
|
|
use App\Models\ProjectTemplate;
|
|
|
|
class CreateProjectBaseTemplate
|
|
{
|
|
public function __construct(
|
|
private readonly DuplicateProjectConfiguration $duplicateProjectConfiguration,
|
|
) {}
|
|
|
|
public function handle(Project $project): ProjectTemplate
|
|
{
|
|
$copyAreas = DuplicateProjectConfiguration::copyableAreas();
|
|
|
|
$slug = strtolower($project->key).'-base-template';
|
|
$suffix = 1;
|
|
$candidate = $slug;
|
|
while (ProjectTemplate::query()->where('slug', $candidate)->exists()) {
|
|
$candidate = $slug.'-'.++$suffix;
|
|
}
|
|
|
|
return ProjectTemplate::query()->create([
|
|
'project_id' => $project->id,
|
|
'source_project_id' => $project->id,
|
|
'name' => "{$project->name} base template",
|
|
'slug' => $candidate,
|
|
'is_published' => false,
|
|
'copy_areas' => $copyAreas,
|
|
'configuration_snapshot' => $this->duplicateProjectConfiguration->export($project, $copyAreas),
|
|
]);
|
|
}
|
|
}
|