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

Top   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.78%

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 25
c 5
b 2
f 0
lcom 1
cbo 3
dl 0
loc 98
ccs 44
cts 45
cp 0.9778
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getNewParentId() 0 4 1
A getLevelShift() 0 4 1
A getHoleLeftIndex() 0 14 4
A getHoleRightIndex() 0 14 4
A getSourceNodeIndexShift() 0 14 4
A fixHoleFromIndex() 0 14 4
A makeHoleFromIndex() 0 4 1
A isSourceNodeAtRequiredPosition() 0 9 3
A canMoveBranch() 0 9 3
1
<?php
2
namespace StefanoTree\NestedSet\MoveStrategy;
3
4
use StefanoTree\Exception;
5
6
class Top
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 3
    public function getNewParentId()
10
    {
11 3
        return $this->getTargetNode()->getParentId();
12
    }
13
14 3
    public function getLevelShift()
15
    {
16 3
        return $this->getTargetNode()->getLevel() - $this->getSourceNode()->getLevel();
17
    }
18
19 3
    public function getHoleLeftIndex()
20
    {
21 3
        if ($this->isMovedToRoot()) {
22 3
            return $this->getSourceNode()->getLeft() + $this->getIndexShift();
23 3
        } elseif ($this->isMovedUp()) {
24 3
            return $this->getSourceNode()->getLeft() + $this->getIndexShift();
25 3
        } elseif ($this->isMovedDown()) {
26 3
            return $this->getSourceNode()->getLeft();
27
        } else {
28
            // @codeCoverageIgnoreStart
29
            throw new Exception\BaseException('Cannot move node');
30
            // @codeCoverageIgnoreEnd
31
        }
32
    }
33
34 3
    public function getHoleRightIndex()
35
    {
36 3
        if ($this->isMovedToRoot()) {
37 3
            return $this->getSourceNode()->getRight() + $this->getIndexShift();
38 3
        } elseif ($this->isMovedUp()) {
39 3
            return $this->getSourceNode()->getRight() + $this->getIndexShift();
40 3
        } elseif ($this->isMovedDown()) {
41 3
            return $this->getSourceNode()->getRight();
42
        } else {
43
            // @codeCoverageIgnoreStart
44
            throw new Exception\BaseException('Cannot move node');
45
            // @codeCoverageIgnoreEnd
46
        }
47
    }
48
49 3
    public function getSourceNodeIndexShift()
50
    {
51 3
        if ($this->isMovedToRoot()) {
52 3
            return $this->getTargetNode()->getLeft() - $this->getSourceNode()->getRight() - 1;
53 3
        } elseif ($this->isMovedUp()) {
54 3
            return $this->getTargetNode()->getLeft() - $this->getSourceNode()->getRight() - 1;
55 3
        } elseif ($this->isMovedDown()) {
56 3
            return $this->getTargetNode()->getLeft() - $this->getSourceNode()->getLeft();
57
        } else {
58
            // @codeCoverageIgnoreStart
59
            throw new Exception\BaseException('Cannot move node');
60
            // @codeCoverageIgnoreEnd
61
        }
62
    }
63
64 3
    public function fixHoleFromIndex()
65
    {
66 3
        if ($this->isMovedToRoot()) {
67 3
            return $this->getSourceNode()->getRight() + $this->getIndexShift();
68 3
        } elseif ($this->isMovedUp()) {
69 3
            return $this->getSourceNode()->getRight();
70 3
        } elseif ($this->isMovedDown()) {
71 3
            return $this->getSourceNode()->getLeft();
72
        } else {
73
            // @codeCoverageIgnoreStart
74
            throw new Exception\BaseException('Cannot move node');
75
            // @codeCoverageIgnoreEnd
76
        }
77
    }
78
79 3
    public function makeHoleFromIndex()
80
    {
81 3
        return $this->getTargetNode()->getLeft() - 1;
82
    }
83
84 3
    public function canMoveBranch()
85
    {
86 3
        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...
87
            return false;
88
        }
89
90 3
        return ($this->getTargetNode()->isRoot()) ?
91 3
            false : true;
92
    }
93
94 3
    public function isSourceNodeAtRequiredPosition()
95
    {
96 3
        $sourceNode = $this->getSourceNode();
97 3
        $targetNode = $this->getTargetNode();
98
99 3
        return ($targetNode->getLeft() == ($sourceNode->getRight() + 1) &&
100 3
                $targetNode->getParentId() == $sourceNode->getParentId()) ?
101 3
            true : false;
102
    }
103
}
104