Passed
Push — master ( a6e07b...9fdc54 )
by Dallas
02:24
created

LTreeCollection::excludeLeaves()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 3
nc 3
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\LTree\Collections;
6
7
use Illuminate\Database\Eloquent\Collection;
8
use Illuminate\Database\Eloquent\Model;
9
use Umbrellio\LTree\Helpers\LTreeBuilder;
10
use Umbrellio\LTree\Helpers\LTreeNode;
11
use Umbrellio\LTree\Interfaces\HasLTreeRelations;
12
use Umbrellio\LTree\Interfaces\LTreeInterface;
13
use Umbrellio\LTree\Interfaces\ModelInterface;
14
use Umbrellio\LTree\Traits\LTreeModelTrait;
15
16
/**
17
 * @method LTreeInterface|ModelInterface first()
18
 * @property LTreeInterface[]|ModelInterface[]|HasLTreeRelations[] $items
19
 */
20
class LTreeCollection extends Collection
21
{
22
    private $withLeaves = true;
23
24
    public function toTree(bool $usingSort = true, bool $loadMissing = true): LTreeNode
25
    {
26
        if (!$model = $this->first()) {
27
            return new LTreeNode();
28
        }
29
30
        if ($loadMissing) {
31
            $this->loadMissingNodes($model);
32
        }
33
34
        if (!$this->withLeaves) {
35
            $this->excludeLeaves();
36
        }
37
38
        $builder = new LTreeBuilder(
39
            $model->getLtreePathColumn(),
40
            $model->getKeyName(),
41
            $model->getLtreeParentColumn()
42
        );
43
44
        return $builder->build($collection ?? $this, $usingSort);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $collection seems to never exist and therefore isset should always be false.
Loading history...
45
    }
46
47
    public function withLeaves(bool $state = true): self
48
    {
49
        $this->withLeaves = $state;
50
51
        return $this;
52
    }
53
54
    private function loadMissingNodes($model): self
55
    {
56
        if ($this->hasMissingNodes($model)) {
57
            $this->appendAncestors($model);
58
        }
59
60
        return $this;
61
    }
62
63
    private function excludeLeaves(): void
64
    {
65
        foreach ($this->items as $key => $item) {
66
            /** @var LTreeModelTrait $item */
67
            if ($item->ltreeChildren->isEmpty()) {
68
                $this->forget($key);
69
            }
70
        }
71
    }
72
73
    /**
74
     * @param LTreeInterface|ModelInterface $model
75
     */
76
    private function hasMissingNodes($model): bool
77
    {
78
        $paths = collect();
79
80
        foreach ($this->items as $item) {
81
            $paths = $paths->merge($item->getLtreePath());
82
        }
83
84
        return $paths
85
            ->unique()
86
            ->diff($this->pluck($model->getKeyName()))
0 ignored issues
show
Bug introduced by
The method getKeyName() does not exist on Umbrellio\LTree\Interfaces\LTreeInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Umbrellio\LTree\Interfaces\LTreeInterface. ( Ignorable by Annotation )

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

86
            ->diff($this->pluck($model->/** @scrutinizer ignore-call */ getKeyName()))
Loading history...
87
            ->isNotEmpty();
88
    }
89
90
    /**
91
     * @param LTreeInterface|ModelInterface $model
92
     */
93
    private function appendAncestors($model): void
94
    {
95
        $paths = $this
96
            ->pluck($model->getLtreePathColumn())
0 ignored issues
show
Bug introduced by
The method getLtreePathColumn() does not exist on Umbrellio\LTree\Interfaces\ModelInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Umbrellio\LTree\Interfaces\ModelInterface. ( Ignorable by Annotation )

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

96
            ->pluck($model->/** @scrutinizer ignore-call */ getLtreePathColumn())
Loading history...
97
            ->toArray();
98
        $ids = $this
99
            ->pluck($model->getKeyName())
100
            ->toArray();
101
102
        /** @var Model $model */
103
        $parents = $model::parentsOf($paths)
0 ignored issues
show
Bug Best Practice introduced by
The method Illuminate\Database\Eloquent\Model::parentsOf() is not static, but was called statically. ( Ignorable by Annotation )

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

103
        $parents = $model::/** @scrutinizer ignore-call */ parentsOf($paths)
Loading history...
104
            ->whereKeyNot($ids)
0 ignored issues
show
Bug introduced by
The method whereKeyNot() does not exist on Illuminate\Database\Eloquent\Builder. ( Ignorable by Annotation )

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

104
            ->/** @scrutinizer ignore-call */ whereKeyNot($ids)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
            ->get();
106
107
        foreach ($parents as $item) {
108
            $this->add($item);
109
        }
110
    }
111
}
112