Passed
Branch master (9026fd)
by Bartko
02:45
created

AncestorQueryBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 41
rs 10
ccs 15
cts 15
cp 1
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getManipulator() 0 3 1
A excludeFirstNLevel() 0 5 1
A __construct() 0 3 1
A excludeLastNLevel() 0 5 1
A get() 0 7 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