Skip to content

Commit 67e8516

Browse files
committed
Refactored addNode to insertNodeBelow
1 parent e06cc5f commit 67e8516

File tree

3 files changed

+38
-75
lines changed

3 files changed

+38
-75
lines changed

src/NestedSetInterface.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
interface NestedSetInterface {
99

1010
/**
11-
* Adds a child to the parent.
11+
* Inserts a node below the target node.
1212
*
13-
* @param \PNX\NestedSet\Node $parent
14-
* The parent.
15-
* @param \PNX\NestedSet\Node $child
16-
* The child.
13+
* @param \PNX\NestedSet\Node $target
14+
* The target node to insert below.
15+
* @param \PNX\NestedSet\Node $node
16+
* The node to insert. Only id and revision ID are required.
1717
*
1818
* @return \PNX\NestedSet\Node
19-
* Returns a new child node with left and right.
19+
* Returns a new child with position values set.
2020
*/
21-
public function addNode(Node $parent, Node $child);
21+
public function insertNodeBelow(Node $target, Node $node);
2222

2323
/**
2424
* Deletes a node and moves descendants up a level.

src/Storage/DbalNestedSet.php

Lines changed: 29 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,47 @@ public function __construct(Connection $connection) {
3232
/**
3333
* {@inheritdoc}
3434
*/
35-
public function addNode(Node $parent, Node $child) {
35+
public function insertNodeBelow(Node $target, Node $node) {
36+
$newLeftPosition = $target->getRight() - 1;
37+
$depth = $target->getDepth() + 1;
38+
return $this->insertNodeAtPostion($newLeftPosition, $depth, $node);
39+
}
40+
41+
/**
42+
* Inserts a node to the target position.
43+
*
44+
* @param int $newLeftPosition
45+
* The new left position.
46+
* @param int $depth
47+
* The new depth.
48+
* @param \PNX\NestedSet\Node $node
49+
* The node to insert.
50+
*
51+
* @return \PNX\NestedSet\Node
52+
* The new node with updated position.
53+
*
54+
* @throws \Exception
55+
* If a transaction error occurs.
56+
*/
57+
protected function insertNodeAtPostion($newLeftPosition, $depth, Node $node) {
3658

3759
try {
3860
$this->connection->beginTransaction();
3961

40-
list($right, $depth) = $this->getInsertionPosition($parent);
41-
4262
// Move everything across two places.
4363
$this->connection->executeUpdate('UPDATE tree SET nested_right = nested_right + 2 WHERE nested_right > ?',
44-
[$right]
64+
[$newLeftPosition]
4565
);
4666
$this->connection->executeUpdate('UPDATE tree SET nested_left = nested_left + 2 WHERE nested_left > ?',
47-
[$right]
67+
[$newLeftPosition]
4868
);
4969

5070
// Create a new node object to be returned.
5171
$newNode = new Node(
52-
$child->getId(),
53-
$child->getRevisionId(),
54-
$right + 1,
55-
$right + 2,
72+
$node->getId(),
73+
$node->getRevisionId(),
74+
$newLeftPosition + 1,
75+
$newLeftPosition + 2,
5676
$depth
5777
);
5878

@@ -75,23 +95,6 @@ public function addNode(Node $parent, Node $child) {
7595

7696
}
7797

78-
/**
79-
* Finds the right-most child node.
80-
*
81-
* @param \PNX\NestedSet\Node $parent
82-
* The parent node.
83-
*
84-
* @return \PNX\NestedSet\Node
85-
* The right-most child node.
86-
*/
87-
protected function findRightMostChild(Node $parent) {
88-
$result = $this->connection->fetchAssoc('SELECT id, revision_id, nested_left, nested_right, depth FROM tree WHERE nested_right = ? - 1',
89-
[$parent->getRight()]);
90-
if ($result) {
91-
return new Node($result['id'], $result['revision_id'], $result['nested_left'], $result['nested_right'], $result['depth']);
92-
}
93-
}
94-
9598
/**
9699
* {@inheritdoc}
97100
*/
@@ -336,19 +339,6 @@ protected function moveSubTreeToPosition($newLeftPosition, Node $node) {
336339

337340
}
338341

339-
/**
340-
* Determines if this node is a 'leaf', i.e. has no children.
341-
*
342-
* @param \PNX\NestedSet\Node $node
343-
* The node to check.
344-
*
345-
* @return bool
346-
* TRUE if there are no children. FALSE otherwise.
347-
*/
348-
protected function isLeaf(Node $node) {
349-
return $node->getRight() - $node->getLeft() === 1;
350-
}
351-
352342
/**
353343
* {@inheritdoc}
354344
*/
@@ -361,31 +351,4 @@ public function getNodeAtPosition($left) {
361351
}
362352
}
363353

364-
/**
365-
* Gets the insertion position under the given parent.
366-
*
367-
* Takes into account if the parent has no children.
368-
*
369-
* @param \PNX\NestedSet\Node $parent
370-
* The parent node.
371-
*
372-
* @return int[]
373-
* The right and depth postiions.
374-
*/
375-
protected function getInsertionPosition(Node $parent) {
376-
if ($this->isLeaf($parent)) {
377-
// We are on a leaf node.
378-
$right = $parent->getLeft();
379-
$depth = $parent->getDepth() + 1;
380-
}
381-
else {
382-
// Find right most child.
383-
/** @var Node $rightChild */
384-
$rightChild = $this->findRightMostChild($parent);
385-
$right = $rightChild->getRight();
386-
$depth = $rightChild->getDepth();
387-
}
388-
return [$right, $depth];
389-
}
390-
391354
}

tests/Functional/DbalNestedSetTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public function testAddNodeWithExistingChildren() {
167167
$parent = $this->nestedSet->getNode(3, 1);
168168
$child = new Node(12, 1);
169169

170-
$newNode = $this->nestedSet->addNode($parent, $child);
170+
$newNode = $this->nestedSet->insertNodeBelow($parent, $child);
171171

172172
// Should be inserted in right-most spot.
173173
$this->assertEquals(21, $newNode->getLeft());
@@ -189,7 +189,7 @@ public function testAddNodeWithNoChildren() {
189189
$parent = $this->nestedSet->getNode(6, 1);
190190
$child = new Node(13, 1);
191191

192-
$newNode = $this->nestedSet->addNode($parent, $child);
192+
$newNode = $this->nestedSet->insertNodeBelow($parent, $child);
193193

194194
// Should be inserted below 6 with depth 4.
195195
$this->assertEquals(7, $newNode->getLeft());

0 commit comments

Comments
 (0)