NestedModelTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 44
ccs 0
cts 13
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A loadTree() 0 10 1
A getLftName() 0 4 1
A getRgtName() 0 4 1
A getParentIdName() 0 4 1
1
<?php
2
3
4
namespace Webfactor\Laravel\Backpack\NestedModels\Traits;
5
6
use Kalnoy\Nestedset\Collection;
7
use Kalnoy\Nestedset\NodeTrait;
8
9
trait NestedModelTrait
10
{
11
    use NodeTrait;
12
13
    /**
14
     * Build the collection representing the tree with possibly many root nodes
15
     *
16
     * @param Collection|null $nodes
17
     * @return Collection
18
     */
19
    public static function loadTree(Collection $nodes = null)
20
    {
21
        $nodes = $nodes ?? static::all();
22
23
        return $nodes->where((new static)->getParentIdName(), null)
24
            ->values()
25
            ->each(function ($item) use ($nodes) {
26
                $nodes->toTree($item);
27
            });
28
    }
29
30
    /*
31
    |--------------------------------------------------------------------------
32
    | Kalnoy/NestedSet
33
    |--------------------------------------------------------------------------
34
    |
35
    | Overwrite the default tree columns here to fit with backpack reorder
36
    */
37
38
    public function getLftName()
39
    {
40
        return 'lft';
41
    }
42
43
    public function getRgtName()
44
    {
45
        return 'rgt';
46
    }
47
48
    public function getParentIdName()
49
    {
50
        return 'parent_id';
51
    }
52
}
53