Skip to content

Commit d471ba1

Browse files
larowlankimpepper
authored andcommitted
Ensure references are fresh
1 parent f129937 commit d471ba1

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/Storage/DbalNestedSet.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function addRootNode(NodeKey $nodeKey) {
2424
* {@inheritdoc}
2525
*/
2626
public function addNodeBelow(Node $target, NodeKey $nodeKey) {
27+
$target = $this->ensureNodeIsFresh($target);
2728
$newLeftPosition = $target->getRight();
2829
$depth = $target->getDepth() + 1;
2930
return $this->insertNodeAtPostion($newLeftPosition, $depth, $nodeKey);
@@ -33,6 +34,7 @@ public function addNodeBelow(Node $target, NodeKey $nodeKey) {
3334
* {@inheritdoc}
3435
*/
3536
public function addNodeBefore(Node $target, NodeKey $nodeKey) {
37+
$target = $this->ensureNodeIsFresh($target);
3638
$newLeftPosition = $target->getLeft();
3739
$depth = $target->getDepth();
3840
return $this->insertNodeAtPostion($newLeftPosition, $depth, $nodeKey);
@@ -42,6 +44,7 @@ public function addNodeBefore(Node $target, NodeKey $nodeKey) {
4244
* {@inheritdoc}
4345
*/
4446
public function addNodeAfter(Node $target, NodeKey $nodeKey) {
47+
$target = $this->ensureNodeIsFresh($target);
4548
$newLeftPosition = $target->getRight() + 1;
4649
$depth = $target->getDepth();
4750
return $this->insertNodeAtPostion($newLeftPosition, $depth, $nodeKey);
@@ -223,6 +226,7 @@ public function getTree() {
223226
* {@inheritdoc}
224227
*/
225228
public function deleteNode(Node $node) {
229+
$node = $this->ensureNodeIsFresh($node);
226230
if ($node->getLeft() < 1 || $node->getRight() < 1) {
227231
throw new \InvalidArgumentException("Left and right values must be > 0");
228232
}
@@ -268,6 +272,7 @@ public function deleteNode(Node $node) {
268272
* {@inheritdoc}
269273
*/
270274
public function deleteSubTree(Node $node) {
275+
$node = $this->ensureNodeIsFresh($node);
271276
$left = $node->getLeft();
272277
$right = $node->getRight();
273278
$width = $right - $left + 1;
@@ -312,6 +317,7 @@ public function moveSubTreeToRoot(Node $node) {
312317
* {@inheritdoc}
313318
*/
314319
public function moveSubTreeBelow(Node $target, Node $node) {
320+
$target = $this->ensureNodeIsFresh($target);
315321
$newLeftPosition = $target->getLeft() + 1;
316322
$this->moveSubTreeToPosition($newLeftPosition, $node, $target->getDepth() + 1);
317323
}
@@ -320,6 +326,7 @@ public function moveSubTreeBelow(Node $target, Node $node) {
320326
* {@inheritdoc}
321327
*/
322328
public function moveSubTreeBefore(Node $target, Node $node) {
329+
$target = $this->ensureNodeIsFresh($target);
323330
$newLeftPosition = $target->getLeft();
324331
$this->moveSubTreeToPosition($newLeftPosition, $node, $target->getDepth());
325332
}
@@ -328,6 +335,7 @@ public function moveSubTreeBefore(Node $target, Node $node) {
328335
* {@inheritdoc}
329336
*/
330337
public function moveSubTreeAfter(Node $target, Node $node) {
338+
$target = $this->ensureNodeIsFresh($target);
331339
$newLeftPosition = $target->getRight() + 1;
332340
$this->moveSubTreeToPosition($newLeftPosition, $node, $target->getDepth());
333341
}
@@ -337,6 +345,7 @@ public function moveSubTreeAfter(Node $target, Node $node) {
337345
*/
338346
public function adoptChildren(Node $oldParent, Node $newParent) {
339347
$children = $this->findChildren($oldParent->getNodeKey());
348+
$newParent = $this->ensureNodeIsFresh($newParent);
340349
$newLeftPosition = $newParent->getRight();
341350
$this->moveMultipleSubTreesToPosition($newLeftPosition, $children, $newParent->getDepth() + 1);
342351
}
@@ -374,8 +383,8 @@ protected function moveSubTreeToPosition($newLeftPosition, Node $node, $newDepth
374383
protected function moveMultipleSubTreesToPosition($newLeftPosition, array $nodes, $newDepth) {
375384
try {
376385

377-
$firstNode = reset($nodes);
378-
$lastNode = end($nodes);
386+
$firstNode = $this->ensureNodeIsFresh(reset($nodes));
387+
$lastNode = $this->ensureNodeIsFresh(end($nodes));
379388
// Calculate position adjustment variables.
380389
$width = $lastNode->getRight() - $firstNode->getLeft() + 1;
381390
$distance = $newLeftPosition - $firstNode->getLeft();
@@ -452,4 +461,17 @@ protected function findMaxRightPosition() {
452461
return $maxRight;
453462
}
454463

464+
/**
465+
* Ensures that any node use in calculations of space is fresh.
466+
*
467+
* @param \PNX\NestedSet\Node $node
468+
* Node to load.
469+
*
470+
* @return \PNX\NestedSet\Node
471+
* Fresh node.
472+
*/
473+
protected function ensureNodeIsFresh(Node $node) {
474+
return $this->getNode($node->getNodeKey());
475+
}
476+
455477
}

0 commit comments

Comments
 (0)