Skip to content

Commit

Permalink
TASK: Streamline ContentGraph::findRootNodeAggregateByType
Browse files Browse the repository at this point in the history
to be nullable like findRootNodeByType in the subgraph
  • Loading branch information
mhsdesign committed Jun 22, 2024
1 parent 84b5b60 commit 1294040
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
use Neos\ContentRepository\Core\Projection\ContentGraph\NodeAggregates;
use Neos\ContentRepository\Core\Projection\ContentGraph\VisibilityConstraints;
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\ContentRepository\Core\SharedModel\Exception\RootNodeAggregateDoesNotExist;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateClassification;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Node\NodeName;
Expand Down Expand Up @@ -117,17 +116,15 @@ public function getSubgraph(
return $this->subgraphs[$index];
}

/**
* @throws RootNodeAggregateDoesNotExist
*/
public function findRootNodeAggregateByType(
NodeTypeName $nodeTypeName
): NodeAggregate {
): ?NodeAggregate {
$rootNodeAggregates = $this->findRootNodeAggregates(
FindRootNodeAggregatesFilter::create(nodeTypeName: $nodeTypeName)
);

if ($rootNodeAggregates->count() > 1) {
// todo drop this check as this is enforced by the write side? https://github.com/neos/neos-development-collection/pull/4339
$ids = [];
foreach ($rootNodeAggregates as $rootNodeAggregate) {
$ids[] = $rootNodeAggregate->nodeAggregateId->value;
Expand All @@ -139,12 +136,7 @@ public function findRootNodeAggregateByType(
));
}

$rootNodeAggregate = $rootNodeAggregates->first();
if ($rootNodeAggregate === null) {
throw RootNodeAggregateDoesNotExist::butWasExpectedTo($nodeTypeName);
}

return $rootNodeAggregate;
return $rootNodeAggregates->first();
}

public function findRootNodeAggregates(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use Neos\ContentRepository\Core\Projection\ContentGraph\NodeAggregates;
use Neos\ContentRepository\Core\Projection\ContentGraph\VisibilityConstraints;
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\ContentRepository\Core\SharedModel\Exception\RootNodeAggregateDoesNotExist;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Node\NodeName;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
Expand Down Expand Up @@ -95,7 +94,7 @@ public function getSubgraph(

public function findRootNodeAggregateByType(
NodeTypeName $nodeTypeName
): NodeAggregate {
): ?NodeAggregate {
$rootNodeAggregates = $this->findRootNodeAggregates(
FindRootNodeAggregatesFilter::create(nodeTypeName: $nodeTypeName)
);
Expand All @@ -112,13 +111,7 @@ public function findRootNodeAggregateByType(
));
}

$rootNodeAggregate = $rootNodeAggregates->first();

if ($rootNodeAggregate === null) {
throw RootNodeAggregateDoesNotExist::butWasExpectedTo($nodeTypeName);
}

return $rootNodeAggregate;
return $rootNodeAggregates->first();
}

