DescendantQueryBuilder::levelLimit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace StefanoTree\NestedSet\QueryBuilder;
6
7
use StefanoTree\NestedSet\Manipulator\ManipulatorInterface;
8
use StefanoTree\NestedSet\Utilities;
9
10
class DescendantQueryBuilder implements DescendantQueryBuilderInterface
11
{
12
    private $manipulator;
13
14
    private $excludeFirstNLevel = 0;
15
    private $limitDepth = null;
16
    private $excludeBranch = null;
17
18
    /**
19
     * @param ManipulatorInterface $manipulator
20
     */
21 9
    public function __construct(ManipulatorInterface $manipulator)
22
    {
23 9
        $this->manipulator = $manipulator;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 9
    public function get($nodeId, bool $nested = false): array
30
    {
31 9
        $result = $this->getManipulator()
32 9
            ->getDescendants($nodeId, $this->excludeFirstNLevel, $this->limitDepth, $this->excludeBranch);
33
34 9
        return $nested ?
35 9
            Utilities::flatToNested($result, $this->getManipulator()->getOptions()->getLevelColumnName()) : $result;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 5
    public function excludeFirstNLevel(int $count): DescendantQueryBuilderInterface
42
    {
43 5
        $this->excludeFirstNLevel = $count;
44
45 5
        return $this;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 5
    public function levelLimit(int $count): DescendantQueryBuilderInterface
52
    {
53 5
        $this->limitDepth = $count;
54
55 5
        return $this;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 1
    public function excludeBranch($nodeId): DescendantQueryBuilderInterface
62
    {
63 1
        $this->excludeBranch = $nodeId;
64
65 1
        return $this;
66
    }
67
68 9
    private function getManipulator(): ManipulatorInterface
69
    {
70 9
        return $this->manipulator;
71
    }
72
}
73