Test Setup Failed
Push — master ( c48a36...2ef870 )
by Oliver
04:00
created

NestedModelTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 44
rs 10
c 1
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
7
use Kalnoy\Nestedset\Collection;
8
use Kalnoy\Nestedset\NodeTrait;
9
10
trait NestedModelTrait
11
{
12
    use NodeTrait;
13
14
    /**
15
     * Build the collection representing the tree with possibly many root nodes
16
     *
17
     * @param Collection|null $nodes
18
     * @return Collection
19
     */
20
    public static function loadTree(Collection $nodes = null)
21
    {
22
        $nodes = $nodes ?? static::all();
23
24
        return $nodes->where((new static)->getParentIdName(), null)
25
            ->values()
26
            ->each(function ($item) use ($nodes) {
27
                $nodes->toTree($item);
28
            });
29
    }
30
31
    /*
32
    |--------------------------------------------------------------------------
33
    | Kalnoy/NestedSet
34
    |--------------------------------------------------------------------------
35
    |
36
    | Overwrite the default tree columns here to fit with backpack reorder
37
    */
38
39
    public function getLftName()
40
    {
41
        return 'lft';
42
    }
43
44
    public function getRgtName()
45
    {
46
        return 'rgt';
47
    }
48
49
    public function getParentIdName()
50
    {
51
        return 'parent_id';
52
    }
53
}