policies->insert( new Policy( $title, $slug, acceptanceScope: $scope ) ); } /** * Add a new draft version to a policy, numbered after the latest existing one. */ public function addDraftVersion( int $policyId, ?string $body ): int { $next = $this->versions->maxVersionNumber( $policyId ) + 1; return $this->versions->insert( new PolicyVersion( policyId: $policyId, versionNumber: $next, body: $body, status: PolicyVersion::STATUS_DRAFT, ) ); } /** * Publish a version: archive the policy's previously current version, mark * the new one published, and point the policy at it. Returns false if the * version does not belong to the policy. */ public function publishVersion( int $policyId, int $versionId ): bool { $policy = $this->policies->findById( $policyId ); $version = $this->versions->findById( $versionId ); if ( null === $policy || null === $version || $version->policyId !== $policyId ) { return false; } if ( null !== $policy->currentVersionId && $policy->currentVersionId !== $versionId ) { $this->versions->updateStatus( $policy->currentVersionId, PolicyVersion::STATUS_ARCHIVED ); } $this->versions->updateStatus( $versionId, PolicyVersion::STATUS_PUBLISHED, current_time( 'mysql' ) ); $this->policies->updateCurrentVersion( $policyId, $versionId ); return true; } }