1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Seongbae\Discuss\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Auth; |
7
|
|
|
|
8
|
|
|
class Channel extends Model |
9
|
|
|
{ |
10
|
|
|
protected $fillable = [ |
11
|
|
|
'name', |
12
|
|
|
'slug' |
13
|
|
|
]; |
14
|
|
|
|
15
|
|
|
public $timestamps = false; |
16
|
|
|
|
17
|
|
|
public function getCssClassesAttribute() |
18
|
|
|
{ |
19
|
|
|
$classes = config('discuss.channel_classes'); |
20
|
|
|
|
21
|
|
|
if (array_key_exists($this->slug, $classes)) |
22
|
|
|
return 'btn '.$classes[$this->slug]; |
23
|
|
|
else |
24
|
|
|
return 'btn btn-outline-primary btn-sm '; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function subscriptions() |
28
|
|
|
{ |
29
|
|
|
return $this->hasMany(Subscription::class, 'subscribable_id')->where('subscribable_type', Thread::class); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function subscribers() |
33
|
|
|
{ |
34
|
|
|
return $this->morphedByMany(config('discuss.user_type'), 'user', 'discuss_subscription', 'subscribable_id'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function subscribersExcept() |
38
|
|
|
{ |
39
|
|
|
return $this->subscribers()->where('user_id', '<>', Auth::id()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
View Code Duplication |
public function attachSubscriber($user) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
if (!Subscription::where('user_id', $user->id) |
45
|
|
|
->where('subscribable_type', Channel::class) |
46
|
|
|
->where('subscribable_id', $this->id) |
47
|
|
|
->exists()) |
48
|
|
|
Subscription::create(['user_id'=>$user->id, 'user_type'=>config('discuss.user_type'), 'subscribable_type'=>Channel::class, 'subscribable_id'=>$this->id]); |
49
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function detachSubscriber($user) |
53
|
|
|
{ |
54
|
|
|
$subscription = Subscription::where('user_id', $user->id)->where('subscribable_type', Channel::class)->where('subscribable_id', $this->id); |
55
|
|
|
|
56
|
|
|
if ($subscription) |
57
|
|
|
$subscription->delete(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function resolveChildRouteBinding($childType, $value, $field) |
61
|
|
|
{ |
62
|
|
|
// TODO: Implement resolveChildRouteBinding() method. |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getRouteKeyName() |
66
|
|
|
{ |
67
|
|
|
return 'slug'; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
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.