Test Setup Failed
Push — master ( 5d739f...e22c69 )
by Ben
06:33
created

Relation   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Test Coverage

Coverage 84.13%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 16
eloc 65
c 3
b 0
f 0
dl 0
loc 142
ccs 53
cts 63
cp 0.8413
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A children() 0 25 2
A parents() 0 12 1
A parent() 0 3 1
A delete() 0 7 1
A isOnline() 0 4 1
A find() 0 8 1
A deleteAllParentRelationsOf() 0 9 2
A deleteAllChildRelationsOf() 0 9 2
A child() 0 3 1
A setKeysForSaveQuery() 0 8 1
A isOffline() 0 3 1
A deleteRelationsOf() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Thinktomorrow\Chief\Relations;
6
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\Model;
9
10
/**
11
 * @property string $child_type
12
 * @property string $child_id
13
 * @property string $parent_type
14
 * @property string $parent_id
15
 */
16
class Relation extends Model
17
{
18
    public $timestamps = false;
19
    public $guarded = [];
20
21
    public static function find(string $parent_type, $parent_id, string $child_type, $child_id): ?Relation
22
    {
23
        return static::query()
24
            ->where('parent_type', $parent_type )
25
            ->where('parent_id', $parent_id )
26
            ->where('child_type', $child_type )
27
            ->where('child_id', $child_id )
28
            ->first();
29
    }
30
31
    public function isOnline(): bool
32 7
    {
33
        // Default is online, except explicitly set offline
34 7
        return ($this->online_status != 0);
35
    }
36
37 34
    public function isOffline(): bool
38
    {
39 34
        return !$this->isOnline();
40
    }
41
42 7
    /**
43
     * Set the keys for a save update query.
44 7
     * We override the default save setup since we do not have a primary key in relation table.
45 7
     * There should however always be the parent and child references so we can use
46 7
     * those to target the record in database.
47 7
     *
48
     * @param Builder $query
49
     * @return Builder
50 6
     */
51 6
    protected function setKeysForSaveQuery(Builder $query)
52 6
    {
53 7
        $query->where('parent_type', $this->parent_type)
54
            ->where('parent_id', $this->parent_id)
55
            ->where('child_type', $this->child_type)
56 65
            ->where('child_id', $this->child_id);
57
58 65
        return $query;
59 65
    }
60 65
61 65
    public function parent()
62
    {
63
        return $this->morphTo('parent', 'parent_type', 'parent_id');
64
    }
65
66 33
    public function child()
67
    {
68
        return $this->morphTo('child', 'child_type', 'child_id');
69
    }
70
71
    public static function parents($child_type, $child_id)
72
    {
73
        $relations = static::where('child_type', $child_type)
74
            ->where('child_id', $child_id)
75
            ->orderBy('sort', 'ASC')
76 33
            ->get();
77
78 33
        return $relations->map(function (Relation $relation) {
79 65
            $parent = $relation->parent;
0 ignored issues
show
Bug introduced by
The property parent does not exist on Thinktomorrow\Chief\Relations\Relation. Did you mean parent_id?
Loading history...
80
            $parent->relation = $relation;
81
82
            return $parent;
83 33
        });
84 65
    }
85 65
86
    public static function children($parent_type, $parent_id)
87
    {
88 2
        $relations = static::where('parent_type', $parent_type)
89
            ->where('parent_id', $parent_id)
90 2
            ->with('child')
91 2
            ->orderBy('sort', 'ASC')
92 2
            ->get();
93 2
94 2
        return $relations->map(function (Relation $relation) use ($parent_type, $parent_id) {
0 ignored issues
show
Unused Code introduced by
The import $parent_id is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
Unused Code introduced by
The import $parent_type is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
95
96
            // It could be that the child itself is archived, soft-deleted or removed. If this is the case, we will ignore it and move on.
97 2
            if (!$child = $relation->child) {
0 ignored issues
show
Bug introduced by
The property child does not exist on Thinktomorrow\Chief\Relations\Relation. Did you mean child_id?
Loading history...
98
                return null;
99
            }
100 2
101 2
            $child->relation = $relation;
102
103 2
            return $child;
104 2
        })
105 2
106
            // In case of soft-deleted entries, this will be null and should be ignored. We make sure that keys are reset in case of removed child
107 2
            ->reject(function ($child) {
108 1
                return is_null($child);
109
            })
110 2
            ->values();
111
    }
112 2
113
    public function delete()
114
    {
115 2
        return static::where('parent_type', $this->parent_type)
116 2
            ->where('parent_id', $this->parent_id)
117 2
            ->where('child_type', $this->child_type)
118
            ->where('child_id', $this->child_id)
119 2
            ->delete();
120 1
    }
121
122 2
    public static function deleteRelationsOf($type, $id)
123
    {
124 2
        $relations = static::where(function ($query) use ($type, $id) {
125
            return $query->where('parent_type', $type)
126
                ->where('parent_id', $id);
127 2
        })->orWhere(function ($query) use ($type, $id) {
128 2
            return $query->where('child_type', $type)
129 2
                ->where('child_id', $id);
130
        })->get();
131 2
132
        foreach ($relations as $relation) {
133
            $relation->delete();
134 2
        }
135
    }
136
137
    public static function deleteAllChildRelationsOf($type, $id)
138
    {
139
        $relations = static::where(function ($query) use ($type, $id) {
140
            return $query->where('parent_type', $type)
141
                ->where('parent_id', $id);
142
        })->get();
143
144
        foreach ($relations as $relation) {
145
            $relation->delete();
146
        }
147
    }
148
149
    public static function deleteAllParentRelationsOf($type, $id)
150
    {
151
        $relations = static::where(function ($query) use ($type, $id) {
152
            return $query->where('child_type', $type)
153
                ->where('child_id', $id);
154
        })->get();
155
156
        foreach ($relations as $relation) {
157
            $relation->delete();
158
        }
159
    }
160
}
161