84 lines
3.0 KiB
PHP
84 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Catalog;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ArticleService
|
|
{
|
|
public function __construct(private readonly ContentResolver $resolver) {}
|
|
|
|
public function create(string $code, ?string $productLine = null): LicenseArticle
|
|
{
|
|
$code = trim($code);
|
|
|
|
if ($code === '') {
|
|
throw new DomainRuleException('Die Kennung des Lizenzartikels fehlt.');
|
|
}
|
|
|
|
return LicenseArticle::query()->create([
|
|
'code' => $code,
|
|
'product_line' => $productLine !== null && trim($productLine) !== '' ? trim($productLine) : null,
|
|
]);
|
|
}
|
|
|
|
public function publishRelease(
|
|
LicenseArticle $article,
|
|
ContentElementVersion|CompositionVersion $root,
|
|
string $classification,
|
|
?string $changeNote = null,
|
|
): ArticleRelease {
|
|
$classification = trim($classification);
|
|
|
|
if ($classification === '') {
|
|
throw new DomainRuleException('Ein neuer Stand braucht eine Einordnung, warum er zum selben Artikel gehört.');
|
|
}
|
|
|
|
if ($root->status !== ContentVersionStatus::Bereit) {
|
|
throw new DomainRuleException('Ein Artikel-Stand kann nur eine bereitgestellte Fassung festschreiben.');
|
|
}
|
|
|
|
$rows = $this->resolver->expand($root);
|
|
|
|
if ($rows === []) {
|
|
throw new DomainRuleException('Ein Artikel-Stand braucht mindestens eine Inhaltsfassung.');
|
|
}
|
|
|
|
return DB::transaction(function () use ($article, $root, $classification, $changeNote, $rows): ArticleRelease {
|
|
$release = $article->releases()->create([
|
|
'number' => ((int) $article->releases()->max('number')) + 1,
|
|
'classification_note' => $classification,
|
|
'status' => ReleaseStatus::Entwurf,
|
|
'change_note' => $changeNote,
|
|
'root_element_version_id' => $root instanceof ContentElementVersion ? $root->id : null,
|
|
'root_composition_version_id' => $root instanceof CompositionVersion ? $root->id : null,
|
|
]);
|
|
|
|
foreach ($rows as $index => $row) {
|
|
$release->contents()->create([
|
|
'position' => $index + 1,
|
|
'content_element_version_id' => $row['version_id'],
|
|
'role' => $row['role'] !== '' ? $row['role'] : $row['path'],
|
|
'path' => $row['path'],
|
|
]);
|
|
}
|
|
|
|
ArticleRelease::query()
|
|
->where('license_article_id', $article->id)
|
|
->where('status', ReleaseStatus::Veroeffentlicht)
|
|
->whereKeyNot($release->id)
|
|
->each(function (ArticleRelease $previous): void {
|
|
$previous->status = ReleaseStatus::AbgeloestVerfuegbar;
|
|
$previous->save();
|
|
});
|
|
|
|
$release->status = ReleaseStatus::Veroeffentlicht;
|
|
$release->save();
|
|
$article->current_release_id = $release->id;
|
|
$article->save();
|
|
|
|
return $release->load('contents');
|
|
});
|
|
}
|
|
}
|