Ergänze das U1-Fachmodell für Inhalt, Paket und Angebot.
Deploy Test / test (push) Successful in 12s
Deploy Test / deploy (push) Failing after 14s

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-11-15 08:39:02 +01:00
1 parent 2035c46edd
commit 2ded4b7fad
35 files changed
+2115

No files matched your search

+78
View File
@@ -0,0 +1,78 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class ArticleRelease extends Model
{
protected $fillable = [
'license_article_id',
'number',
'change_kind',
'important',
'classification_note',
'status',
'block_reason',
'fallback_release_id',
'change_note',
'root_element_version_id',
'root_composition_version_id',
];
protected function casts(): array
{
return [
'important' => 'boolean',
'status' => ReleaseStatus::class,
];
}
protected static function booted(): void
{
static::updating(function (ArticleRelease $release): void {
$original = StatusValue::of($release->getOriginal('status'));
if (! in_array($original, [
ReleaseStatus::Veroeffentlicht->value,
ReleaseStatus::AbgeloestVerfuegbar->value,
], true)) {
return;
}
$dirty = array_diff(array_keys($release->getDirty()), ['status', 'updated_at']);
if ($dirty !== []) {
throw new DomainRuleException('Ein veröffentlichter Artikel-Stand ist unveränderlich.');
}
$next = StatusValue::of($release->status);
if ($original === ReleaseStatus::Veroeffentlicht->value && $next !== ReleaseStatus::AbgeloestVerfuegbar->value) {
throw new DomainRuleException('Ein veröffentlichter Artikel-Stand kann nur abgelöst werden.');
}
});
}
public function article(): BelongsTo
{
return $this->belongsTo(LicenseArticle::class, 'license_article_id');
}
public function contents(): HasMany
{
return $this->hasMany(ReleaseContent::class)->orderBy('position');
}
public function rootElementVersion(): BelongsTo
{
return $this->belongsTo(ContentElementVersion::class, 'root_element_version_id');
}
public function rootCompositionVersion(): BelongsTo
{
return $this->belongsTo(CompositionVersion::class, 'root_composition_version_id');
}
}
+83
View File
@@ -0,0 +1,83 @@
<?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');
});
}
}
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Bundle extends Model
{
protected $fillable = [
'name',
'archived_at',
];
protected function casts(): array
{
return [
'archived_at' => 'datetime',
];
}
public function versions(): HasMany
{
return $this->hasMany(BundleVersion::class);
}
}
+46
View File
@@ -0,0 +1,46 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class BundleItem extends Model
{
protected $fillable = [
'bundle_version_id',
'position',
'license_article_id',
'nested_bundle_id',
];
protected static function booted(): void
{
static::creating(function (BundleItem $item): void {
$status = BundleVersion::query()->whereKey($item->bundle_version_id)->value('status');
if (StatusValue::of($status) === BundleStatus::Veroeffentlicht->value) {
throw new DomainRuleException('Eine veröffentlichte Paketfassung nimmt keine weiteren Bestandteile auf.');
}
});
static::updating(function (): void {
throw new DomainRuleException('Bestandteile einer Paketfassung sind unveränderlich.');
});
}
public function bundleVersion(): BelongsTo
{
return $this->belongsTo(BundleVersion::class);
}
public function article(): BelongsTo
{
return $this->belongsTo(LicenseArticle::class, 'license_article_id');
}
public function nestedBundle(): BelongsTo
{
return $this->belongsTo(Bundle::class, 'nested_bundle_id');
}
}
+137
View File
@@ -0,0 +1,137 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Support\Facades\DB;
class BundleService
{
public function create(string $name): Bundle
{
$name = trim($name);
if ($name === '') {
throw new DomainRuleException('Der Name des Pakets fehlt.');
}
return Bundle::query()->create([
'name' => $name,
]);
}
/**
* @param list<LicenseArticle|Bundle> $items
*/
public function publish(Bundle $bundle, array $items): BundleVersion
{
if ($items === []) {
throw new DomainRuleException('Ein Paket braucht mindestens einen Bestandteil.');
}
$this->assertItems($items);
$this->collectArticleIds($bundle->id, $items);
return DB::transaction(function () use ($bundle, $items): BundleVersion {
$version = $bundle->versions()->create([
'number' => ((int) $bundle->versions()->max('number')) + 1,
'status' => BundleStatus::Entwurf,
]);
foreach (array_values($items) as $index => $item) {
$version->items()->create([
'position' => $index + 1,
'license_article_id' => $item instanceof LicenseArticle ? $item->id : null,
'nested_bundle_id' => $item instanceof Bundle ? $item->id : null,
]);
}
$version->status = BundleStatus::Veroeffentlicht;
$version->save();
return $version->load('items');
});
}
/**
* @return list<int>
*/
public function articleIds(BundleVersion $version): array
{
return $this->collectArticleIds($version->bundle_id, $this->targetsOf($version));
}
/**
* @param list<LicenseArticle|Bundle> $items
*/
private function assertItems(array $items): void
{
foreach ($items as $item) {
if (! $item instanceof LicenseArticle && ! $item instanceof Bundle) {
throw new DomainRuleException('Ein Paket enthält nur Lizenzartikel oder andere Pakete.');
}
}
}
/**
* @param list<LicenseArticle|Bundle> $items
* @param list<int> $stack
* @return list<int>
*/
private function collectArticleIds(int $bundleId, array $items, array $stack = []): array
{
if (in_array($bundleId, $stack, true)) {
throw new DomainRuleException('Pakete dürfen keinen Zyklus bilden.');
}
$stack[] = $bundleId;
$articleIds = [];
foreach ($items as $item) {
if ($item instanceof LicenseArticle) {
$articleIds[$item->id] = $item->id;
continue;
}
$published = $this->publishedVersion($item);
if ($published === null) {
throw new DomainRuleException('Ein eingebettetes Paket braucht eine veröffentlichte Fassung.');
}
foreach ($this->collectArticleIds($item->id, $this->targetsOf($published), $stack) as $articleId) {
$articleIds[$articleId] = $articleId;
}
}
return array_values($articleIds);
}
private function publishedVersion(Bundle $bundle): ?BundleVersion
{
return $bundle->versions()
->where('status', BundleStatus::Veroeffentlicht)
->orderByDesc('number')
->first();
}
/**
* @return list<LicenseArticle|Bundle>
*/
private function targetsOf(BundleVersion $version): array
{
$targets = [];
foreach ($version->items()->orderBy('position')->get() as $item) {
$target = $item->article ?? $item->nestedBundle;
if ($target === null) {
throw new DomainRuleException('Ein Paketbestandteil braucht genau ein Ziel.');
}
$targets[] = $target;
}
return $targets;
}
}
+9
View File
@@ -0,0 +1,9 @@
<?php
namespace App\Domain\Catalog;
enum BundleStatus: string
{
case Entwurf = 'entwurf';
case Veroeffentlicht = 'veroeffentlicht';
}
+42
View File
@@ -0,0 +1,42 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class BundleVersion extends Model
{
protected $fillable = [
'bundle_id',
'number',
'status',
];
protected function casts(): array
{
return [
'status' => BundleStatus::class,
];
}
protected static function booted(): void
{
static::updating(function (BundleVersion $version): void {
if (StatusValue::of($version->getOriginal('status')) === BundleStatus::Veroeffentlicht->value) {
throw new DomainRuleException('Eine veröffentlichte Paketfassung ist unveränderlich.');
}
});
}
public function bundle(): BelongsTo
{
return $this->belongsTo(Bundle::class);
}
public function items(): HasMany
{
return $this->hasMany(BundleItem::class)->orderBy('position');
}
}
@@ -0,0 +1,37 @@
<?php
namespace App\Domain\Catalog;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class CompatibilityAttestation extends Model
{
public $timestamps = false;
protected $fillable = [
'composition_version_id',
'resolved_hash',
'confirmed_by',
'confirmed_at',
'note',
];
protected function casts(): array
{
return [
'confirmed_at' => 'datetime',
];
}
public function compositionVersion(): BelongsTo
{
return $this->belongsTo(CompositionVersion::class);
}
public function confirmer(): BelongsTo
{
return $this->belongsTo(User::class, 'confirmed_by');
}
}
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Composition extends Model
{
protected $fillable = [
'designation',
'archived_at',
];
protected function casts(): array
{
return [
'archived_at' => 'datetime',
];
}
public function versions(): HasMany
{
return $this->hasMany(CompositionVersion::class);
}
}
+47
View File
@@ -0,0 +1,47 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class CompositionMember extends Model
{
protected $fillable = [
'composition_version_id',
'position',
'role',
'content_element_version_id',
'nested_composition_version_id',
];
protected static function booted(): void
{
static::creating(function (CompositionMember $member): void {
$status = CompositionVersion::query()->whereKey($member->composition_version_id)->value('status');
if (StatusValue::of($status) === ContentVersionStatus::Bereit->value) {
throw new DomainRuleException('Eine bestätigte Kompositionsfassung nimmt keine weiteren Mitglieder auf.');
}
});
static::updating(function (): void {
throw new DomainRuleException('Mitglieder einer Komposition werden nicht verändert, sondern durch eine neue Fassung ersetzt.');
});
}
public function compositionVersion(): BelongsTo
{
return $this->belongsTo(CompositionVersion::class);
}
public function contentElementVersion(): BelongsTo
{
return $this->belongsTo(ContentElementVersion::class);
}
public function nestedCompositionVersion(): BelongsTo
{
return $this->belongsTo(CompositionVersion::class, 'nested_composition_version_id');
}
}
+49
View File
@@ -0,0 +1,49 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class CompositionVersion extends Model
{
protected $fillable = [
'composition_id',
'number',
'status',
'members_hash',
'resolved_hash',
];
protected function casts(): array
{
return [
'status' => ContentVersionStatus::class,
];
}
protected static function booted(): void
{
static::updating(function (CompositionVersion $version): void {
if (StatusValue::of($version->getOriginal('status')) === ContentVersionStatus::Bereit->value) {
throw new DomainRuleException('Eine bestätigte Kompositionsfassung ist unveränderlich.');
}
});
}
public function composition(): BelongsTo
{
return $this->belongsTo(Composition::class);
}
public function members(): HasMany
{
return $this->hasMany(CompositionMember::class)->orderBy('position');
}
public function attestations(): HasMany
{
return $this->hasMany(CompatibilityAttestation::class);
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class ContentElement extends Model
{
protected $fillable = [
'kind',
'output_class',
'working_title',
'archived_at',
];
protected function casts(): array
{
return [
'archived_at' => 'datetime',
];
}
public function versions(): HasMany
{
return $this->hasMany(ContentElementVersion::class);
}
}
@@ -0,0 +1,44 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ContentElementVersion extends Model
{
protected $fillable = [
'content_element_id',
'number',
'status',
'content_hash',
'body',
'metadata',
'change_note',
];
protected function casts(): array
{
return [
'status' => ContentVersionStatus::class,
'metadata' => 'array',
];
}
protected static function booted(): void
{
static::updating(function (ContentElementVersion $version): void {
if (in_array(StatusValue::of($version->getOriginal('status')), [
ContentVersionStatus::Bereit->value,
ContentVersionStatus::Gesperrt->value,
], true)) {
throw new DomainRuleException('Eine bereitgestellte Inhaltsfassung ist unveränderlich.');
}
});
}
public function element(): BelongsTo
{
return $this->belongsTo(ContentElement::class, 'content_element_id');
}
}
+61
View File
@@ -0,0 +1,61 @@
<?php
namespace App\Domain\Catalog;
class ContentResolver
{
/**
* @return list<array{version_id: int, role: string, path: string}>
*/
public function expand(ContentElementVersion|CompositionVersion $node, string $path = '', array $stack = []): array
{
if ($node instanceof ContentElementVersion) {
$node->loadMissing('element');
return [[
'version_id' => $node->id,
'role' => $path,
'path' => $path !== '' ? $path : $node->element->working_title,
]];
}
if (in_array($node->composition_id, $stack, true)) {
throw new DomainRuleException('Kompositionen dürfen keinen Zyklus bilden.');
}
$stack[] = $node->composition_id;
$rows = [];
foreach ($node->members()->orderBy('position')->get() as $member) {
$segment = $path === '' ? $member->role : $path.' / '.$member->role;
if ($member->content_element_version_id !== null) {
$rows[] = [
'version_id' => $member->content_element_version_id,
'role' => $member->role,
'path' => $segment,
];
continue;
}
$nested = $member->nestedCompositionVersion;
if ($nested === null) {
throw new DomainRuleException('Ein Kompositionsmitglied braucht genau ein Ziel.');
}
$rows = array_merge($rows, $this->expand($nested, $segment, $stack));
}
return $rows;
}
public function resolvedHash(ContentElementVersion|CompositionVersion $node): string
{
$ids = array_values(array_unique(array_column($this->expand($node), 'version_id')));
sort($ids);
return hash('sha256', json_encode($ids, JSON_THROW_ON_ERROR));
}
}
+196
View File
@@ -0,0 +1,196 @@
<?php
namespace App\Domain\Catalog;
use App\Models\User;
use Illuminate\Support\Facades\DB;
class ContentService
{
public function __construct(private readonly ContentResolver $resolver) {}
public function createElement(string $kind, string $outputClass, string $workingTitle): ContentElement
{
return ContentElement::query()->create([
'kind' => $this->required($kind, 'Die Art des Inhaltselements fehlt.'),
'output_class' => $this->required($outputClass, 'Die Ausgabeklasse fehlt.'),
'working_title' => $this->required($workingTitle, 'Der Arbeitstitel fehlt.'),
]);
}
public function publishElementVersion(
ContentElement $element,
?string $body = null,
array $metadata = [],
?string $changeNote = null,
): ContentElementVersion {
if (($body === null || $body === '') && $metadata === []) {
throw new DomainRuleException('Eine Inhaltsfassung braucht einen Inhalt.');
}
$number = ((int) $element->versions()->max('number')) + 1;
return $element->versions()->create([
'number' => $number,
'status' => ContentVersionStatus::Bereit,
'content_hash' => hash('sha256', json_encode([
'body' => $body,
'metadata' => $metadata,
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE)),
'body' => $body,
'metadata' => $metadata === [] ? null : $metadata,
'change_note' => $changeNote,
]);
}
/**
* @param list<array{role: string, target: ContentElementVersion|CompositionVersion}> $members
*/
public function publishComposition(
Composition $composition,
array $members,
?User $confirmedBy = null,
?string $note = null,
): CompositionVersion {
if ($members === []) {
throw new DomainRuleException('Eine Komposition braucht mindestens ein Mitglied.');
}
$this->assertAcyclic($composition->id, $members);
foreach ($members as $member) {
$this->assertReadyMember($member['target']);
}
return DB::transaction(function () use ($composition, $members, $confirmedBy, $note): CompositionVersion {
$number = ((int) $composition->versions()->max('number')) + 1;
$version = $composition->versions()->create([
'number' => $number,
'status' => ContentVersionStatus::Entwurf,
'members_hash' => $this->membersHash($members),
'resolved_hash' => 'pending',
]);
foreach (array_values($members) as $index => $member) {
$target = $member['target'];
$version->members()->create([
'position' => $index + 1,
'role' => $this->required($member['role'], 'Die Rolle eines Mitglieds fehlt.'),
'content_element_version_id' => $target instanceof ContentElementVersion ? $target->id : null,
'nested_composition_version_id' => $target instanceof CompositionVersion ? $target->id : null,
]);
}
$resolvedHash = $this->resolver->resolvedHash($version);
$version->attestations()->create([
'resolved_hash' => $resolvedHash,
'confirmed_by' => $confirmedBy?->id,
'confirmed_at' => now(),
'note' => $note,
]);
$version->resolved_hash = $resolvedHash;
$version->status = ContentVersionStatus::Bereit;
$version->save();
return $version->load('members');
});
}
public function createComposition(string $designation): Composition
{
return Composition::query()->create([
'designation' => $this->required($designation, 'Die Bezeichnung der Komposition fehlt.'),
]);
}
/**
* @return list<int>
*/
public function containingCompositionIds(ContentElementVersion $version): array
{
$ids = [];
foreach (CompositionVersion::query()->where('status', ContentVersionStatus::Bereit)->get() as $compositionVersion) {
$contained = array_column($this->resolver->expand($compositionVersion), 'version_id');
if (in_array($version->id, $contained, true)) {
$ids[] = $compositionVersion->id;
}
}
return $ids;
}
/**
* @param list<array{role: string, target: ContentElementVersion|CompositionVersion}> $members
* @param list<int> $stack
*/
private function assertAcyclic(int $compositionId, array $members, array $stack = []): void
{
if (in_array($compositionId, $stack, true)) {
throw new DomainRuleException('Kompositionen dürfen keinen Zyklus bilden.');
}
$stack[] = $compositionId;
foreach ($members as $member) {
$target = $member['target'];
if (! $target instanceof CompositionVersion) {
continue;
}
$nested = [];
foreach ($target->members()->orderBy('position')->get() as $row) {
$child = $row->contentElementVersion ?? $row->nestedCompositionVersion;
if ($child === null) {
throw new DomainRuleException('Ein Kompositionsmitglied braucht genau ein Ziel.');
}
$nested[] = ['role' => $row->role, 'target' => $child];
}
$this->assertAcyclic($target->composition_id, $nested, $stack);
}
}
private function assertReadyMember(ContentElementVersion|CompositionVersion $target): void
{
if ($target->status !== ContentVersionStatus::Bereit) {
throw new DomainRuleException('Eine Komposition darf nur bereitgestellte Fassungen enthalten.');
}
}
/**
* @param list<array{role: string, target: ContentElementVersion|CompositionVersion}> $members
*/
private function membersHash(array $members): string
{
$list = [];
foreach (array_values($members) as $index => $member) {
$target = $member['target'];
$list[] = [
'position' => $index + 1,
'role' => $member['role'],
'element_version_id' => $target instanceof ContentElementVersion ? $target->id : null,
'composition_version_id' => $target instanceof CompositionVersion ? $target->id : null,
];
}
return hash('sha256', json_encode($list, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
}
private function required(string $value, string $message): string
{
$value = trim($value);
if ($value === '') {
throw new DomainRuleException($message);
}
return $value;
}
}
@@ -0,0 +1,10 @@
<?php
namespace App\Domain\Catalog;
enum ContentVersionStatus: string
{
case Entwurf = 'entwurf';
case Bereit = 'bereit';
case Gesperrt = 'gesperrt';
}
@@ -0,0 +1,7 @@
<?php
namespace App\Domain\Catalog;
use RuntimeException;
class DomainRuleException extends RuntimeException {}
+40
View File
@@ -0,0 +1,40 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class LicenseArticle extends Model
{
protected $fillable = [
'code',
'product_line',
'successor_of_id',
'current_release_id',
'archived_at',
];
protected function casts(): array
{
return [
'archived_at' => 'datetime',
];
}
public function releases(): HasMany
{
return $this->hasMany(ArticleRelease::class);
}
public function currentRelease(): BelongsTo
{
return $this->belongsTo(ArticleRelease::class, 'current_release_id');
}
public function successorOf(): BelongsTo
{
return $this->belongsTo(self::class, 'successor_of_id');
}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Offer extends Model
{
protected $fillable = [
'name',
];
public function versions(): HasMany
{
return $this->hasMany(OfferVersion::class);
}
}
+55
View File
@@ -0,0 +1,55 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class OfferLine extends Model
{
protected $fillable = [
'offer_version_id',
'position',
'license_article_id',
'bundle_version_id',
'rights_profile_version_id',
];
protected static function booted(): void
{
static::creating(function (OfferLine $line): void {
$status = OfferVersion::query()->whereKey($line->offer_version_id)->value('status');
if (in_array(StatusValue::of($status), [
OfferStatus::Aktiv->value,
OfferStatus::Beendet->value,
], true)) {
throw new DomainRuleException('Eine aktivierte Angebotsfassung nimmt keine weiteren Positionen auf.');
}
});
static::updating(function (): void {
throw new DomainRuleException('Positionen einer Angebotsfassung sind unveränderlich.');
});
}
public function offerVersion(): BelongsTo
{
return $this->belongsTo(OfferVersion::class);
}
public function article(): BelongsTo
{
return $this->belongsTo(LicenseArticle::class, 'license_article_id');
}
public function bundleVersion(): BelongsTo
{
return $this->belongsTo(BundleVersion::class);
}
public function rights(): BelongsTo
{
return $this->belongsTo(RightsProfileVersion::class, 'rights_profile_version_id');
}
}
+182
View File
@@ -0,0 +1,182 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Support\Facades\DB;
class OfferService
{
public function __construct(private readonly BundleService $bundles) {}
/**
* @param array<string, mixed> $capabilities
* @param array<string, mixed> $options
*/
public function createRightsProfile(
string $name,
string $purpose,
array $capabilities,
array $options = [],
): RightsProfileVersion {
$purpose = trim($purpose);
if (! in_array($purpose, ['ehrenamtlich', 'kommerziell', 'institutionell'], true)) {
throw new DomainRuleException('Der Nutzungszweck eines Rechteprofils ist ehrenamtlich, kommerziell oder institutionell.');
}
if ($capabilities === []) {
throw new DomainRuleException('Ein Rechteprofil braucht mindestens eine Fähigkeit.');
}
$profile = RightsProfile::query()->create([
'name' => trim($name) !== '' ? trim($name) : throw new DomainRuleException('Der Name des Rechteprofils fehlt.'),
]);
return $profile->versions()->create([
'number' => 1,
'purpose' => $purpose,
'capabilities' => $capabilities,
'options' => $options,
]);
}
public function create(string $name): Offer
{
$name = trim($name);
if ($name === '') {
throw new DomainRuleException('Der Name des Angebots fehlt.');
}
return Offer::query()->create([
'name' => $name,
]);
}
/**
* @param list<array{target: LicenseArticle|BundleVersion, rights: RightsProfileVersion}> $lines
* @param array<string, mixed> $attributes
*/
public function activate(Offer $offer, array $lines, array $attributes = []): OfferVersion
{
if ($lines === []) {
throw new DomainRuleException('Eine Angebotsfassung braucht mindestens eine Position.');
}
$attributes = array_merge([
'price_cents' => null,
'tax_class' => null,
'term_rule' => 'dauerhaft',
'update_promise' => 'alle_freigegebenen_fassungen',
'dynamic' => false,
'grant_kind' => 'kauf',
'holder_kind' => 'person',
], $attributes);
$this->assertAttributes($attributes);
$scope = $this->scopeFor($lines);
return DB::transaction(function () use ($offer, $lines, $attributes, $scope): OfferVersion {
$version = $offer->versions()->create([
'number' => ((int) $offer->versions()->max('number')) + 1,
'status' => OfferStatus::Entwurf,
'price_cents' => $attributes['price_cents'],
'tax_class' => $attributes['tax_class'],
'term_rule' => $attributes['term_rule'],
'update_promise' => $attributes['update_promise'],
'dynamic' => $attributes['dynamic'],
'grant_kind' => $attributes['grant_kind'],
'holder_kind' => $attributes['holder_kind'],
]);
foreach (array_values($lines) as $index => $line) {
$target = $line['target'];
$version->lines()->create([
'position' => $index + 1,
'license_article_id' => $target instanceof LicenseArticle ? $target->id : null,
'bundle_version_id' => $target instanceof BundleVersion ? $target->id : null,
'rights_profile_version_id' => $line['rights']->id,
]);
}
foreach ($scope as $entry) {
$version->resolvedScopes()->create($entry);
}
RightsProfileVersion::query()
->whereIn('id', array_column($scope, 'rights_profile_version_id'))
->whereNull('locked_at')
->update(['locked_at' => now()]);
$version->status = OfferStatus::Aktiv;
$version->activated_at = now();
$version->save();
return $version->load('resolvedScopes');
});
}
/**
* @return list<int>
*/
public function scopeArticleIds(OfferVersion $version): array
{
return $version->resolvedScopes()->orderBy('id')->pluck('license_article_id')->all();
}
/**
* @param array<string, mixed> $attributes
*/
private function assertAttributes(array $attributes): void
{
if (! in_array($attributes['term_rule'], ['dauerhaft', 'befristet', 'solange_angebot_aktiv', 'periodisch'], true)) {
throw new DomainRuleException('Die Laufzeitregel der Angebotsfassung ist unbekannt.');
}
if (! in_array($attributes['grant_kind'], ['kauf', 'bei_registrierung', 'auf_abruf'], true)) {
throw new DomainRuleException('Die Vergabeart der Angebotsfassung ist unbekannt.');
}
if (! in_array($attributes['holder_kind'], ['person', 'organisation'], true)) {
throw new DomainRuleException('Die Inhaberart der Angebotsfassung ist Person oder Organisation.');
}
if ($attributes['update_promise'] !== 'alle_freigegebenen_fassungen') {
throw new DomainRuleException('Die Updatezusage umfasst alle später freigegebenen Fassungen des Artikels.');
}
}
/**
* @param list<array{target: LicenseArticle|BundleVersion, rights: RightsProfileVersion}> $lines
* @return list<array{license_article_id: int, rights_profile_version_id: int}>
*/
private function scopeFor(array $lines): array
{
$scope = [];
foreach ($lines as $line) {
$target = $line['target'];
$rights = $line['rights'];
if ($target instanceof BundleVersion && $target->status !== BundleStatus::Veroeffentlicht) {
throw new DomainRuleException('Eine Angebotsfassung enthält nur veröffentlichte Paketfassungen.');
}
$articleIds = match (true) {
$target instanceof LicenseArticle => [$target->id],
$target instanceof BundleVersion => $this->bundles->articleIds($target),
default => throw new DomainRuleException('Eine Angebotsposition zeigt auf einen Lizenzartikel oder eine Paketfassung.'),
};
foreach ($articleIds as $articleId) {
$key = $articleId.'-'.$rights->id;
$scope[$key] = [
'license_article_id' => $articleId,
'rights_profile_version_id' => $rights->id,
];
}
}
return array_values($scope);
}
}
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace App\Domain\Catalog;
enum OfferStatus: string
{
case Entwurf = 'entwurf';
case Aktiv = 'aktiv';
case Beendet = 'beendet';
}
+60
View File
@@ -0,0 +1,60 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class OfferVersion extends Model
{
protected $fillable = [
'offer_id',
'number',
'status',
'price_cents',
'tax_class',
'term_rule',
'update_promise',
'dynamic',
'grant_kind',
'holder_kind',
'activated_at',
];
protected function casts(): array
{
return [
'status' => OfferStatus::class,
'dynamic' => 'boolean',
'activated_at' => 'datetime',
];
}
protected static function booted(): void
{
static::updating(function (OfferVersion $version): void {
if (in_array(StatusValue::of($version->getOriginal('status')), [
OfferStatus::Aktiv->value,
OfferStatus::Beendet->value,
], true)) {
throw new DomainRuleException('Eine aktivierte Angebotsfassung ist unveränderlich.');
}
});
}
public function offer(): BelongsTo
{
return $this->belongsTo(Offer::class);
}
public function lines(): HasMany
{
return $this->hasMany(OfferLine::class)->orderBy('position');
}
public function resolvedScopes(): HasMany
{
return $this->hasMany(ResolvedScope::class);
}
}
+42
View File
@@ -0,0 +1,42 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ReleaseContent extends Model
{
protected $fillable = [
'article_release_id',
'position',
'content_element_version_id',
'role',
'path',
];
protected static function booted(): void
{
static::creating(function (ReleaseContent $content): void {
$status = StatusValue::of(ArticleRelease::query()->whereKey($content->article_release_id)->value('status'));
if ($status !== ReleaseStatus::Entwurf->value) {
throw new DomainRuleException('Der Inhalt eines Artikel-Stands wird nur im Entwurf festgeschrieben.');
}
});
static::updating(function (): void {
throw new DomainRuleException('Der festgeschriebene Inhalt eines Artikel-Stands ist unveränderlich.');
});
}
public function release(): BelongsTo
{
return $this->belongsTo(ArticleRelease::class, 'article_release_id');
}
public function elementVersion(): BelongsTo
{
return $this->belongsTo(ContentElementVersion::class, 'content_element_version_id');
}
}
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace App\Domain\Catalog;
enum ReleaseStatus: string
{
case Entwurf = 'entwurf';
case Veroeffentlicht = 'veroeffentlicht';
case AbgeloestVerfuegbar = 'abgeloest_verfuegbar';
}
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ResolvedScope extends Model
{
protected $fillable = [
'offer_version_id',
'license_article_id',
'rights_profile_version_id',
];
protected static function booted(): void
{
static::updating(function (): void {
throw new DomainRuleException('Der aufgelöste Leistungsumfang einer Angebotsfassung ist unveränderlich.');
});
}
public function offerVersion(): BelongsTo
{
return $this->belongsTo(OfferVersion::class);
}
public function article(): BelongsTo
{
return $this->belongsTo(LicenseArticle::class, 'license_article_id');
}
public function rights(): BelongsTo
{
return $this->belongsTo(RightsProfileVersion::class, 'rights_profile_version_id');
}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class RightsProfile extends Model
{
protected $fillable = [
'name',
];
public function versions(): HasMany
{
return $this->hasMany(RightsProfileVersion::class);
}
}
@@ -0,0 +1,41 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class RightsProfileVersion extends Model
{
protected $fillable = [
'rights_profile_id',
'number',
'purpose',
'capabilities',
'options',
'locked_at',
];
protected function casts(): array
{
return [
'capabilities' => 'array',
'options' => 'array',
'locked_at' => 'datetime',
];
}
protected static function booted(): void
{
static::updating(function (RightsProfileVersion $version): void {
if ($version->getOriginal('locked_at') !== null) {
throw new DomainRuleException('Eine verwendete Rechteprofil-Fassung ist unveränderlich.');
}
});
}
public function profile(): BelongsTo
{
return $this->belongsTo(RightsProfile::class, 'rights_profile_id');
}
}
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace App\Domain\Catalog;
use BackedEnum;
class StatusValue
{
public static function of(mixed $status): ?string
{
return $status instanceof BackedEnum ? $status->value : $status;
}
}
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace App\Domain\Entitlement;
use App\Domain\Identity\Party;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* Individuelle Berechtigungsquelle eines Inhabers. Die Entscheidung, wann ein Erwerb entsteht, folgt später.
* Der Bezug ist je Typ eine Bestellposition, ein Angebot, ein Verwaltungsvorgang oder ein Abonnement.
*/
class Acquisition extends Model
{
protected $fillable = [
'party_id',
'type',
'reference_type',
'reference_id',
'conditions',
];
protected function casts(): array
{
return [
'type' => AcquisitionType::class,
'conditions' => 'array',
];
}
public function party(): BelongsTo
{
return $this->belongsTo(Party::class);
}
public function grants(): HasMany
{
return $this->hasMany(Grant::class);
}
}
@@ -0,0 +1,11 @@
<?php
namespace App\Domain\Entitlement;
enum AcquisitionType: string
{
case Bestellposition = 'bestellposition';
case FreierAbruf = 'freier_abruf';
case ManuelleVergabe = 'manuelle_vergabe';
case Abonnement = 'abonnement';
}
+54
View File
@@ -0,0 +1,54 @@
<?php
namespace App\Domain\Entitlement;
use App\Domain\Catalog\ArticleRelease;
use App\Domain\Catalog\LicenseArticle;
use App\Domain\Catalog\RightsProfileVersion;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Grant extends Model
{
protected $fillable = [
'acquisition_id',
'license_article_id',
'rights_profile_version_id',
'rights_snapshot',
'valid_from',
'valid_until',
'release_at_grant_id',
'status',
'status_reason',
];
protected function casts(): array
{
return [
'rights_snapshot' => 'array',
'valid_from' => 'datetime',
'valid_until' => 'datetime',
'status' => GrantStatus::class,
];
}
public function acquisition(): BelongsTo
{
return $this->belongsTo(Acquisition::class);
}
public function article(): BelongsTo
{
return $this->belongsTo(LicenseArticle::class, 'license_article_id');
}
public function rights(): BelongsTo
{
return $this->belongsTo(RightsProfileVersion::class, 'rights_profile_version_id');
}
public function releaseAtGrant(): BelongsTo
{
return $this->belongsTo(ArticleRelease::class, 'release_at_grant_id');
}
}
+11
View File
@@ -0,0 +1,11 @@
<?php
namespace App\Domain\Entitlement;
enum GrantStatus: string
{
case Aktiv = 'aktiv';
case Ausgesetzt = 'ausgesetzt';
case Widerrufen = 'widerrufen';
case Abgelaufen = 'abgelaufen';
}
@@ -0,0 +1,268 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('content_elements', function (Blueprint $table) {
$table->id();
$table->string('kind');
$table->string('output_class');
$table->string('working_title');
$table->timestamp('archived_at')->nullable();
$table->timestamps();
});
Schema::create('content_element_versions', function (Blueprint $table) {
$table->id();
$table->foreignId('content_element_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('number');
$table->string('status');
$table->string('content_hash');
$table->text('body')->nullable();
$table->json('metadata')->nullable();
$table->text('change_note')->nullable();
$table->timestamps();
$table->unique(['content_element_id', 'number']);
});
Schema::create('compositions', function (Blueprint $table) {
$table->id();
$table->string('designation');
$table->timestamp('archived_at')->nullable();
$table->timestamps();
});
Schema::create('composition_versions', function (Blueprint $table) {
$table->id();
$table->foreignId('composition_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('number');
$table->string('status');
$table->string('members_hash');
$table->string('resolved_hash');
$table->timestamps();
$table->unique(['composition_id', 'number']);
});
Schema::create('composition_members', function (Blueprint $table) {
$table->id();
$table->foreignId('composition_version_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('position');
$table->string('role');
$table->foreignId('content_element_version_id')->nullable()->constrained()->restrictOnDelete();
$table->foreignId('nested_composition_version_id')->nullable()->constrained('composition_versions')->restrictOnDelete();
$table->timestamps();
$table->unique(['composition_version_id', 'position']);
});
Schema::create('compatibility_attestations', function (Blueprint $table) {
$table->id();
$table->foreignId('composition_version_id')->constrained()->cascadeOnDelete();
$table->string('resolved_hash');
$table->foreignId('confirmed_by')->nullable()->constrained('users')->nullOnDelete();
$table->timestamp('confirmed_at');
$table->text('note')->nullable();
});
Schema::create('license_articles', function (Blueprint $table) {
$table->id();
$table->string('code')->unique();
$table->string('product_line')->nullable();
$table->foreignId('successor_of_id')->nullable()->constrained('license_articles')->nullOnDelete();
$table->timestamp('archived_at')->nullable();
$table->timestamps();
});
Schema::create('article_releases', function (Blueprint $table) {
$table->id();
$table->foreignId('license_article_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('number');
$table->string('change_kind')->nullable();
$table->boolean('important')->default(false);
$table->text('classification_note');
$table->string('status');
$table->text('block_reason')->nullable();
$table->foreignId('fallback_release_id')->nullable()->constrained('article_releases')->nullOnDelete();
$table->text('change_note')->nullable();
$table->foreignId('root_element_version_id')->nullable()->constrained('content_element_versions')->restrictOnDelete();
$table->foreignId('root_composition_version_id')->nullable()->constrained('composition_versions')->restrictOnDelete();
$table->timestamps();
$table->unique(['license_article_id', 'number']);
});
Schema::create('release_contents', function (Blueprint $table) {
$table->id();
$table->foreignId('article_release_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('position');
$table->foreignId('content_element_version_id')->constrained()->restrictOnDelete();
$table->string('role');
$table->string('path');
$table->timestamps();
$table->unique(['article_release_id', 'position']);
});
Schema::table('license_articles', function (Blueprint $table) {
$table->foreignId('current_release_id')->nullable()->constrained('article_releases')->nullOnDelete();
});
Schema::create('bundles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamp('archived_at')->nullable();
$table->timestamps();
});
Schema::create('bundle_versions', function (Blueprint $table) {
$table->id();
$table->foreignId('bundle_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('number');
$table->string('status');
$table->timestamps();
$table->unique(['bundle_id', 'number']);
});
Schema::create('bundle_items', function (Blueprint $table) {
$table->id();
$table->foreignId('bundle_version_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('position');
$table->foreignId('license_article_id')->nullable()->constrained()->restrictOnDelete();
$table->foreignId('nested_bundle_id')->nullable()->constrained('bundles')->restrictOnDelete();
$table->timestamps();
$table->unique(['bundle_version_id', 'position']);
});
Schema::create('rights_profiles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
Schema::create('rights_profile_versions', function (Blueprint $table) {
$table->id();
$table->foreignId('rights_profile_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('number');
$table->string('purpose');
$table->json('capabilities');
$table->json('options');
$table->timestamp('locked_at')->nullable();
$table->timestamps();
$table->unique(['rights_profile_id', 'number']);
});
Schema::create('offers', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
Schema::create('offer_versions', function (Blueprint $table) {
$table->id();
$table->foreignId('offer_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('number');
$table->string('status');
$table->unsignedInteger('price_cents')->nullable();
$table->string('tax_class')->nullable();
$table->string('term_rule');
$table->string('update_promise');
$table->boolean('dynamic')->default(false);
$table->string('grant_kind');
$table->string('holder_kind');
$table->timestamp('activated_at')->nullable();
$table->timestamps();
$table->unique(['offer_id', 'number']);
});
Schema::create('offer_lines', function (Blueprint $table) {
$table->id();
$table->foreignId('offer_version_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('position');
$table->foreignId('license_article_id')->nullable()->constrained()->restrictOnDelete();
$table->foreignId('bundle_version_id')->nullable()->constrained()->restrictOnDelete();
$table->foreignId('rights_profile_version_id')->constrained()->restrictOnDelete();
$table->timestamps();
$table->unique(['offer_version_id', 'position']);
});
Schema::create('resolved_scopes', function (Blueprint $table) {
$table->id();
$table->foreignId('offer_version_id')->constrained()->cascadeOnDelete();
$table->foreignId('license_article_id')->constrained()->restrictOnDelete();
$table->foreignId('rights_profile_version_id')->constrained()->restrictOnDelete();
$table->timestamps();
$table->unique(['offer_version_id', 'license_article_id', 'rights_profile_version_id'], 'resolved_scopes_unique');
});
Schema::create('acquisitions', function (Blueprint $table) {
$table->id();
$table->foreignId('party_id')->constrained()->restrictOnDelete();
$table->string('type');
$table->string('reference_type');
$table->unsignedBigInteger('reference_id');
$table->json('conditions')->nullable();
$table->timestamps();
$table->unique(['party_id', 'type', 'reference_type', 'reference_id'], 'acquisitions_holder_source_unique');
});
Schema::create('grants', function (Blueprint $table) {
$table->id();
$table->foreignId('acquisition_id')->constrained()->cascadeOnDelete();
$table->foreignId('license_article_id')->constrained()->restrictOnDelete();
$table->foreignId('rights_profile_version_id')->constrained()->restrictOnDelete();
$table->json('rights_snapshot');
$table->timestamp('valid_from')->nullable();
$table->timestamp('valid_until')->nullable();
$table->foreignId('release_at_grant_id')->nullable()->constrained('article_releases')->nullOnDelete();
$table->string('status');
$table->text('status_reason')->nullable();
$table->timestamps();
$table->unique(
['acquisition_id', 'license_article_id', 'rights_profile_version_id'],
'grants_acquisition_article_rights_unique',
);
});
}
public function down(): void
{
Schema::dropIfExists('grants');
Schema::dropIfExists('acquisitions');
Schema::dropIfExists('resolved_scopes');
Schema::dropIfExists('offer_lines');
Schema::dropIfExists('offer_versions');
Schema::dropIfExists('offers');
Schema::dropIfExists('rights_profile_versions');
Schema::dropIfExists('rights_profiles');
Schema::dropIfExists('bundle_items');
Schema::dropIfExists('bundle_versions');
Schema::dropIfExists('bundles');
Schema::table('license_articles', function (Blueprint $table) {
$table->dropConstrainedForeignId('current_release_id');
});
Schema::dropIfExists('release_contents');
Schema::dropIfExists('article_releases');
Schema::dropIfExists('license_articles');
Schema::dropIfExists('compatibility_attestations');
Schema::dropIfExists('composition_members');
Schema::dropIfExists('composition_versions');
Schema::dropIfExists('compositions');
Schema::dropIfExists('content_element_versions');
Schema::dropIfExists('content_elements');
}
};
+278
View File
@@ -0,0 +1,278 @@
<?php
namespace Tests\Feature;
use App\Domain\Catalog\ArticleService;
use App\Domain\Catalog\BundleService;
use App\Domain\Catalog\ContentElementVersion;
use App\Domain\Catalog\ContentService;
use App\Domain\Catalog\DomainRuleException;
use App\Domain\Catalog\LicenseArticle;
use App\Domain\Catalog\OfferService;
use App\Domain\Catalog\RightsProfileVersion;
use App\Domain\Entitlement\Acquisition;
use App\Domain\Entitlement\AcquisitionType;
use App\Domain\Entitlement\Grant;
use App\Domain\Entitlement\GrantStatus;
use App\Domain\Identity\Party;
use App\Domain\Identity\PartyType;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class CatalogRulesTest extends TestCase
{
use RefreshDatabase;
public function test_a_single_element_becomes_one_license_article(): void
{
$version = $this->element('Arbeitsheft', 'Seite 1');
$release = app(ArticleService::class)->publishRelease(
$this->article('arbeitsheft'),
$version,
'Eigenständiges Arbeitsheft, kein Bestandteil einer Übung.',
);
$this->assertSame([$version->id], $release->contents->pluck('content_element_version_id')->all());
$this->assertSame($version->id, $release->root_element_version_id);
}
public function test_b_composition_fixes_exact_member_versions(): void
{
$film = $this->element('Film', 'Aufnahme');
$sheet = $this->element('Arbeitsblatt', 'Aufgaben');
$composition = app(ContentService::class)->publishComposition(
app(ContentService::class)->createComposition('Übung'),
[
['role' => 'Film', 'target' => $film],
['role' => 'Arbeitsblatt', 'target' => $sheet],
],
);
$release = app(ArticleService::class)->publishRelease(
$this->article('uebung'),
$composition,
'Die Übung wird als ein Lizenzartikel geführt.',
);
$this->assertSame(
[$film->id, $sheet->id],
$release->contents->pluck('content_element_version_id')->all(),
);
$this->assertNotNull($composition->attestations()->first());
}
public function test_c_one_element_version_can_be_reused(): void
{
$shared = $this->element('Merkblatt', 'Hinweise');
$first = app(ContentService::class)->publishComposition(
app(ContentService::class)->createComposition('Übung A'),
[['role' => 'Merkblatt', 'target' => $shared]],
);
$second = app(ContentService::class)->publishComposition(
app(ContentService::class)->createComposition('Übung B'),
[['role' => 'Merkblatt', 'target' => $shared]],
);
$this->assertEqualsCanonicalizing(
[$first->id, $second->id],
app(ContentService::class)->containingCompositionIds($shared),
);
}
public function test_d_nested_composition_resolves_inner_versions_and_rejects_cycles(): void
{
$sheet = $this->element('Arbeitsblatt', 'Aufgaben');
$content = app(ContentService::class);
$inner = $content->publishComposition(
$content->createComposition('Materialsatz'),
[['role' => 'Arbeitsblatt', 'target' => $sheet]],
);
$outer = $content->publishComposition(
$content->createComposition('Übung'),
[['role' => 'Materialsatz', 'target' => $inner]],
);
$release = app(ArticleService::class)->publishRelease(
$this->article('uebung-mit-satz'),
$outer,
'Verschachtelte Übung, derselbe Artikel.',
);
$this->assertSame(['Materialsatz / Arbeitsblatt'], $release->contents->pluck('path')->all());
$this->assertSame([$sheet->id], $release->contents->pluck('content_element_version_id')->all());
$this->expectException(DomainRuleException::class);
$content->publishComposition(
$inner->composition,
[['role' => 'Übung', 'target' => $outer]],
);
}
public function test_e_one_article_can_belong_to_two_bundles(): void
{
$article = $this->publishedArticle('einzel', 'Blatt');
$bundles = app(BundleService::class);
$first = $bundles->publish($bundles->create('Paket eins'), [$article]);
$second = $bundles->publish($bundles->create('Paket zwei'), [$article]);
$this->assertSame([$article->id], $bundles->articleIds($first));
$this->assertSame([$article->id], $bundles->articleIds($second));
}
public function test_f_nested_bundles_reject_cycles_and_deduplicate_articles(): void
{
$article = $this->publishedArticle('gemeinsam', 'Blatt');
$bundles = app(BundleService::class);
$left = $bundles->create('Links');
$right = $bundles->create('Rechts');
$bundles->publish($left, [$article]);
$bundles->publish($right, [$article]);
$outer = $bundles->publish($bundles->create('Außen'), [$left, $right]);
$this->assertSame([$article->id], $bundles->articleIds($outer));
$this->expectException(DomainRuleException::class);
$bundles->publish($left, [$outer->bundle]);
}
public function test_g_a_new_element_version_does_not_change_a_published_composition(): void
{
$content = app(ContentService::class);
$element = $content->createElement('pdf', 'begleitmaterial', 'Anleitung');
$first = $content->publishElementVersion($element, 'Fassung 1');
$composition = $content->publishComposition(
$content->createComposition('Übung'),
[['role' => 'Anleitung', 'target' => $first]],
);
$release = app(ArticleService::class)->publishRelease(
$this->article('anleitung'),
$composition,
'Korrektur bleibt eine Fassung desselben Artikels.',
);
$second = $content->publishElementVersion($element->fresh(), 'Fassung 2');
$composition->refresh()->load('members');
$this->assertSame($first->id, $composition->members->sole()->content_element_version_id);
$this->assertNotSame($second->id, $composition->members->sole()->content_element_version_id);
$this->assertSame(
[$first->id],
$release->fresh()->contents->pluck('content_element_version_id')->all(),
);
$this->expectException(DomainRuleException::class);
$first->body = 'stillschweigend geändert';
$first->save();
}
public function test_h_a_bundle_change_does_not_change_an_activated_offer(): void
{
$first = $this->publishedArticle('grundlage', 'Grundlage');
$second = $this->publishedArticle('erweiterung', 'Erweiterung');
$bundles = app(BundleService::class);
$bundle = $bundles->create('Programm');
$published = $bundles->publish($bundle, [$first]);
$rights = $this->rights();
$offer = app(OfferService::class)->activate(
app(OfferService::class)->create('Programmangebot'),
[['target' => $published, 'rights' => $rights]],
);
$bundles->publish($bundle, [$first, $second]);
$offer->refresh();
$this->assertSame([$first->id], app(OfferService::class)->scopeArticleIds($offer));
$this->assertSame([$first->id], $bundles->articleIds($published->fresh()));
$this->assertNotNull($rights->fresh()->locked_at);
$this->expectException(DomainRuleException::class);
$offer->tax_class = 'unveraendert';
$offer->save();
}
public function test_each_holder_gets_an_own_acquisition_and_one_grant_per_article(): void
{
$article = $this->publishedArticle('frei', 'Kostenloses Blatt');
$rights = $this->rights();
$offer = app(OfferService::class)->create('Kostenlos');
app(OfferService::class)->activate($offer, [
['target' => $article, 'rights' => $rights],
]);
$acquisitions = [];
foreach (['Mitglied Eins', 'Mitglied Zwei'] as $name) {
$party = Party::query()->create([
'type' => PartyType::Person,
'display_name' => $name,
]);
$acquisitions[] = Acquisition::query()->create([
'party_id' => $party->id,
'type' => AcquisitionType::FreierAbruf,
'reference_type' => 'offer',
'reference_id' => $offer->id,
]);
}
$this->assertCount(2, $acquisitions);
try {
Acquisition::query()->create([
'party_id' => $acquisitions[0]->party_id,
'type' => AcquisitionType::FreierAbruf,
'reference_type' => 'offer',
'reference_id' => $offer->id,
]);
$this->fail('Derselbe Inhaber darf dasselbe Angebot nicht ein zweites Mal abrufen.');
} catch (QueryException) {
}
Grant::query()->create([
'acquisition_id' => $acquisitions[0]->id,
'license_article_id' => $article->id,
'rights_profile_version_id' => $rights->id,
'rights_snapshot' => $rights->capabilities,
'release_at_grant_id' => $article->current_release_id,
'status' => GrantStatus::Aktiv,
]);
$this->expectException(QueryException::class);
Grant::query()->create([
'acquisition_id' => $acquisitions[0]->id,
'license_article_id' => $article->id,
'rights_profile_version_id' => $rights->id,
'rights_snapshot' => $rights->capabilities,
'status' => GrantStatus::Aktiv,
]);
}
private function element(string $title, string $body): ContentElementVersion
{
$content = app(ContentService::class);
return $content->publishElementVersion($content->createElement('text', 'text', $title), $body);
}
private function article(string $code): LicenseArticle
{
return app(ArticleService::class)->create($code);
}
private function publishedArticle(string $code, string $title): LicenseArticle
{
$article = $this->article($code);
app(ArticleService::class)->publishRelease(
$article,
$this->element($title, $title),
'Eigenständiger Artikel.',
);
return $article->fresh();
}
private function rights(): RightsProfileVersion
{
return app(OfferService::class)->createRightsProfile('Lesen', 'ehrenamtlich', [
'text' => ['lesen'],
]);
}
}