AncestorQueryBuilder::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 7
rs 10
ccs 5
cts 5
cp 1
crap 2
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 AncestorQueryBuilder implements AncestorQueryBuilderInterface
11
{
12
    private $manipulator;
13
14
    private $excludeFirstNLevel = 0;
15
    private $excludeLastNLevel = 0;
16
17
    /**
18
     * @param ManipulatorInterface $manipulator
19
     */
20 5
    public function __construct(ManipulatorInterface $manipulator)
21
    {
22 5
        $this->manipulator = $manipulator;
23
    }
24
25 5
    public function get($nodeId, bool $nested = false): array
26
    {
27 5
        $result = $this->getManipulator()
28 5
            ->getAncestors($nodeId, $this->excludeFirstNLevel, $this->excludeLastNLevel);
29
30 5
        return $nested ?
31 5
            Utilities::flatToNested($result, $this->getManipulator()->getOptions()->getLevelColumnName()) : $result;
32
    }
33
34 1
    public function excludeFirstNLevel(int $count): AncestorQueryBuilderInterface
35
    {
36 1
        $this->excludeFirstNLevel = $count;
37
38 1
        return $this;
39
    }
40
41 2
    public function excludeLastNLevel(int $count): AncestorQueryBuilderInterface
42
    {
43 2
        $this->excludeLastNLevel = $count;
44
45 2
        return $this;
46
    }
47
48 5
    private function getManipulator(): ManipulatorInterface
49
    {
50 5
        return $this->manipulator;
51
    }
52
}
53