85 lines
2.4 KiB
PHP
85 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Cms;
|
|
|
|
use App\Domain\Cms\Blocks\BlockRegistry;
|
|
use App\Jobs\PublishPublicMedia;
|
|
use App\Models\User;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class PageService
|
|
{
|
|
public function __construct(private readonly BlockRegistry $blocks) {}
|
|
|
|
/**
|
|
* @param list<array<string, mixed>> $blocks
|
|
*/
|
|
public function saveRevision(Page $page, array $blocks, ?string $seoTitle, ?string $seoDescription, ?User $author): PageRevision
|
|
{
|
|
$normalized = $this->blocks->normalize($blocks);
|
|
|
|
$revision = $page->revisions()->create([
|
|
'blocks' => $normalized,
|
|
'seo_title' => $seoTitle !== null && $seoTitle !== '' ? $seoTitle : null,
|
|
'seo_description' => $seoDescription !== null && $seoDescription !== '' ? $seoDescription : null,
|
|
'created_by' => $author?->id,
|
|
]);
|
|
|
|
foreach ($normalized as $block) {
|
|
if (($block['type'] ?? null) === 'image_text' && filled($block['image'] ?? null)) {
|
|
PublishPublicMedia::dispatch($block['image']);
|
|
}
|
|
}
|
|
|
|
return $revision;
|
|
}
|
|
|
|
public function publish(Page $page, PageRevision $revision): void
|
|
{
|
|
if ($revision->page_id !== $page->id) {
|
|
throw ValidationException::withMessages([
|
|
'revision' => 'Die Revision gehört zu einer anderen Seite.',
|
|
]);
|
|
}
|
|
|
|
$page->published_revision_id = $revision->id;
|
|
$page->save();
|
|
}
|
|
|
|
public function restore(Page $page, PageRevision $source, ?User $author): PageRevision
|
|
{
|
|
if ($source->page_id !== $page->id) {
|
|
throw ValidationException::withMessages([
|
|
'revision' => 'Die Revision gehört zu einer anderen Seite.',
|
|
]);
|
|
}
|
|
|
|
return $this->saveRevision(
|
|
$page,
|
|
$source->blocks,
|
|
$source->seo_title,
|
|
$source->seo_description,
|
|
$author,
|
|
);
|
|
}
|
|
|
|
public function normalizePath(string $path): string
|
|
{
|
|
$path = '/'.trim($path, '/');
|
|
|
|
if ($path === '/') {
|
|
return '/';
|
|
}
|
|
|
|
$path = mb_strtolower($path);
|
|
|
|
if (preg_match('#^/[a-z0-9]+(?:-[a-z0-9]+)*$#', $path) !== 1) {
|
|
throw ValidationException::withMessages([
|
|
'path' => 'Der Pfad darf nur Kleinbuchstaben, Ziffern und Bindestriche enthalten.',
|
|
]);
|
|
}
|
|
|
|
return $path;
|
|
}
|
|
}
|