57 lines
1.6 KiB
PHP
57 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Projects;
|
|
|
|
use App\Models\Project;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ProjectTemplateOverrideManager
|
|
{
|
|
private static int $suppressionDepth = 0;
|
|
|
|
public function mark(Project $project, string $area): void
|
|
{
|
|
if (self::$suppressionDepth > 0) {
|
|
return;
|
|
}
|
|
|
|
DB::transaction(function () use ($project, $area): void {
|
|
$project = Project::query()->whereKey($project->id)->lockForUpdate()->firstOrFail();
|
|
$project->load('projectTemplate');
|
|
if ($project->projectTemplate === null || ! $project->projectTemplate->is_published) {
|
|
return;
|
|
}
|
|
$project->forceFill(['template_overrides' => array_values(array_unique([
|
|
...($project->template_overrides ?? []), $area,
|
|
]))])->saveQuietly();
|
|
}, 3);
|
|
}
|
|
|
|
/**
|
|
* @param list<string> $areas
|
|
*/
|
|
public function clear(Project $project, array $areas): void
|
|
{
|
|
DB::transaction(function () use ($project, $areas): void {
|
|
$project = Project::query()->whereKey($project->id)->lockForUpdate()->firstOrFail();
|
|
$project->forceFill(['template_overrides' => array_values(array_diff($project->template_overrides ?? [], $areas))])->saveQuietly();
|
|
}, 3);
|
|
}
|
|
|
|
public function withoutRecording(callable $callback): mixed
|
|
{
|
|
self::$suppressionDepth++;
|
|
|
|
try {
|
|
return $callback();
|
|
} finally {
|
|
self::$suppressionDepth--;
|
|
}
|
|
}
|
|
|
|
public function isRecording(): bool
|
|
{
|
|
return self::$suppressionDepth === 0;
|
|
}
|
|
}
|