Completed
Push — master ( 5033a3...4be7fb )
by Bartko
03:45
created

Validator   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 117
ccs 48
cts 48
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A _getAdapter() 0 4 1
B isValid() 0 24 3
A rebuild() 0 19 2
B _rebuild() 0 28 6
A _checkIfNodeIsRootNode() 0 8 2
1
<?php
2
namespace StefanoTree\NestedSet\Validator;
3
4
use Exception;
5
use StefanoTree\Exception\InvalidArgumentException;
6
use StefanoTree\Exception\TreeIsBrokenException;
7
use StefanoTree\NestedSet\Adapter\AdapterInterface;
8
use StefanoTree\NestedSet\NodeInfo;
9
10
class Validator
11
    implements ValidatorInterface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
12
{
13
    private $adapter = null;
14
15
    /**
16
     * @param AdapterInterface $adapter
17
     */
18
    public function __construct(AdapterInterface $adapter)
19
    {
20
        $this->adapter = $adapter;
21 9
    }
22
23 9
    /**
24 9
     * @return AdapterInterface
25
     */
26
    private function _getAdapter()
27
    {
28
        return $this->adapter;
29 9
    }
30
31 9
    public function isValid($rootNodeId)
32
    {
33
        $adapter = $this->_getAdapter();
34
35
        $adapter->beginTransaction();
36
        try {
37
            $adapter->lockTree();
38
39 9
            $rootNodeInfo = $this->_getAdapter()->getNodeInfo($rootNodeId);
40
41 9
            $this->_checkIfNodeIsRootNode($rootNodeInfo);
0 ignored issues
show
Bug introduced by
It seems like $rootNodeInfo defined by $this->_getAdapter()->getNodeInfo($rootNodeId) on line 39 can be null; however, StefanoTree\NestedSet\Va...checkIfNodeIsRootNode() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
42 9
            $this->_rebuild($rootNodeInfo, True);
0 ignored issues
show
Bug introduced by
It seems like $rootNodeInfo defined by $this->_getAdapter()->getNodeInfo($rootNodeId) on line 39 can be null; however, StefanoTree\NestedSet\Va...r\Validator::_rebuild() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
43
44 9
            $adapter->commitTransaction();
45
        } catch (TreeIsBrokenException $e) {
46 9
            $adapter->rollbackTransaction();
47 9
            return False;
48
        } catch (Exception $e) {
49
            $adapter->rollbackTransaction();
50 9
            throw $e;
51
        }
52
53
        return True;
54
    }
55
56
57 9
    public function rebuild($rootNodeId)
58
    {
59 9
        $adapter = $this->_getAdapter();
60
61 9
        $adapter->beginTransaction();
62 9
        try {
63
            $adapter->lockTree();
64 9
65 9
            $rootNodeInfo = $this->_getAdapter()->getNodeInfo($rootNodeId);
66
67 9
            $this->_checkIfNodeIsRootNode($rootNodeInfo);
0 ignored issues
show
Bug introduced by
It seems like $rootNodeInfo defined by $this->_getAdapter()->getNodeInfo($rootNodeId) on line 65 can be null; however, StefanoTree\NestedSet\Va...checkIfNodeIsRootNode() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
68 6
            $this->_rebuild($rootNodeInfo);
0 ignored issues
show
Bug introduced by
It seems like $rootNodeInfo defined by $this->_getAdapter()->getNodeInfo($rootNodeId) on line 65 can be null; however, StefanoTree\NestedSet\Va...r\Validator::_rebuild() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
69 6
70
            $adapter->commitTransaction();
71 9
        } catch (Exception $e) {
72
            $adapter->rollbackTransaction();
73
            throw $e;
74
        }
75 9
    }
76
77
    /**
78
     * @param NodeInfo $parentNodeInfo
79
     * @param bool $onlyValidate
80
     * @param int $left
81
     * @param int $level
82 9
     * @return int|mixed
83
     * @throws TreeIsBrokenException if tree is broken and $onlyValidate is true
84 9
     */
85 9
    private function _rebuild(NodeInfo $parentNodeInfo, $onlyValidate = false, $left = 1, $level = 0)
86
    {
87 9
        $adapter = $this->_getAdapter();
88 9
89
        $right = $left + 1;
90 9
91 9
        $children = $adapter->getChildrenNodeInfo($parentNodeInfo->getId());
92 9
93 9
        foreach ($children as $childNode) {
94
            $right = $this->_rebuild($childNode, $onlyValidate, $right, $level + 1);
95 6
        }
96
97
        if ($parentNodeInfo->getLeft() != $left
98 9
            || $parentNodeInfo->getRight() != $right
99 6
            || $parentNodeInfo->getLevel() != $level) {
100 6
            $parentNodeInfo->setLeft($left);
101
            $parentNodeInfo->setRight($right);
102 6
            $parentNodeInfo->setLevel($level);
103
104
            if ($onlyValidate) {
105 9
                throw new TreeIsBrokenException();
106
            } else {
107
                $adapter->updateNodeMetadata($parentNodeInfo);
108 9
            }
109
        }
110
111
        return $right + 1;
112
    }
113
114
    /**
115 9
     * @param NodeInfo $node
116
     * @throws InvalidArgumentException
117 9
     */
118 9
    private function _checkIfNodeIsRootNode(NodeInfo $node)
119
    {
120 9
        if (null != $node->getParentId()) {
121 9
            throw new InvalidArgumentException(
122 9
                sprintf('Given node id "%s" is not root id', $node->getId())
123 9
            );
124 9
        }
125 9
    }
126
}
127