Skip to content

Commit

Permalink
4708 - Add ExpectedVersion checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Schmitt committed Mar 13, 2024
1 parent 35822a9 commit bf84fa9
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use Neos\ContentRepository\Core\NodeType\NodeTypeName;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
use Neos\EventStore\Model\EventStream\ExpectedVersion;

/**
* @internal implementation details of command handlers
Expand Down Expand Up @@ -676,4 +677,15 @@ protected function validateReferenceProperties(
}
}
}

protected function getExpectedVersionOfContentStream(
ContentStreamId $contentStreamId,
ContentRepository $contentRepository
): ExpectedVersion {
return ExpectedVersion::fromVersion(
$contentRepository->getContentStreamFinder()
->findVersionForContentStream($contentStreamId)
->unwrap()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Neos\ContentRepository\Core\ContentRepository;
use Neos\ContentRepository\Core\EventStore\Events;
use Neos\ContentRepository\Core\EventStore\EventsToPublish;
use Neos\ContentRepository\Core\Feature\Common\ConstraintChecks;
use Neos\ContentRepository\Core\Feature\ContentStreamCreation\Command\CreateContentStream;
use Neos\ContentRepository\Core\Feature\ContentStreamCreation\Event\ContentStreamWasCreated;
use Neos\ContentRepository\Core\Feature\ContentStreamForking\Command\CloseContentStream;
Expand Down Expand Up @@ -84,7 +85,7 @@ private function handleCloseContentStream(
ContentRepository $contentRepository
): EventsToPublish {
$this->requireContentStreamToExist($command->contentStreamId, $contentRepository);

$expectedVersion = $this->getExpectedVersionOfContentStream($command->contentStreamId, $contentRepository);
$streamName = ContentStreamEventStreamName::fromContentStreamId($command->contentStreamId)->getEventStreamName();

return new EventsToPublish(
Expand All @@ -94,7 +95,7 @@ private function handleCloseContentStream(
$command->contentStreamId,
),
),
ExpectedVersion::ANY()
$expectedVersion
);
}

Expand Down Expand Up @@ -135,6 +136,7 @@ private function handleRemoveContentStream(
ContentRepository $contentRepository
): EventsToPublish {
$this->requireContentStreamToExist($command->contentStreamId, $contentRepository);
$expectedVersion = $this->getExpectedVersionOfContentStream($command->contentStreamId, $contentRepository);

$streamName = ContentStreamEventStreamName::fromContentStreamId(
$command->contentStreamId
Expand All @@ -147,7 +149,7 @@ private function handleRemoveContentStream(
$command->contentStreamId,
),
),
ExpectedVersion::ANY()
$expectedVersion
);
}

Expand Down Expand Up @@ -194,4 +196,15 @@ protected function requireContentStreamToNotBeClosed(
);
}
}

protected function getExpectedVersionOfContentStream(
ContentStreamId $contentStreamId,
ContentRepository $contentRepository
): ExpectedVersion {
return ExpectedVersion::fromVersion(
$contentRepository->getContentStreamFinder()
->findVersionForContentStream($contentStreamId)
->unwrap()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ private function handleCreateNodeAggregateWithNodeAndSerializedProperties(
ContentRepository $contentRepository
): EventsToPublish {
$contentStreamId = $this->requireContentStream($command->workspaceName, $contentRepository);
$expectedVersion = $this->getExpectedVersionOfContentStream($contentStreamId, $contentRepository);
$this->requireDimensionSpacePointToExist($command->originDimensionSpacePoint->toDimensionSpacePoint());
$nodeType = $this->requireNodeType($command->nodeTypeName);
$this->requireNodeTypeToNotBeAbstract($nodeType);
Expand Down Expand Up @@ -227,7 +228,7 @@ private function handleCreateNodeAggregateWithNodeAndSerializedProperties(
ContentStreamEventStreamName::fromContentStreamId($contentStreamId)
->getEventStreamName(),
NodeAggregateEventPublisher::enrichWithCommand($command, Events::fromArray($events)),
ExpectedVersion::ANY()
$expectedVersion
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ private function handleDisableNodeAggregate(
ContentRepository $contentRepository
): EventsToPublish {
$contentStreamId = $this->requireContentStream($command->workspaceName, $contentRepository);
$expectedVersion = $this->getExpectedVersionOfContentStream($contentStreamId, $contentRepository);
$this->requireDimensionSpacePointToExist($command->coveredDimensionSpacePoint);
$nodeAggregate = $this->requireProjectedNodeAggregate(
$contentStreamId,
Expand Down Expand Up @@ -87,7 +88,7 @@ private function handleDisableNodeAggregate(
$command,
$events
),
ExpectedVersion::ANY()
$expectedVersion
);
}

Expand All @@ -103,6 +104,7 @@ public function handleEnableNodeAggregate(
ContentRepository $contentRepository
): EventsToPublish {
$contentStreamId = $this->requireContentStream($command->workspaceName, $contentRepository);
$expectedVersion = $this->getExpectedVersionOfContentStream($contentStreamId, $contentRepository);
$this->requireDimensionSpacePointToExist($command->coveredDimensionSpacePoint);
$nodeAggregate = $this->requireProjectedNodeAggregate(
$contentStreamId,
Expand Down Expand Up @@ -137,7 +139,7 @@ public function handleEnableNodeAggregate(
return new EventsToPublish(
ContentStreamEventStreamName::fromContentStreamId($contentStreamId)->getEventStreamName(),
NodeAggregateEventPublisher::enrichWithCommand($command, $events),
ExpectedVersion::ANY()
$expectedVersion
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private function handleCopyNodesRecursively(
): EventsToPublish {
// Basic constraints (Content Stream / Dimension Space Point / Node Type of to-be-inserted root node)
$contentStreamId = $this->requireContentStream($command->workspaceName, $contentRepository);
$expectedVersion = $this->getExpectedVersionOfContentStream($contentStreamId, $contentRepository);
$this->requireDimensionSpacePointToExist(
$command->targetDimensionSpacePoint->toDimensionSpacePoint()
);
Expand Down Expand Up @@ -168,7 +169,7 @@ private function handleCopyNodesRecursively(
$command,
Events::fromArray($events)
),
ExpectedVersion::ANY()
$expectedVersion
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private function handleSetSerializedNodeProperties(
ContentRepository $contentRepository
): EventsToPublish {
$contentStreamId = $this->requireContentStream($command->workspaceName, $contentRepository);
$contentStreamVersion = $contentRepository->getContentStreamFinder()->findVersionForContentStream($contentStreamId);
$expectedVersion = $this->getExpectedVersionOfContentStream($contentStreamId, $contentRepository);
// Check if node exists
$nodeAggregate = $this->requireProjectedNodeAggregate(
$contentStreamId,
Expand Down Expand Up @@ -137,7 +137,7 @@ private function handleSetSerializedNodeProperties(
$command,
Events::fromArray($events)
),
ExpectedVersion::fromVersion($contentStreamVersion->unwrap())
$expectedVersion
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ private function handleMoveNodeAggregate(
ContentRepository $contentRepository
): EventsToPublish {
$contentStreamId = $this->requireContentStream($command->workspaceName, $contentRepository);
$expectedVersion = $this->getExpectedVersionOfContentStream($contentStreamId, $contentRepository);
$this->requireDimensionSpacePointToExist($command->dimensionSpacePoint);
$nodeAggregate = $this->requireProjectedNodeAggregate(
$contentStreamId,
Expand Down Expand Up @@ -179,7 +180,7 @@ private function handleMoveNodeAggregate(
$command,
$events
),
ExpectedVersion::ANY()
$expectedVersion
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ private function handleSetSerializedNodeReferences(
ContentRepository $contentRepository
): EventsToPublish {
$contentStreamId = $this->requireContentStream($command->workspaceName, $contentRepository);
$expectedVersion = $this->getExpectedVersionOfContentStream($contentStreamId, $contentRepository);
$this->requireDimensionSpacePointToExist(
$command->sourceOriginDimensionSpacePoint->toDimensionSpacePoint()
);
Expand Down Expand Up @@ -162,7 +163,7 @@ private function handleSetSerializedNodeReferences(
$command,
$events
),
ExpectedVersion::ANY()
$expectedVersion
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private function handleRemoveNodeAggregate(
ContentRepository $contentRepository
): EventsToPublish {
$contentStreamId = $this->requireContentStream($command->workspaceName, $contentRepository);
$expectedVersion = $this->getExpectedVersionOfContentStream($contentStreamId, $contentRepository);
$nodeAggregate = $this->requireProjectedNodeAggregate(
$contentStreamId,
$command->nodeAggregateId,
Expand Down Expand Up @@ -95,7 +96,7 @@ private function handleRemoveNodeAggregate(
$command,
$events
),
ExpectedVersion::ANY()
$expectedVersion
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ trait NodeRenaming
private function handleChangeNodeAggregateName(ChangeNodeAggregateName $command, ContentRepository $contentRepository): EventsToPublish
{
$contentStreamId = $this->requireContentStream($command->workspaceName, $contentRepository);
$expectedVersion = $this->getExpectedVersionOfContentStream($contentStreamId, $contentRepository);
$nodeAggregate = $this->requireProjectedNodeAggregate(
$contentStreamId,
$command->nodeAggregateId,
Expand Down Expand Up @@ -68,7 +69,7 @@ private function handleChangeNodeAggregateName(ChangeNodeAggregateName $command,
$command,
$events
),
ExpectedVersion::ANY()
$expectedVersion
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ private function handleChangeNodeAggregateType(
**************/
// existence of content stream, node type and node aggregate
$contentStreamId = $this->requireContentStream($command->workspaceName, $contentRepository);
$expectedVersion = $this->getExpectedVersionOfContentStream($contentStreamId, $contentRepository);
$newNodeType = $this->requireNodeType($command->newNodeTypeName);
$nodeAggregate = $this->requireProjectedNodeAggregate(
$contentStreamId,
Expand Down Expand Up @@ -223,7 +224,7 @@ private function handleChangeNodeAggregateType(
$command,
Events::fromArray($events),
),
ExpectedVersion::ANY()
$expectedVersion
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ private function handleCreateNodeVariant(
ContentRepository $contentRepository
): EventsToPublish {
$contentStreamId = $this->requireContentStream($command->workspaceName, $contentRepository);
$expectedVersion = $this->getExpectedVersionOfContentStream($contentStreamId, $contentRepository);
$nodeAggregate = $this->requireProjectedNodeAggregate(
$contentStreamId,
$command->nodeAggregateId,
Expand Down Expand Up @@ -90,7 +91,7 @@ private function handleCreateNodeVariant(
$command,
$events
),
ExpectedVersion::ANY()
$expectedVersion
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ private function handleCreateRootNodeAggregateWithNode(
ContentRepository $contentRepository
): EventsToPublish {
$contentStreamId = $this->requireContentStream($command->workspaceName, $contentRepository);
$expectedVersion = $this->getExpectedVersionOfContentStream($contentStreamId, $contentRepository);
$this->requireProjectedNodeAggregateToNotExist(
$contentStreamId,
$command->nodeAggregateId,
Expand Down Expand Up @@ -121,7 +122,7 @@ private function handleCreateRootNodeAggregateWithNode(
$command,
Events::fromArray($events)
),
ExpectedVersion::ANY()
$expectedVersion
);
}

Expand All @@ -148,6 +149,7 @@ private function handleUpdateRootNodeAggregateDimensions(
ContentRepository $contentRepository
): EventsToPublish {
$contentStreamId = $this->requireContentStream($command->workspaceName, $contentRepository);
$expectedVersion = $this->getExpectedVersionOfContentStream($contentStreamId, $contentRepository);
$nodeAggregate = $this->requireProjectedNodeAggregate(
$contentStreamId,
$command->nodeAggregateId,
Expand All @@ -174,7 +176,7 @@ private function handleUpdateRootNodeAggregateDimensions(
$command,
$events
),
ExpectedVersion::ANY()
$expectedVersion
);
}

Expand Down

0 comments on commit bf84fa9

Please sign in to comment.