Completed
Push — develop ( 32afc4...f78f16 )
by Bartko
03:00
created

Bottom::getHoleLeftIndex()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.2
cc 4
eloc 9
nc 4
nop 0
crap 4
1
<?php
2
namespace StefanoTree\NestedSet\MoveStrategy;
3
4
use StefanoTree\Exception;
5
6
class Bottom
7
    extends MoveStrategyAbstract
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
8
{
9 6
    public function getNewParentId()
10
    {
11 6
        return $this->getTargetNode()
12 6
                    ->getParentId();
13
    }
14
15 6
    public function getLevelShift()
16
    {
17 6
        return $this->getTargetNode()->getLevel() - $this->getSourceNode()->getLevel();
18
    }
19
20 6
    public function getHoleLeftIndex()
21
    {
22 6
        if ($this->isMovedToRoot()) {
23 3
            return $this->getSourceNode()->getLeft();
24 6
        } elseif ($this->isMovedUp()) {
25 3
            return $this->getSourceNode()->getLeft() + $this->getIndexShift();
26 6
        } elseif ($this->isMovedDown()) {
27 6
            return $this->getSourceNode()->getLeft();
28
        } else {
29
            // @codeCoverageIgnoreStart
30
            throw new Exception\BaseException('Cannot move node');
31
            // @codeCoverageIgnoreEnd
32
        }
33
    }
34
35 6
    public function getHoleRightIndex()
36
    {
37 6
        if ($this->isMovedToRoot()) {
38 3
            return $this->getSourceNode()->getRight();
39 6
        } elseif ($this->isMovedUp()) {
40 3
            return $this->getSourceNode()->getRight() + $this->getIndexShift();
41 6
        } elseif ($this->isMovedDown()) {
42 6
            return $this->getSourceNode()->getRight();
43
        } else {
44
            // @codeCoverageIgnoreStart
45
            throw new Exception\BaseException('Cannot move node');
46
            // @codeCoverageIgnoreEnd
47
        }
48
    }
49
50 6
    public function getSourceNodeIndexShift()
51
    {
52 6
        if ($this->isMovedToRoot()) {
53 3
            return $this->getTargetNode()->getRight() - $this->getSourceNode()->getLeft() + 1;
54 6
        } elseif ($this->isMovedUp()) {
55 3
            return $this->getTargetNode()->getRight() - $this->getSourceNode()->getLeft()
56 3
                + 1 - $this->getIndexShift();
57 6
        } elseif ($this->isMovedDown()) {
58 6
            return $this->getTargetNode()->getRight() - $this->getSourceNode()->getLeft() + 1;
59
        } else {
60
            // @codeCoverageIgnoreStart
61
            throw new Exception\BaseException('Cannot move node');
62
            // @codeCoverageIgnoreEnd
63
        }
64
    }
65
66 6
    public function fixHoleFromIndex()
67
    {
68 6
        if ($this->isMovedToRoot()) {
69 3
            return $this->getSourceNode()->getLeft();
70 6
        } elseif ($this->isMovedUp()) {
71 3
            return $this->getSourceNode()->getLeft() + $this->getIndexShift();
72 6
        } elseif ($this->isMovedDown()) {
73 6
            return $this->getSourceNode()->getLeft();
74
        } else {
75
            // @codeCoverageIgnoreStart
76
            throw new Exception\BaseException('Cannot move node');
77
            // @codeCoverageIgnoreEnd
78
        }
79
    }
80
81 6
    public function makeHoleFromIndex()
82
    {
83 6
        return $this->getTargetNode()->getRight();
84
    }
85
86 9
    public function canMoveBranch()
87
    {
88 9
        if (false == parent::canMoveBranch()) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
89 3
            return false;
90
        }
91
92 6
        return ($this->getTargetNode()->isRoot()) ?
93 6
            false : true;
94
    }
95
96 6
    public function isSourceNodeAtRequiredPosition()
97
    {
98 6
        $sourceNode = $this->getSourceNode();
99 6
        $targetNode = $this->getTargetNode();
100
101 6
        return ($targetNode->getRight() == ($sourceNode->getLeft() - 1) &&
102 3
                $targetNode->getParentId() == $sourceNode->getParentId()) ?
103 6
            true : false;
104
    }
105
}
106