Files
laraundfelix/app/Domain/Catalog/ArticleService.php
T
LarsandCursor fef0ea2ac2
Deploy Test / test (push) Successful in 12s
Deploy Test / deploy (push) Successful in 32s
Halte Auswirkung und Rückzug beim Veröffentlichen eines Artikel-Stands fest.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-15 09:07:14 +01:00

192 lines
7.0 KiB
PHP

<?php
namespace App\Domain\Catalog;
use App\Domain\Entitlement\Acquisition;
use App\Domain\Entitlement\GrantStatus;
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.');
}
$this->checkedRows($root);
return DB::transaction(function () use ($article, $root, $classification, $changeNote): ArticleRelease {
$rows = $this->checkedRows($root);
$impact = $this->impact($article, count($rows));
$release = $article->releases()->create([
'number' => ((int) $article->releases()->max('number')) + 1,
'classification_note' => $classification,
'status' => ReleaseStatus::Entwurf,
'change_note' => $changeNote,
'impact_report' => $impact,
'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');
});
}
/**
* @return array{fallback_number: int|null, holders: list<string>}
*/
public function withdraw(LicenseArticle $article, string $reason): array
{
$reason = trim($reason);
if ($reason === '') {
throw new DomainRuleException('Ein Rückzug braucht eine Begründung.');
}
return DB::transaction(function () use ($article, $reason): array {
$article->refresh();
$current = $article->currentRelease;
if ($current === null || $current->status !== ReleaseStatus::Veroeffentlicht) {
throw new DomainRuleException('Zurückziehen lässt sich nur der aktuell veröffentlichte Stand.');
}
$fallback = ArticleRelease::query()
->where('license_article_id', $article->id)
->where('status', ReleaseStatus::AbgeloestVerfuegbar)
->orderByDesc('number')
->first();
$current->status = ReleaseStatus::Gesperrt;
$current->block_reason = $reason;
$current->fallback_release_id = $fallback?->id;
$current->save();
if ($fallback instanceof ArticleRelease) {
$fallback->status = ReleaseStatus::Veroeffentlicht;
$fallback->save();
$article->current_release_id = $fallback->id;
} else {
$article->current_release_id = null;
}
$article->save();
return [
'fallback_number' => $fallback?->number,
'holders' => Acquisition::query()
->whereHas('grants', fn ($query) => $query
->where('license_article_id', $article->id)
->where('status', GrantStatus::Aktiv))
->with('party')
->get()
->map(fn (Acquisition $acquisition): string => $acquisition->party->display_name)
->unique()
->values()
->all(),
];
});
}
/**
* @return list<array{version_id: int, role: string, path: string}>
*/
private function checkedRows(ContentElementVersion|CompositionVersion $root): array
{
$root->refresh();
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.');
}
$blocked = ContentElementVersion::query()
->whereIn('id', array_column($rows, 'version_id'))
->where('status', '!=', ContentVersionStatus::Bereit->value)
->exists();
if ($blocked) {
throw new DomainRuleException('Eine gesperrte Inhaltsfassung kann nicht veröffentlicht werden.');
}
return $rows;
}
/**
* @return array{holders: int, offers: list<string>, contents: int, replaces_number: int|null}
*/
private function impact(LicenseArticle $article, int $contents): array
{
$article->loadMissing('currentRelease');
return [
'holders' => Acquisition::query()
->whereHas('grants', fn ($query) => $query
->where('license_article_id', $article->id)
->where('status', GrantStatus::Aktiv))
->distinct()
->count('party_id'),
'offers' => Offer::query()
->whereHas('versions', fn ($query) => $query
->where('status', OfferStatus::Aktiv)
->whereHas('resolvedScopes', fn ($scope) => $scope->where('license_article_id', $article->id)))
->orderBy('name')
->pluck('name')
->all(),
'contents' => $contents,
'replaces_number' => $article->currentRelease?->number,
];
}
}