Passed
Push — feature/permissions-management ( 3c8a9f...1cc208 )
by Harings
19:21 queued 10s
created

HasNesting::saveTreeFromIds()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 13
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 18
rs 8.8333
1
<?php
2
3
namespace A17\Twill\Models\Behaviors;
4
5
use Kalnoy\Nestedset\NodeTrait;
6
7
trait HasNesting
8
{
9
    use NodeTrait;
10
11
    /**
12
     * Returns the combined slug for this item including all ancestors.
13
     *
14
     * @param string|null $locale
15
     * @return string
16
     */
17
    public function getNestedSlug($locale = null)
18
    {
19
        return collect([$this->getAncestorsSlug($locale), $this->getSlug($locale)])
0 ignored issues
show
Bug introduced by
It seems like getSlug() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

19
        return collect([$this->getAncestorsSlug($locale), $this->/** @scrutinizer ignore-call */ getSlug($locale)])
Loading history...
20
            ->filter()
21
            ->implode('/');
22
    }
23
24
    /**
25
     * @return string
26
     */
27
    public function getNestedSlugAttribute()
28
    {
29
        return $this->getNestedSlug();
30
    }
31
32
    /**
33
     * Returns the combined slug for all ancestors of this item.
34
     *
35
     * @param string|null $locale
36
     * @return string
37
     */
38
    public function getAncestorsSlug($locale = null)
39
    {
40
        return collect($this->ancestors ?? [])
41
            ->map(function ($i) use ($locale) { return $i->getSlug($locale); })
42
            ->implode('/');
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getAncestorsSlugAttribute()
49
    {
50
        return $this->getAncestorsSlug();
51
    }
52
53
    public static function saveTreeFromIds($nodeTree)
54
    {
55
        $nodeModels = self::all();
56
        $nodeArrays = self::flattenTree($nodeTree);
57
58
        foreach ($nodeArrays as $nodeArray) {
59
            $nodeModel = $nodeModels->where('id', $nodeArray['id'])->first();
60
61
            if ($nodeArray['parent_id'] === null) {
62
                if (!$nodeModel->isRoot() || $nodeModel->position !== $nodeArray['position']) {
63
                    $nodeModel->position = $nodeArray['position'];
64
                    $nodeModel->saveAsRoot();
65
                }
66
            } else {
67
                if ($nodeModel->position !== $nodeArray['position'] || $nodeModel->parent_id !== $nodeArray['parent_id']) {
68
                    $nodeModel->position = $nodeArray['position'];
69
                    $nodeModel->parent_id = $nodeArray['parent_id'];
70
                    $nodeModel->save();
71
                }
72
            }
73
        }
74
    }
75
76
    public static function flattenTree(array $nodeTree, int $parentId = null)
77
    {
78
        $nodeArrays = [];
79
        $position = 0;
80
81
        foreach ($nodeTree as $node) {
82
            $nodeArrays[] = [
83
                'id' => $node['id'],
84
                'position' => $position++,
85
                'parent_id' => $parentId,
86
            ];
87
88
            if (count($node['children']) > 0) {
89
                $childArrays = self::flattenTree($node['children'], $node['id']);
90
                $nodeArrays = array_merge($nodeArrays, $childArrays);
91
            }
92
        }
93
94
        return $nodeArrays;
95
    }
96
}
97