Eine Fassung bleibt nummeriert und kann deaktiviert werden. Beschreibung und fachliche Säulen gehören zum Medium und legen keine neue Fassung an. Co-authored-by: Cursor <cursoragent@cursor.com>
64 lines
1.6 KiB
PHP
64 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Catalog;
|
|
|
|
class PillarService
|
|
{
|
|
public function create(string $name): Pillar
|
|
{
|
|
$name = $this->name($name);
|
|
|
|
if (Pillar::query()->where('name', $name)->exists()) {
|
|
throw new DomainRuleException('Diese fachliche Säule gibt es schon.');
|
|
}
|
|
|
|
return Pillar::query()->create(['name' => $name]);
|
|
}
|
|
|
|
public function rename(Pillar $pillar, string $name): void
|
|
{
|
|
$name = $this->name($name);
|
|
|
|
if (Pillar::query()->where('name', $name)->whereKeyNot($pillar->id)->exists()) {
|
|
throw new DomainRuleException('Diese fachliche Säule gibt es schon.');
|
|
}
|
|
|
|
$pillar->name = $name;
|
|
$pillar->save();
|
|
}
|
|
|
|
public function delete(Pillar $pillar): void
|
|
{
|
|
if ($pillar->elements()->exists()) {
|
|
throw new DomainRuleException('Eine Säule mit zugeordneten Medien wird nicht gelöscht.');
|
|
}
|
|
|
|
$pillar->delete();
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $pillarIds
|
|
*/
|
|
public function assign(ContentElement $element, array $pillarIds): void
|
|
{
|
|
$ids = array_values(array_unique(array_map(intval(...), $pillarIds)));
|
|
|
|
if (Pillar::query()->whereIn('id', $ids)->count() !== count($ids)) {
|
|
throw new DomainRuleException('Eine fachliche Säule fehlt.');
|
|
}
|
|
|
|
$element->pillars()->sync($ids);
|
|
}
|
|
|
|
private function name(string $name): string
|
|
{
|
|
$name = trim($name);
|
|
|
|
if ($name === '') {
|
|
throw new DomainRuleException('Der Name der fachlichen Säule fehlt.');
|
|
}
|
|
|
|
return $name;
|
|
}
|
|
}
|