Completed
Push — master ( 2b0446...afde37 )
by Bartko
04:01 queued 01:40
created

ChildBottom::makeHole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace StefanoTree\NestedSet\MoveStrategy;
6
7
use StefanoTree\Exception\TreeIsBrokenException;
8
use StefanoTree\Exception\ValidationException;
9
10
class ChildBottom extends MoveStrategyAbstract implements MoveStrategyInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15 1
    protected function canMoveBranch(): void
16
    {
17 1
        if ($this->isTargetNodeInsideSourceBranch()) {
18
            throw new ValidationException('Cannot move. Target node is inside source branch.');
19
        }
20 1
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 1
    protected function isSourceNodeAtRequiredPosition(): bool
26
    {
27 1
        $source = $this->getSourceNodeInfo();
28 1
        $target = $this->getTargetNodeInfo();
29
30 1
        return ($source->getParentId() == $target->getId() && $source->getRight() == ($target->getRight() - 1)) ? true : false;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 1
    protected function updateParentId(): void
37
    {
38 1
        $newParentId = $this->getTargetNodeInfo()->getId();
39 1
        $this->_updateParentId($this->getSourceNodeInfo(), $newParentId);
40 1
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    protected function updateLevels(): void
46
    {
47 1
        $source = $this->getSourceNodeInfo();
48
49 1
        $levelShift = $this->getTargetNodeInfo()->getLevel() - $source->getLevel() + 1;
50 1
        $this->_updateLevels($source, $levelShift);
51 1
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 1
    protected function makeHole(): void
57
    {
58 1
        $holeFromIndex = $this->getTargetNodeInfo()->getRight() - 1;
59 1
        $indexShift = $this->getIndexShift();
60 1
        $scope = $this->getSourceNodeInfo()->getScope();
61
62 1
        $this->_makeHole($holeFromIndex, $indexShift, $scope);
63 1
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 1
    protected function moveBranchToTheHole(): void
69
    {
70 1
        $source = $this->getSourceNodeInfo();
71 1
        $target = $this->getTargetNodeInfo();
72
73 1
        if ($this->isMovedToRoot() || $this->isMovedDown()) {
74 1
            $leftIndex = $source->getLeft();
75 1
            $rightIndex = $source->getRight();
76 1
            $indexShift = $target->getRight() - $source->getLeft();
77 1
        } elseif ($this->isMovedUp()) {
78 1
            $leftIndex = $source->getLeft() + $this->getIndexShift();
79 1
            $rightIndex = $source->getRight() + $this->getIndexShift();
80 1
            $indexShift = $target->getRight() - $source->getRight() - 1;
81
        } else {
82
            throw new TreeIsBrokenException();
83
        }
84
85 1
        $scope = $source->getScope();
86
87 1
        $this->_moveBranchToTheHole($leftIndex, $rightIndex, $indexShift, $scope);
88 1
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 1
    protected function patchHole(): void
94
    {
95 1
        $source = $this->getSourceNodeInfo();
96
97 1
        $fromIndex = $source->getRight();
98 1
        $indexShift = $this->getIndexShift() * -1;
99 1
        $scope = $source->getScope();
100
101 1
        $this->_patchHole($fromIndex, $indexShift, $scope);
102 1
    }
103
}
104