Skip to content

Commit

Permalink
Ensure references are fresh
Browse files Browse the repository at this point in the history
  • Loading branch information
larowlan authored and kimpepper committed Aug 16, 2017
1 parent f129937 commit d471ba1
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/Storage/DbalNestedSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function addRootNode(NodeKey $nodeKey) {
* {@inheritdoc}
*/
public function addNodeBelow(Node $target, NodeKey $nodeKey) {
$target = $this->ensureNodeIsFresh($target);
$newLeftPosition = $target->getRight();
$depth = $target->getDepth() + 1;
return $this->insertNodeAtPostion($newLeftPosition, $depth, $nodeKey);
Expand All @@ -33,6 +34,7 @@ public function addNodeBelow(Node $target, NodeKey $nodeKey) {
* {@inheritdoc}
*/
public function addNodeBefore(Node $target, NodeKey $nodeKey) {
$target = $this->ensureNodeIsFresh($target);
$newLeftPosition = $target->getLeft();
$depth = $target->getDepth();
return $this->insertNodeAtPostion($newLeftPosition, $depth, $nodeKey);
Expand All @@ -42,6 +44,7 @@ public function addNodeBefore(Node $target, NodeKey $nodeKey) {
* {@inheritdoc}
*/
public function addNodeAfter(Node $target, NodeKey $nodeKey) {
$target = $this->ensureNodeIsFresh($target);
$newLeftPosition = $target->getRight() + 1;
$depth = $target->getDepth();
return $this->insertNodeAtPostion($newLeftPosition, $depth, $nodeKey);
Expand Down Expand Up @@ -223,6 +226,7 @@ public function getTree() {
* {@inheritdoc}
*/
public function deleteNode(Node $node) {
$node = $this->ensureNodeIsFresh($node);
if ($node->getLeft() < 1 || $node->getRight() < 1) {
throw new \InvalidArgumentException("Left and right values must be > 0");
}
Expand Down Expand Up @@ -268,6 +272,7 @@ public function deleteNode(Node $node) {
* {@inheritdoc}
*/
public function deleteSubTree(Node $node) {
$node = $this->ensureNodeIsFresh($node);
$left = $node->getLeft();
$right = $node->getRight();
$width = $right - $left + 1;
Expand Down Expand Up @@ -312,6 +317,7 @@ public function moveSubTreeToRoot(Node $node) {
* {@inheritdoc}
*/
public function moveSubTreeBelow(Node $target, Node $node) {
$target = $this->ensureNodeIsFresh($target);
$newLeftPosition = $target->getLeft() + 1;
$this->moveSubTreeToPosition($newLeftPosition, $node, $target->getDepth() + 1);
}
Expand All @@ -320,6 +326,7 @@ public function moveSubTreeBelow(Node $target, Node $node) {
* {@inheritdoc}
*/
public function moveSubTreeBefore(Node $target, Node $node) {
$target = $this->ensureNodeIsFresh($target);
$newLeftPosition = $target->getLeft();
$this->moveSubTreeToPosition($newLeftPosition, $node, $target->getDepth());
}
Expand All @@ -328,6 +335,7 @@ public function moveSubTreeBefore(Node $target, Node $node) {
* {@inheritdoc}
*/
public function moveSubTreeAfter(Node $target, Node $node) {
$target = $this->ensureNodeIsFresh($target);
$newLeftPosition = $target->getRight() + 1;
$this->moveSubTreeToPosition($newLeftPosition, $node, $target->getDepth());
}
Expand All @@ -337,6 +345,7 @@ public function moveSubTreeAfter(Node $target, Node $node) {
*/
public function adoptChildren(Node $oldParent, Node $newParent) {
$children = $this->findChildren($oldParent->getNodeKey());
$newParent = $this->ensureNodeIsFresh($newParent);
$newLeftPosition = $newParent->getRight();
$this->moveMultipleSubTreesToPosition($newLeftPosition, $children, $newParent->getDepth() + 1);
}
Expand Down Expand Up @@ -374,8 +383,8 @@ protected function moveSubTreeToPosition($newLeftPosition, Node $node, $newDepth
protected function moveMultipleSubTreesToPosition($newLeftPosition, array $nodes, $newDepth) {
try {

$firstNode = reset($nodes);
$lastNode = end($nodes);
$firstNode = $this->ensureNodeIsFresh(reset($nodes));
$lastNode = $this->ensureNodeIsFresh(end($nodes));
// Calculate position adjustment variables.
$width = $lastNode->getRight() - $firstNode->getLeft() + 1;
$distance = $newLeftPosition - $firstNode->getLeft();
Expand Down Expand Up @@ -452,4 +461,17 @@ protected function findMaxRightPosition() {
return $maxRight;
}

/**
* Ensures that any node use in calculations of space is fresh.
*
* @param \PNX\NestedSet\Node $node
* Node to load.
*
* @return \PNX\NestedSet\Node
* Fresh node.
*/
protected function ensureNodeIsFresh(Node $node) {
return $this->getNode($node->getNodeKey());
}

}

0 comments on commit d471ba1

Please sign in to comment.