Test Failed
Push — master ( f74f09...dc3255 )
by Petr
02:47
created

Actions::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace kalanis\nested_tree;
4
5
class Actions
6
{
7
    protected ?Support\Options $options = null;
8
9
    public function __construct(
10
        protected readonly NestedSet $nestedSet,
11
    ) {
12
    }
13
14
    public function setExtraOptions(?Support\Options $options) : void
15
    {
16
        $this->options = $options;
17
    }
18
19
    public function fixStructure() : void
20
    {
21
        $this->nestedSet->rebuild($this->getOptions());
22
    }
23
24
    public function create(Support\Node $node) : void
25
    {
26
        $this->nestedSet->add($node, $this->getOptions());
27
        $this->nestedSet->rebuild($this->getOptions());
28
    }
29
30
    public function update(Support\Node $node) : void
31
    {
32
        $this->nestedSet->update($node);
33
        $this->nestedSet->rebuild($this->getOptions());
34
    }
35
36
    /**
37
     * @param int<1, max> $nodeId
38
     * @param int<1, max> $newPosition
39
     * @return void
40
     */
41
    public function movePosition(int $nodeId, int $newPosition) : void
42
    {
43
        $this->nestedSet->move($nodeId, $newPosition);
44
        $this->nestedSet->rebuild($this->getOptions());
45
    }
46
47
    /**
48
     * @param int<1, max> $nodeId
49
     * @param int<0, max>|null $newParent
50
     * @return void
51
     */
52
    public function changeParent(int $nodeId, ?int $newParent) : void
53
    {
54
        $this->nestedSet->changeParent($nodeId, $newParent);
55
        $this->nestedSet->rebuild($this->getOptions());
56
    }
57
58
    /**
59
     * @param int<1, max> $nodeId
60
     * @param bool $childrenUp
61
     * @return bool
62
     */
63
    public function delete(int $nodeId, bool $childrenUp = false) : bool
64
    {
65
        $result = $childrenUp
66
            ? $this->nestedSet->deletePullUpChildren($nodeId, $this->getOptions())
67
            : !empty($this->nestedSet->deleteWithChildren($nodeId, $this->getOptions()))
68
        ;
69
        $this->nestedSet->rebuild($this->getOptions());
70
71
        return $result;
72
    }
73
74
    protected function getOptions() : Support\Options
75
    {
76
        return $this->options ?? new Support\Options();
77
    }
78
}
79