Passed
Push — master ( 0f755c...8f995b )
by Ben
10:33
created

ActingAsParent::detachChild()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Thinktomorrow\Chief\Relations;
4
5
use Illuminate\Database\Eloquent\Collection;
6
use Thinktomorrow\Chief\PageBuilder\PresentSections;
7
8
trait ActingAsParent
9
{
10
    protected $loadedChildRelations;
11
12 65
    public function children(): Collection
13
    {
14 65
        if ($this->areChildRelationsLoaded()) {
15 50
            return $this->loadedChildRelations;
16
        }
17 65
        return $this->loadedChildRelations = $this->freshChildren();
18
    }
19
20 65
    public function freshChildren(): Collection
21
    {
22 65
        $this->loadedChildRelations = null;
23
24 65
        return new Collection(Relation::children($this->getMorphClass(), $this->getKey())->all());
0 ignored issues
show
Bug introduced by
It seems like getKey() 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

24
        return new Collection(Relation::children($this->getMorphClass(), $this->/** @scrutinizer ignore-call */ getKey())->all());
Loading history...
Bug introduced by
It seems like getMorphClass() 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

24
        return new Collection(Relation::children($this->/** @scrutinizer ignore-call */ getMorphClass(), $this->getKey())->all());
Loading history...
25
    }
26
27 38
    public function adoptChild(ActsAsChild $child, array $attributes = [])
28
    {
29
        // Reset cached relation
30 38
        $this->loadedChildRelations = null;
31
32 38
        $this->attachChild($child->getMorphClass(), $child->getKey(), $attributes);
0 ignored issues
show
Bug introduced by
The method getKey() does not exist on Thinktomorrow\Chief\Relations\ActsAsChild. Since it exists in all sub-types, consider adding an abstract or default implementation to Thinktomorrow\Chief\Relations\ActsAsChild. ( Ignorable by Annotation )

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

32
        $this->attachChild($child->getMorphClass(), $child->/** @scrutinizer ignore-call */ getKey(), $attributes);
Loading history...
Bug introduced by
The method getMorphClass() does not exist on Thinktomorrow\Chief\Relations\ActsAsChild. Since it exists in all sub-types, consider adding an abstract or default implementation to Thinktomorrow\Chief\Relations\ActsAsChild. ( Ignorable by Annotation )

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

32
        $this->attachChild($child->/** @scrutinizer ignore-call */ getMorphClass(), $child->getKey(), $attributes);
Loading history...
33 38
    }
34
35 8
    public function rejectChild(ActsAsChild $child)
36
    {
37
        // Reset cached relation
38 8
        $this->loadedChildRelations = null;
39
40 8
        $this->detachChild($child->getMorphClass(), $child->getKey());
41 8
    }
42
43 7
    public function renderChildren(): string
44
    {
45 7
        return $this->presentChildren()->implode('');
46
    }
47
48 10
    public function presentChildren(): \Illuminate\Support\Collection
49
    {
50 10
        return (new PresentSections())($this, $this->children());
51
    }
52
53 1
    public function relationWithChild(ActsAsChild $child): Relation
54
    {
55 1
        return Relation::query()
0 ignored issues
show
Bug Best Practice introduced by
The expression return Thinktomorrow\Chi...ild->getKey())->first() could return the type null which is incompatible with the type-hinted return Thinktomorrow\Chief\Relations\Relation. Consider adding an additional type-check to rule them out.
Loading history...
56 1
            ->where('parent_type', $this->getMorphClass())
57 1
            ->where('parent_id', $this->getKey())
58 1
            ->where('child_type', $child->getMorphClass())
59 1
            ->where('child_id', $child->getKey())
60 1
            ->first();
61
    }
62
63 5
    public function sortChild(ActsAsChild $child, $sort = 0)
64
    {
65 5
        $this->loadedChildRelations = null;
66
67 5
        Relation::query()
68 5
            ->where('parent_type', $this->getMorphClass())
69 5
            ->where('parent_id', $this->getKey())
70 5
            ->where('child_type', $child->getMorphClass())
71 5
            ->where('child_id', $child->getKey())
72 5
            ->update(['sort' => $sort]);
73 5
    }
74
75 38
    public function detachAllChildRelations()
76
    {
77 38
        Relation::deleteAllChildRelationsOf($this->getMorphClass(), $this->getKey());
78 38
    }
79 38
80 38
    private function attachChild($child_type, $child_id, array $attributes = [])
81 38
    {
82 38
        Relation::firstOrCreate([
83 38
            'parent_type' => $this->getMorphClass(),
84
            'parent_id'   => $this->getKey(),
85 8
            'child_type'  => $child_type,
86
            'child_id'    => $child_id,
87 8
        ], $attributes);
88 8
    }
89 8
90 8
    private function detachChild($child_type, $child_id)
91 8
    {
92 8
        Relation::query()
93 8
            ->where('parent_type', $this->getMorphClass())
94
            ->where('parent_id', $this->getKey())
95 65
            ->where('child_type', $child_type)
96
            ->where('child_id', $child_id)
97 65
            ->delete();
98
    }
99
100
    private function areChildRelationsLoaded(): bool
101
    {
102
        return !is_null($this->loadedChildRelations);
103
    }
104
}
105