Actions::getNode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace kalanis\nested_tree;
4
5
class Actions
6
{
7
    protected ?Support\Options $options = null;
8
9 9
    public function __construct(
10
        protected readonly NestedSet $nestedSet,
11
    ) {
12 9
    }
13
14 2
    public function setExtraOptions(?Support\Options $options) : void
15
    {
16 2
        $this->options = $options;
17
    }
18
19 1
    public function fixStructure() : void
20
    {
21 1
        $this->nestedSet->rebuild($this->getOptions());
22
    }
23
24
    /**
25
     * Get concrete node
26
     *
27
     * @param int $nodeId
28
     * @return Support\Node|null
29
     */
30 2
    public function getNode(int $nodeId) : ?Support\Node
31
    {
32 2
        $options = clone $this->getOptions();
33 2
        $options->currentId = $nodeId;
34
35 2
        return $this->nestedSet->getNode($options);
36
    }
37
38
    /**
39
     * Create new node in connected table
40
     *
41
     * @param Support\Node $node
42
     * @return Support\Node
43
     */
44 1
    public function create(Support\Node $node) : Support\Node
45
    {
46 1
        $result = $this->nestedSet->add($node, $this->getOptions());
47 1
        $this->nestedSet->rebuild($this->getOptions());
48
49 1
        return $result;
50
    }
51
52
    /**
53
     * Update node's data
54
     *
55
     * @param Support\Node $node
56
     * @return bool
57
     */
58 1
    public function update(Support\Node $node) : bool
59
    {
60 1
        $result = $this->nestedSet->update($node);
61 1
        $this->nestedSet->rebuild($this->getOptions());
62
63 1
        return $result;
64
    }
65
66
    /**
67
     * Move node to the position
68
     *
69
     * @param int<1, max> $nodeId
70
     * @param int<0, max> $newPosition
71
     * @return bool
72
     */
73 1
    public function movePosition(int $nodeId, int $newPosition) : bool
74
    {
75 1
        $result = $this->nestedSet->move($nodeId, $newPosition);
76 1
        $this->nestedSet->rebuild($this->getOptions());
77
78 1
        return $result;
79
    }
80
81
    /**
82
     * Change node's parent
83
     *
84
     * @param int<1, max> $nodeId
85
     * @param int<0, max>|null $newParent
86
     * @return bool
87
     */
88 1
    public function changeParent(int $nodeId, ?int $newParent) : bool
89
    {
90 1
        $result = $this->nestedSet->changeParent($nodeId, $newParent);
91 1
        $this->nestedSet->rebuild($this->getOptions());
92
93 1
        return $result;
94
    }
95
96
    /**
97
     * Delete node, with or without its children
98
     *
99
     * @param int<1, max> $nodeId
100
     * @param bool $childrenUp children will be reconnected to upper entry or deleted too
101
     * @return bool
102
     */
103 2
    public function delete(int $nodeId, bool $childrenUp = false) : bool
104
    {
105 2
        $result = $childrenUp
106 1
            ? $this->nestedSet->deletePullUpChildren($nodeId, $this->getOptions())
107 1
            : !empty($this->nestedSet->deleteWithChildren($nodeId, $this->getOptions()))
108 2
        ;
109 2
        $this->nestedSet->rebuild($this->getOptions());
110
111 2
        return $result;
112
    }
113
114 9
    public function getOptions() : Support\Options
115
    {
116 9
        return $this->options ?? new Support\Options();
117
    }
118
}
119