Test Setup Failed
Push — dependabot/composer/thinktomor... ( e86b85...256c6d )
by
unknown
67:00 queued 59:01
created

ActingAsParent::breakChildRelationCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

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

27
        return new Collection(Relation::children($this->getMorphClass(), $this->/** @scrutinizer ignore-call */ getKey())->all());
Loading history...
28
    }
29
30
    public function adoptChild(ActsAsChild $child, array $attributes = [])
31
    {
32
        // Reset cached relation
33
        $this->loadedChildRelations = null;
34
35
        $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

35
        $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

35
        $this->attachChild($child->/** @scrutinizer ignore-call */ getMorphClass(), $child->getKey(), $attributes);
Loading history...
36
    }
37
38
    public function rejectChild(ActsAsChild $child)
39
    {
40
        // Reset cached relation
41
        $this->loadedChildRelations = null;
42
43
        $this->detachChild($child->getMorphClass(), $child->getKey());
44
    }
45
46
    public function renderChildren(): string
47
    {
48
        return $this->presentChildren()->implode('');
49
    }
50
51
    public function presentChildren(): \Illuminate\Support\Collection
52
    {
53
        return (new PresentSections())($this, $this->children());
54
    }
55
56
    public function relationWithChild(ActsAsChild $child): Relation
57
    {
58
        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...
59
            ->where('parent_type', $this->getMorphClass())
60
            ->where('parent_id', $this->getKey())
61
            ->where('child_type', $child->getMorphClass())
62
            ->where('child_id', $child->getKey())
63
            ->first();
64
    }
65
66
    public function sortChild(ActsAsChild $child, $sort = 0)
67
    {
68
        $this->loadedChildRelations = null;
69
70
        Relation::query()
71
            ->where('parent_type', $this->getMorphClass())
72
            ->where('parent_id', $this->getKey())
73
            ->where('child_type', $child->getMorphClass())
74
            ->where('child_id', $child->getKey())
75
            ->update(['sort' => $sort]);
76
    }
77
78
    public function detachAllChildRelations()
79
    {
80
        Relation::deleteAllChildRelationsOf($this->getMorphClass(), $this->getKey());
81
    }
82
83
    private function attachChild($child_type, $child_id, array $attributes = [])
84
    {
85
        Relation::firstOrCreate([
86
            'parent_type' => $this->getMorphClass(),
87
            'parent_id'   => $this->getKey(),
88
            'child_type'  => $child_type,
89
            'child_id'    => $child_id,
90
        ], $attributes);
91
    }
92
93
    private function detachChild($child_type, $child_id)
94
    {
95
        Relation::query()
96
            ->where('parent_type', $this->getMorphClass())
97
            ->where('parent_id', $this->getKey())
98
            ->where('child_type', $child_type)
99
            ->where('child_id', $child_id)
100
            ->delete();
101
    }
102
103
    private function areChildRelationsLoaded(): bool
104
    {
105
        return !is_null($this->loadedChildRelations);
106
    }
107
108
    public function breakChildRelationCache()
109
    {
110
        $this->loadedChildRelations = null;
111
    }
112
}
113