Passed
Push — master ( 687cbc...187bee )
by Petr
02:53
created

Actions::getNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
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 5
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 2
        return $this->nestedSet->getNode($options);
35
    }
36
37
    /**
38
     * Create new node in connected table
39
     *
40
     * @param Support\Node $node
41
     * @return Support\Node
42
     */
43 1
    public function create(Support\Node $node) : Support\Node
44
    {
45 1
        $result = $this->nestedSet->add($node, $this->getOptions());
46 1
        $this->nestedSet->rebuild($this->getOptions());
47
48 1
        return $result;
49
    }
50
51
    /**
52
     * Update node's data
53
     *
54
     * @param Support\Node $node
55
     * @return bool
56
     */
57 1
    public function update(Support\Node $node) : bool
58
    {
59 1
        $result = $this->nestedSet->update($node);
60 1
        $this->nestedSet->rebuild($this->getOptions());
61
62 1
        return $result;
63
    }
64
65
    /**
66
     * Move node to the position
67
     *
68
     * @param int<1, max> $nodeId
69
     * @param int<1, max> $newPosition
70
     * @return bool
71
     */
72 1
    public function movePosition(int $nodeId, int $newPosition) : bool
73
    {
74 1
        $result = $this->nestedSet->move($nodeId, $newPosition);
75 1
        $this->nestedSet->rebuild($this->getOptions());
76
77 1
        return $result;
78
    }
79
80
    /**
81
     * Change node's parent
82
     *
83
     * @param int<1, max> $nodeId
84
     * @param int<0, max>|null $newParent
85
     * @return bool
86
     */
87 1
    public function changeParent(int $nodeId, ?int $newParent) : bool
88
    {
89 1
        $result = $this->nestedSet->changeParent($nodeId, $newParent);
90 1
        $this->nestedSet->rebuild($this->getOptions());
91
92 1
        return $result;
93
    }
94
95
    /**
96
     * Delete node, with or without its children
97
     *
98
     * @param int<1, max> $nodeId
99
     * @param bool $childrenUp children will be reconnected to upper entry or deleted too
100
     * @return bool
101
     */
102 2
    public function delete(int $nodeId, bool $childrenUp = false) : bool
103
    {
104 2
        $result = $childrenUp
105 1
            ? $this->nestedSet->deletePullUpChildren($nodeId, $this->getOptions())
106 1
            : !empty($this->nestedSet->deleteWithChildren($nodeId, $this->getOptions()))
107 2
        ;
108 2
        $this->nestedSet->rebuild($this->getOptions());
109
110 2
        return $result;
111
    }
112
113 9
    public function getOptions() : Support\Options
114
    {
115 9
        return $this->options ?? new Support\Options();
116
    }
117
}
118