1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use App\Events\SongLikeToggled; |
6
|
|
|
use App\Traits\CanFilterByUser; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @property bool liked |
11
|
|
|
* @property int play_count |
12
|
|
|
* @property Song song |
13
|
|
|
* @property User user |
14
|
|
|
*/ |
15
|
|
|
class Interaction extends Model |
16
|
|
|
{ |
17
|
|
|
use CanFilterByUser; |
18
|
|
|
|
19
|
|
|
protected $casts = [ |
20
|
|
|
'liked' => 'boolean', |
21
|
|
|
'play_count' => 'integer', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
protected $guarded = ['id']; |
25
|
|
|
|
26
|
|
|
protected $hidden = ['id', 'user_id', 'created_at', 'updated_at']; |
27
|
|
|
|
28
|
|
|
public function user() |
29
|
|
|
{ |
30
|
|
|
return $this->belongsTo(User::class); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function song() |
34
|
|
|
{ |
35
|
|
|
return $this->belongsTo(Song::class); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Increase the number of times a song is played by a user. |
40
|
|
|
* |
41
|
|
|
* @param string $songId |
42
|
|
|
* @param User $user |
43
|
|
|
* |
44
|
|
|
* @return Interaction |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public static function increasePlayCount($songId, User $user) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
return tap(self::firstOrCreate([ |
49
|
|
|
'song_id' => $songId, |
50
|
|
|
'user_id' => $user->id, |
51
|
|
|
]), function (Interaction $interaction) { |
52
|
|
|
if (!$interaction->exists) { |
53
|
|
|
$interaction->liked = false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
++$interaction->play_count; |
57
|
|
|
$interaction->save(); |
58
|
|
|
}); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Like or unlike a song on behalf of a user. |
63
|
|
|
* |
64
|
|
|
* @param string $songId |
65
|
|
|
* @param User $user |
66
|
|
|
* |
67
|
|
|
* @return Interaction |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public static function toggleLike($songId, User $user) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
return tap(self::firstOrCreate([ |
72
|
|
|
'song_id' => $songId, |
73
|
|
|
'user_id' => $user->id, |
74
|
|
|
]), function (Interaction $interaction) { |
75
|
|
|
$interaction->liked = !$interaction->liked; |
76
|
|
|
$interaction->save(); |
77
|
|
|
|
78
|
|
|
event(new SongLikeToggled($interaction)); |
79
|
|
|
}); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Like several songs at once. |
84
|
|
|
* |
85
|
|
|
* @param array $songIds |
86
|
|
|
* @param User $user |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public static function batchLike(array $songIds, User $user) |
91
|
|
|
{ |
92
|
|
|
return collect($songIds)->map(function ($songId) use ($user) { |
93
|
|
|
return tap(self::firstOrCreate([ |
94
|
|
|
'song_id' => $songId, |
95
|
|
|
'user_id' => $user->id, |
96
|
|
|
]), function (Interaction $interaction) { |
97
|
|
|
if (!$interaction->exists) { |
98
|
|
|
$interaction->play_count = 0; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$interaction->liked = true; |
102
|
|
|
$interaction->save(); |
103
|
|
|
|
104
|
|
|
event(new SongLikeToggled($interaction)); |
105
|
|
|
}); |
106
|
|
|
})->all(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Unlike several songs at once. |
111
|
|
|
* |
112
|
|
|
* @param array $songIds |
113
|
|
|
* @param User $user |
114
|
|
|
* |
115
|
|
|
* @return int |
116
|
|
|
*/ |
117
|
|
|
public static function batchUnlike(array $songIds, User $user) |
118
|
|
|
{ |
119
|
|
|
self::whereIn('song_id', $songIds)->whereUserId($user->id)->get()->each(function (Interaction $interaction) { |
120
|
|
|
$interaction->liked = false; |
121
|
|
|
$interaction->save(); |
122
|
|
|
|
123
|
|
|
event(new SongLikeToggled($interaction)); |
124
|
|
|
}); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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.