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} */ 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 */ 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, 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, ]; } }