Show template inheritance overrides
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Projects;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\ProjectTemplate;
|
||||
|
||||
class ResolveProjectTemplateState
|
||||
{
|
||||
public function __construct(
|
||||
private readonly DuplicateProjectConfiguration $duplicateProjectConfiguration,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param list<string>|null $areas
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
public function handle(Project $project, ?array $areas = null): ?array
|
||||
{
|
||||
$project->loadMissing('projectTemplate');
|
||||
|
||||
/** @var ProjectTemplate|null $projectTemplate */
|
||||
$projectTemplate = $project->projectTemplate;
|
||||
|
||||
if ($projectTemplate === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$selectedAreas = $areas === null
|
||||
? DuplicateProjectConfiguration::copyableAreas()
|
||||
: $this->duplicateProjectConfiguration->normalizeCopyAreas($areas);
|
||||
|
||||
$templateConfiguration = collect($projectTemplate->configuration_snapshot ?? [])
|
||||
->only($selectedAreas)
|
||||
->all();
|
||||
$currentConfiguration = $this->duplicateProjectConfiguration->export($project, $selectedAreas);
|
||||
|
||||
$overriddenAreas = [];
|
||||
$inheritedAreas = [];
|
||||
|
||||
foreach ($selectedAreas as $area) {
|
||||
$label = $this->labelForArea($area);
|
||||
$hasTemplateValue = array_key_exists($area, $templateConfiguration);
|
||||
$hasCurrentValue = array_key_exists($area, $currentConfiguration);
|
||||
|
||||
if (! $hasTemplateValue && ! $hasCurrentValue) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($hasTemplateValue && $hasCurrentValue && $currentConfiguration[$area] === $templateConfiguration[$area]) {
|
||||
$inheritedAreas[] = [
|
||||
'label' => $label,
|
||||
'value' => $area,
|
||||
];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$overriddenAreas[] = [
|
||||
'label' => $label,
|
||||
'value' => $area,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'effectiveConfiguration' => array_replace($templateConfiguration, array_intersect_key($currentConfiguration, array_flip(array_column($overriddenAreas, 'value')))),
|
||||
'inheritedAreas' => $inheritedAreas,
|
||||
'inheritsFromTemplate' => true,
|
||||
'overriddenAreas' => $overriddenAreas,
|
||||
'templateId' => $projectTemplate->id,
|
||||
'templateName' => $projectTemplate->name,
|
||||
'templateSlug' => $projectTemplate->slug,
|
||||
];
|
||||
}
|
||||
|
||||
private function labelForArea(string $area): string
|
||||
{
|
||||
foreach (DuplicateProjectConfiguration::copyAreaOptions() as $option) {
|
||||
if ($option['value'] === $area) {
|
||||
return $option['label'];
|
||||
}
|
||||
}
|
||||
|
||||
return $area;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user