public function findRootNodeAggregates(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
use Neos\ContentRepository\Core\SharedModel\Exception\NodeTypeNotFound;
use Neos\ContentRepository\Core\SharedModel\Exception\PropertyCannotBeSet;
use Neos\ContentRepository\Core\SharedModel\Exception\ReferenceCannotBeSet;
use Neos\ContentRepository\Core\SharedModel\Exception\RootNodeAggregateDoesNotExist;
use Neos\ContentRepository\Core\SharedModel\Exception\RootNodeAggregateTypeIsAlreadyOccupied;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Node\NodeName;
Expand Down Expand Up @@ -160,12 +159,9 @@ protected function requireRootNodeTypeToBeUnoccupied(
ContentGraphInterface $contentGraph,
NodeTypeName $nodeTypeName
): void {
try {
$contentGraph->findRootNodeAggregateByType($nodeTypeName);
} catch (RootNodeAggregateDoesNotExist $_) {
if ($contentGraph->findRootNodeAggregateByType($nodeTypeName) === null) {
return;
}

throw RootNodeAggregateTypeIsAlreadyOccupied::butWasExpectedNotTo($nodeTypeName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Neos\ContentRepository\Core\Projection\ProjectionStateInterface;
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\ContentRepository\Core\SharedModel\Exception\NodeAggregatesTypeIsAmbiguous;
use Neos\ContentRepository\Core\SharedModel\Exception\RootNodeAggregateDoesNotExist;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Node\NodeName;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
Expand Down Expand Up @@ -58,18 +57,11 @@ public function getSubgraph(
): ContentSubgraphInterface;

/**
* Throws exception if no root aggregate found, because a Content Repository needs at least
* one root node to function.
*
* Also throws exceptions if multiple root node aggregates of the given $nodeTypeName were found,
* as this would lead to nondeterministic results in your code.
*
* @throws RootNodeAggregateDoesNotExist
* @api
*/
public function findRootNodeAggregateByType(
NodeTypeName $nodeTypeName
): NodeAggregate;
): ?NodeAggregate;

/**
* @api
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,25 @@ public function getOrCreateLiveWorkspace(): Workspace
}

/**
* Retrieve the root Node Aggregate ID for the specified $contentStreamId
* Retrieve the root Node Aggregate ID for the specified $workspace
* If no root node of the specified $rootNodeTypeName exist, it will be created
*/
public function getOrCreateRootNodeAggregate(
Workspace $workspace,
NodeTypeName $rootNodeTypeName
): NodeAggregateId {
try {
return $this->contentRepository->getContentGraph($workspace->workspaceName)->findRootNodeAggregateByType(
$rootNodeTypeName
)->nodeAggregateId;

// TODO make this case more explicit
} catch (\Exception $exception) {
$rootNodeAggregateId = NodeAggregateId::create();
$this->contentRepository->handle(CreateRootNodeAggregateWithNode::create(
$workspace->workspaceName,
$rootNodeAggregateId,
$rootNodeTypeName,
));
return $rootNodeAggregateId;
$rootNodeAggregate = $this->contentRepository->getContentGraph($workspace->workspaceName)->findRootNodeAggregateByType(
$rootNodeTypeName
);
if ($rootNodeAggregate) {
return $rootNodeAggregate->nodeAggregateId;
}
$rootNodeAggregateId = NodeAggregateId::create();
$this->contentRepository->handle(CreateRootNodeAggregateWithNode::create(
$workspace->workspaceName,
$rootNodeAggregateId,
$rootNodeTypeName,
));
return $rootNodeAggregateId;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use Neos\ContentRepository\Core\Projection\Workspace\Workspace;
use Neos\ContentRepository\Core\Service\ContentStreamPruner;
use Neos\ContentRepository\Core\Service\ContentStreamPrunerFactory;
use Neos\ContentRepository\Core\SharedModel\Exception\RootNodeAggregateDoesNotExist;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamState;
Expand Down Expand Up @@ -241,13 +240,9 @@ protected function getRootNodeAggregateId(): ?NodeAggregateId
return $this->currentRootNodeAggregateId;
}

try {
return $this->currentContentRepository->getContentGraph($this->currentWorkspaceName)->findRootNodeAggregateByType(
NodeTypeName::fromString('Neos.Neos:Sites')
)->nodeAggregateId;
} catch (RootNodeAggregateDoesNotExist) {
return null;
}
return $this->currentContentRepository->getContentGraph($this->currentWorkspaceName)->findRootNodeAggregateByType(
NodeTypeName::fromString('Neos.Neos:Sites')
)->nodeAggregateId;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,10 @@ public function updateSiteAction(Site $site, $newSiteNodeName)
);
}

try {
$sitesNode = $contentRepository->getContentGraph($liveWorkspace->workspaceName)->findRootNodeAggregateByType(
NodeTypeNameFactory::forSites()
);
} catch (\Exception $exception) {
$sitesNode = $contentRepository->getContentGraph($liveWorkspace->workspaceName)->findRootNodeAggregateByType(
NodeTypeNameFactory::forSites()
);
if (!$sitesNode) {
throw new \InvalidArgumentException(
'Cannot update a site without the sites note being present.',
1651958452
Expand Down
4 changes: 4 additions & 0 deletions Neos.Neos/Classes/Domain/Service/SiteNodeUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public function findSiteNodeBySite(
$rootNodeAggregate = $contentGraph->findRootNodeAggregateByType(
NodeTypeNameFactory::forSites()
);
if (!$rootNodeAggregate) {
throw new \RuntimeException(sprintf('No sites root node found in content repository "%s", while fetching site node "%s"', $contentRepository->id->value, $site->getNodeName()), 1719046570);
}

$rootNode = $rootNodeAggregate->getNodeByCoveredDimensionSpacePoint($dimensionSpacePoint);

$siteNode = $subgraph->findNodeByPath(
Expand Down
4 changes: 4 additions & 0 deletions Neos.Neos/Classes/Domain/Service/SiteServiceInternals.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public function removeSiteNode(SiteNodeName $siteNodeName): void
$sitesNodeAggregate = $contentGraph->findRootNodeAggregateByType(
NodeTypeNameFactory::forSites()
);
if (!$sitesNodeAggregate) {
// nothing to prune, we could probably also return here directly?
continue;
}
$siteNodeAggregate = $contentGraph->findChildNodeAggregateByName(
$sitesNodeAggregate->nodeAggregateId,
$siteNodeName->toNodeName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\PropertyValue\Criteria\PropertyValueLessThanOrEqual;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepository\Core\Projection\ContentGraph\VisibilityConstraints;
use Neos\ContentRepository\Core\Projection\Workspace\Workspace;
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\ContentRepository\Core\SharedModel\Exception\RootNodeAggregateDoesNotExist;
use Neos\ContentRepository\Core\SharedModel\Exception\WorkspaceDoesNotExist;
use Neos\ContentRepository\Core\SharedModel\Node\NodeVariantSelectionStrategy;
use Neos\ContentRepository\Core\SharedModel\Node\PropertyName;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
Expand Down Expand Up @@ -101,7 +98,7 @@ private function getNodesWithExceededDates(ContentRepository $contentRepository,
$sitesNodeTypeName = NodeTypeName::fromString('Neos.Neos:Sites');
$rootNode = $subgraph->findRootNodeByType($sitesNodeTypeName);
if ($rootNode === null) {
throw RootNodeAggregateDoesNotExist::butWasExpectedTo($sitesNodeTypeName);
throw new \RuntimeException(sprintf('No sites root node found in content repository "%s"', $contentRepository->id->value), 1719047148);
}

$nodes = $subgraph->findDescendantNodes(
Expand Down

0 comments on commit 1294040

Please sign in to comment.