97 lines
2.9 KiB
PHP
97 lines
2.9 KiB
PHP
<?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',
|
|
'impact_report',
|
|
'root_element_version_id',
|
|
'root_composition_version_id',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'important' => 'boolean',
|
|
'status' => ReleaseStatus::class,
|
|
'impact_report' => 'array',
|
|
];
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::updating(function (ArticleRelease $release): void {
|
|
$original = StatusValue::of($release->getOriginal('status'));
|
|
$next = StatusValue::of($release->status);
|
|
$locked = [
|
|
ReleaseStatus::Veroeffentlicht->value,
|
|
ReleaseStatus::AbgeloestVerfuegbar->value,
|
|
ReleaseStatus::Gesperrt->value,
|
|
];
|
|
|
|
if (! in_array($original, $locked, true)) {
|
|
return;
|
|
}
|
|
|
|
$allowedFields = ['status', 'updated_at'];
|
|
|
|
if ($original === ReleaseStatus::Veroeffentlicht->value && $next === ReleaseStatus::Gesperrt->value) {
|
|
$allowedFields = ['status', 'updated_at', 'block_reason', 'fallback_release_id'];
|
|
}
|
|
|
|
if (array_diff(array_keys($release->getDirty()), $allowedFields) !== []) {
|
|
throw new DomainRuleException('Ein veröffentlichter Artikel-Stand ist unveränderlich.');
|
|
}
|
|
|
|
$allowed = match ($original) {
|
|
ReleaseStatus::Veroeffentlicht->value => [
|
|
ReleaseStatus::AbgeloestVerfuegbar->value,
|
|
ReleaseStatus::Gesperrt->value,
|
|
],
|
|
ReleaseStatus::AbgeloestVerfuegbar->value => [
|
|
ReleaseStatus::Veroeffentlicht->value,
|
|
],
|
|
default => [],
|
|
};
|
|
|
|
if (! in_array($next, $allowed, true)) {
|
|
throw new DomainRuleException('Dieser Statuswechsel eines Artikel-Stands ist nicht zulässig.');
|
|
}
|
|
});
|
|
}
|
|
|
|
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');
|
|
}
|
|
}
|