Thread::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
namespace Seongbae\Discuss\Models;
3
4
use Illuminate\Database\Eloquent\Model;
5
use Seongbae\Discuss\Events\NewReply;
6
use Seongbae\Discuss\Events\NewThread;
7
use Auth;
8
9
class Thread extends Model
10
{
11
	/**
12
     * The attributes that are mass assignable.
13
     *
14
     * @var array
15
     */
16
    protected $fillable = [
17
        'user_id',
18
        'user_type',
19
        'channel_id',
20
        'title',
21
        'body',
22
        'view_count',
23
        'slug'
24
    ];
25
26
    protected $guarded = [];
27
28
    protected $with = ['user', 'channel'];
29
30
    protected $appends = ['created_at_human_readable'];
31
32
    /**
33
     * Fetch a path to the current thread.
34
     *
35
     * @return string
36
     */
37
    public function path()
38
    {
39
        return '/discuss/' . $this->channel->slug . '/'.$this->slug;
40
    }
41
42
    public function getCreatedAtHumanReadableAttribute()
43
    {
44
        return $this->created_at->diffForHumans();
45
    }
46
47
    /**
48
     * A thread belongs to a creator.
49
     *
50
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
51
     */
52
    public function user()
53
    {
54
        return $this->morphTo();
55
    }
56
57
    public function subscriptions()
58
    {
59
        return $this->hasMany(Subscription::class, 'subscribable_id')->where('subscribable_type', Thread::class);
60
    }
61
62
    public function subscribers()
63
    {
64
        return $this->morphedByMany(config('discuss.user_type'), 'user', 'discuss_subscription', 'subscribable_id');
65
    }
66
67
    public function subscribersExcept()
68
    {
69
        return $this->subscribers()->where('user_id', '<>', Auth::id());
70
    }
71
72
    public function channel()
73
    {
74
        return $this->belongsTo(Channel::class);
75
    }
76
77
    public function replies()
78
    {
79
        return $this->hasMany(Reply::class);
80
    }
81
82
    /**
83
     * Add a reply to the thread.
84
     *
85
     * @param $reply
86
     */
87
    public function addReply($reply, $subscribe=true)
88
    {
89
        $reply = $this->replies()->create($reply);
90
91
        event(new NewReply($reply));
92
93
        if ($subscribe)
94
            $this->attachSubscriber($reply->user);
95
    }
96
97 View Code Duplication
    public function attachSubscriber($user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        if (!Subscription::where('user_id', $user->id)
100
            ->where('subscribable_type', Thread::class)
101
            ->where('subscribable_id', $this->id)
102
            ->exists())
103
            Subscription::create(['user_id'=>$user->id, 'user_type'=>config('discuss.user_type'), 'subscribable_type'=>Thread::class, 'subscribable_id'=>$this->id]);
104
    }
105
106
    public function detachSubscriber($user)
107
    {
108
        $subscription = Subscription::where('user_id', $user->id)->where('subscribable_type', Thread::class)->where('subscribable_id', $this->id)->first();
109
110
        if ($subscription)
111
            $subscription->delete();
112
    }
113
114
    public function getRouteKeyName()
115
    {
116
        return 'slug';
117
    }
118
119
    protected static function boot()
120
    {
121
        parent::boot();
122
123
        static::addGlobalScope('replyCount', function($builder) {
124
            $builder->withCount('replies');
125
        });
126
127
        static::created(function ($thread) {
128
            event(new NewThread($thread));
129
        });
130
131
        static::deleting(function ($thread) {
132
            $thread->replies()->delete();
133
        });
134
    }
135
}
136