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

Bottom::patchHole()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

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