70 lines
2.5 KiB
PHP
70 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Projects;
|
|
|
|
use App\Models\Project;
|
|
use App\Models\ProjectTemplate;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class UpdateProjectTemplateLink
|
|
{
|
|
public function __construct(
|
|
private readonly DuplicateProjectConfiguration $duplicateProjectConfiguration,
|
|
private readonly ProjectTemplateOverrideManager $projectTemplateOverrideManager,
|
|
) {}
|
|
|
|
public function switch(Project $project, ProjectTemplate $template): void
|
|
{
|
|
DB::transaction(function () use ($project, $template): void {
|
|
$project = Project::query()->whereKey($project->id)->lockForUpdate()->firstOrFail();
|
|
$configuration = $this->duplicateProjectConfiguration->normalizeConfiguration(
|
|
$template->configuration_snapshot ?? [],
|
|
);
|
|
$localAreas = array_values(array_diff(
|
|
DuplicateProjectConfiguration::copyableAreas(),
|
|
array_keys($configuration),
|
|
));
|
|
|
|
$project->forceFill([
|
|
'project_template_id' => $template->id,
|
|
'template_overrides' => $localAreas,
|
|
])->saveQuietly();
|
|
|
|
$blockedAreas = $this->duplicateProjectConfiguration->apply($project, $configuration);
|
|
$this->projectTemplateOverrideManager->clear(
|
|
$project->fresh(),
|
|
array_values(array_diff(array_keys($configuration), $blockedAreas)),
|
|
);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param list<string> $areas
|
|
* @param array<int|string, int|string> $statusReplacements
|
|
*/
|
|
public function reset(Project $project, array $areas, array $statusReplacements = []): void
|
|
{
|
|
$template = $project->projectTemplate;
|
|
|
|
if ($template === null) {
|
|
return;
|
|
}
|
|
|
|
DB::transaction(function () use ($project, $template, $areas, $statusReplacements): void {
|
|
$project = Project::query()->whereKey($project->id)->lockForUpdate()->firstOrFail();
|
|
$configuration = collect($this->duplicateProjectConfiguration->normalizeConfiguration(
|
|
$template->configuration_snapshot ?? [],
|
|
))->only($areas)->all();
|
|
$blockedAreas = $this->duplicateProjectConfiguration->apply(
|
|
$project,
|
|
$configuration,
|
|
$statusReplacements,
|
|
);
|
|
$this->projectTemplateOverrideManager->clear(
|
|
$project->fresh(),
|
|
array_values(array_diff(array_keys($configuration), $blockedAreas)),
|
|
);
|
|
});
|
|
}
|
|
}
|