Passed
Push — master ( 85c126...3a5647 )
by Petr
04:53 queued 01:54
created

Actions::create()   A

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