Skip to content

Commit

Permalink
Merge pull request #5158 from mhsdesign/task/streamLineContentGraphFi…
Browse files Browse the repository at this point in the history
…ndRootNodeAggregateByType

!!! TASK: Streamline `ContentGraph::findRootNodeAggregateByType`
  • Loading branch information
nezaniel authored Jul 29, 2024
2 parents b072eb6 + 2b411f9 commit a023bea
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,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 @@ -119,17 +118,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 @@ -144,12 +141,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 @@ -31,7 +31,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 @@ -96,7 +95,7 @@ public function getSubgraph(

public function findRootNodeAggregateByType(
NodeTypeName $nodeTypeName
): NodeAggregate {
): ?NodeAggregate {
$rootNodeAggregates = $this->findRootNodeAggregates(
FindRootNodeAggregatesFilter::create(nodeTypeName: $nodeTypeName)
);
Expand All @@ -113,13 +112,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 @@ -22,7 +22,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 @@ -59,14 +58,11 @@ public function getSubgraph(
): ContentSubgraphInterface;

/**
* Throws exception if no root aggregate of the given type found.
*
* @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 !== null) {
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 @@ -27,7 +27,6 @@
use Neos\ContentRepository\Core\Projection\ContentGraph\VisibilityConstraints;
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 @@ -222,13 +221,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 === null) {
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 a023bea

Please sign in to comment.