Skip to content

Commit f7ca968

Browse files
committed
TASK: refactor API for findNodeAggregates
I forgot to change this after discussing in #4093 so this is the cleanup commit.
1 parent d51cd12 commit f7ca968

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

Neos.ContentGraph.DoctrineDbalAdapter/src/Domain/Repository/ContentGraph.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,28 @@ public function findRootNodeAggregateByType(
121121
ContentStreamId $contentStreamId,
122122
NodeTypeName $nodeTypeName
123123
): NodeAggregate {
124-
return $this->findRootNodeAggregates(
124+
$rootNodeAggregates = $this->findRootNodeAggregates(
125125
$contentStreamId,
126126
FindRootNodeAggregatesFilter::nodeTypeName($nodeTypeName)
127-
)->first();
127+
);
128+
129+
if ($rootNodeAggregates->count() === 0) {
130+
throw new \RuntimeException('Root Node Aggregate not found');
131+
}
132+
133+
if ($rootNodeAggregates->count() > 1) {
134+
$ids = [];
135+
foreach ($rootNodeAggregates as $rootNodeAggregate) {
136+
$ids[] = $rootNodeAggregate->nodeAggregateId->value;
137+
}
138+
throw new \RuntimeException(sprintf(
139+
'More than one root node aggregate of type "%s" found (IDs: %s).',
140+
$nodeTypeName->value,
141+
implode(', ', $ids)
142+
));
143+
}
144+
145+
return $rootNodeAggregates->first();
128146
}
129147

130148
public function findRootNodeAggregates(

Neos.ContentGraph.PostgreSQLAdapter/src/Domain/Repository/ContentHypergraph.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,28 @@ public function findRootNodeAggregateByType(
108108
ContentStreamId $contentStreamId,
109109
NodeTypeName $nodeTypeName
110110
): NodeAggregate {
111-
return $this->findRootNodeAggregates(
111+
$rootNodeAggregates = $this->findRootNodeAggregates(
112112
$contentStreamId,
113113
FindRootNodeAggregatesFilter::nodeTypeName($nodeTypeName)
114-
)->first();
114+
);
115+
116+
if ($rootNodeAggregates->count() === 0) {
117+
throw new \RuntimeException('Root Node Aggregate not found');
118+
}
119+
120+
if ($rootNodeAggregates->count() > 1) {
121+
$ids = [];
122+
foreach ($rootNodeAggregates as $rootNodeAggregate) {
123+
$ids[] = $rootNodeAggregate->nodeAggregateId->value;
124+
}
125+
throw new \RuntimeException(sprintf(
126+
'More than one root node aggregate of type "%s" found (IDs: %s).',
127+
$nodeTypeName->value,
128+
implode(', ', $ids)
129+
));
130+
}
131+
132+
return $rootNodeAggregates->first();
115133
}
116134

117135
public function findRootNodeAggregates(

Neos.ContentRepository.Core/Classes/Projection/ContentGraph/ContentGraphInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ public function getSubgraph(
4646
): ContentSubgraphInterface;
4747

4848
/**
49-
* @deprecated please use {@see findRootNodeAggregates} instead
50-
* @internal
49+
* @api
50+
* Throws exception if no root aggregate found, because a Content Repository needs at least
51+
* one root node to function.
5152
*/
5253
public function findRootNodeAggregateByType(
5354
ContentStreamId $contentStreamId,

Neos.ContentRepository.Core/Classes/Projection/ContentGraph/NodeAggregates.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,14 @@ public function count(): int
7474
}
7575

7676
/**
77-
* Throws exception if no root aggregate found, because a Content Repository needs at least
78-
* one root node to function.
79-
*
80-
* @return NodeAggregate
77+
* @return NodeAggregate|null
8178
*/
82-
public function first(): NodeAggregate
79+
public function first(): ?NodeAggregate
8380
{
8481
if (count($this->nodeAggregates) > 0) {
8582
$array = $this->nodeAggregates;
8683
return reset($array);
8784
}
88-
throw new \RuntimeException('Root Node Aggregate not found');
85+
return null;
8986
}
9087
}

0 commit comments

Comments
 (0)