Passed
Push — master ( 3b518a...cd2a02 )
by Adam
11:23
created

Guide   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 14

12 Methods

Rating   Name   Duplication   Size   Complexity  
A assets() 0 3 1
A voters() 0 3 1
A loadUserVoterRelation() 0 7 2
A tags() 0 3 1
A page() 0 3 1
A user() 0 3 1
A loadUserRoleRelation() 0 7 2
A getSlugAttribute() 0 3 1
A roles() 0 3 1
A commentsWithChildren() 0 13 1
A comments() 0 3 1
A subscribers() 0 3 1
1
<?php
2
3
namespace Coyote;
4
5
use Coyote\Guide\Role;
6
use Coyote\Guide\Vote;
7
use Coyote\Models\Asset;
8
use Coyote\Models\Subscription;
9
use Illuminate\Database\Eloquent\Model;
10
11
/**
12
 * @property int $id
13
 * @property User $user
14
 * @property string $title
15
 * @property string $excerpt
16
 * @property string $text
17
 * @property Tag[] $tags
18
 * @property int $user_id
19
 * @property Comment[] $comments
20
 * @property Comment[] $commentsWithChildren
21
 * @property string $slug
22
 * @property int $votes
23
 * @property string $role
24
 * @property Role[] $roles
25
 */
26
class Guide extends Model
27
{
28
    use Taggable;
29
30
    protected $fillable = ['title', 'excerpt', 'text'];
31
32
    public function getSlugAttribute(): string
33
    {
34
        return str_slug($this->title, '_');
35
    }
36
37
    /**
38
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
39
     */
40
    public function voters()
41
    {
42
        return $this->hasMany(Vote::class);
43
    }
44
45
    public function user()
46
    {
47
        return $this->belongsTo(User::class);
48
    }
49
50
    public function subscribers()
51
    {
52
        return $this->morphMany(Subscription::class, 'resource');
53
    }
54
55
    /**
56
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
57
     */
58
    public function page()
59
    {
60
        return $this->morphOne(Page::class, 'content');
61
    }
62
63
    /**
64
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
65
     */
66
    public function tags()
67
    {
68
        return $this->morphToMany(Tag::class, 'resource', 'tag_resources');
69
    }
70
71
    public function comments()
72
    {
73
        return $this->morphMany(Comment::class, 'resource');
74
    }
75
76
    public function roles()
77
    {
78
        return $this->hasMany(Role::class);
79
    }
80
81
    /**
82
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
83
     */
84
    public function assets()
85
    {
86
        return $this->morphMany(Asset::class, 'content');
87
    }
88
89
    public function commentsWithChildren()
90
    {
91
        $userRelation = fn ($builder) => $builder->select(['id', 'name', 'photo', 'deleted_at', 'is_blocked', 'is_online'])->withTrashed();
92
93
        return $this
94
            ->comments()
95
            ->whereNull('parent_id')
96
            ->orderBy('id', 'DESC')
97
            ->with([
98
                'children' => function ($builder) use ($userRelation) {
99
                    return $builder->orderBy('id')->with(['user' => $userRelation]);
100
                },
101
                'user' => $userRelation
102
            ]);
103
    }
104
105
    public function loadUserVoterRelation(?int $userId): void
106
    {
107
        if (!$userId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $userId of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
108
            return;
109
        }
110
111
        $this->load(['voters' => fn ($builder) => $builder->select(['id', 'guide_id', 'user_id'])->where('user_id', $userId)]);
112
    }
113
114
    public function loadUserRoleRelation(?int $userId): void
115
    {
116
        if (!$userId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $userId of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
117
            return;
118
        }
119
120
        $this->load(['roles' => fn ($builder) => $builder->select(['id', 'guide_id', 'role'])->where('user_id', $userId)]);
121
    }
122
}
123