Completed
Push — master ( 2e5d0e...583994 )
by Bartko
06:02
created

Top   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 88.37%

Importance

Changes 6
Bugs 2 Features 0
Metric Value
wmc 25
c 6
b 2
f 0
lcom 1
cbo 3
dl 0
loc 84
ccs 38
cts 43
cp 0.8837
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getNewParentId() 0 4 1
A getLevelShift() 0 4 1
A getHoleLeftIndex() 0 10 4
A getHoleRightIndex() 0 10 4
A getSourceNodeIndexShift() 0 10 4
A fixHoleFromIndex() 0 12 4
A makeHoleFromIndex() 0 4 1
A canMoveBranch() 0 9 3
A isSourceNodeAtRequiredPosition() 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() || $this->isMovedUp()) {
22 3
            return $this->getSourceNode()->getLeft() + $this->getIndexShift();
23 3
        } elseif ($this->isMovedDown()) {
24 3
            return $this->getSourceNode()->getLeft();
25
        } else {
26
            throw new Exception\BaseException('Cannot move node');
27
        }
28
    }
29
30 3
    public function getHoleRightIndex()
31
    {
32 3
        if ($this->isMovedToRoot() || $this->isMovedUp()) {
33 3
            return $this->getSourceNode()->getRight() + $this->getIndexShift();
34 3
        } elseif ($this->isMovedDown()) {
35 3
            return $this->getSourceNode()->getRight();
36
        } else {
37
            throw new Exception\BaseException('Cannot move node');
38
        }
39
    }
40
41 3
    public function getSourceNodeIndexShift()
42
    {
43 3
        if ($this->isMovedToRoot() || $this->isMovedUp()) {
44 3
            return $this->getTargetNode()->getLeft() - $this->getSourceNode()->getRight() - 1;
45 3
        } elseif ($this->isMovedDown()) {
46 3
            return $this->getTargetNode()->getLeft() - $this->getSourceNode()->getLeft();
47
        } else {
48
            throw new Exception\BaseException('Cannot move node');
49
        }
50
    }
51
52 3
    public function fixHoleFromIndex()
53
    {
54 3
        if ($this->isMovedToRoot()) {
55 3
            return $this->getSourceNode()->getRight() + $this->getIndexShift();
56 3
        } elseif ($this->isMovedUp()) {
57 3
            return $this->getSourceNode()->getRight();
58 3
        } elseif ($this->isMovedDown()) {
59 3
            return $this->getSourceNode()->getLeft();
60
        } else {
61
            throw new Exception\BaseException('Cannot move node');
62
        }
63
    }
64
65 3
    public function makeHoleFromIndex()
66
    {
67 3
        return $this->getTargetNode()->getLeft() - 1;
68
    }
69
70 3
    public function canMoveBranch()
71
    {
72 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...
73
            return false;
74
        }
75
76 3
        return ($this->getTargetNode()->isRoot()) ?
77 3
            false : true;
78
    }
79
80 3
    public function isSourceNodeAtRequiredPosition()
81
    {
82 3
        $sourceNode = $this->getSourceNode();
83 3
        $targetNode = $this->getTargetNode();
84
85 3
        return ($targetNode->getLeft() == ($sourceNode->getRight() + 1) &&
86 3
                $targetNode->getParentId() == $sourceNode->getParentId()) ?
87 3
            true : false;
88
    }
89
}
90