koel/app/Services/InteractionService.php

117 lines
3.1 KiB
PHP
Raw Normal View History

2018-08-18 12:27:26 +00:00
<?php
namespace App\Services;
use App\Events\SongLikeToggled;
use App\Models\Interaction;
use App\Models\User;
2018-08-22 20:25:01 +00:00
use Illuminate\Support\Collection;
2018-08-18 12:27:26 +00:00
class InteractionService
{
private $interaction;
public function __construct(Interaction $interaction)
{
$this->interaction = $interaction;
}
/**
* Increase the number of times a song is played by a user.
*
* @return Interaction The affected Interaction object
*/
2018-08-24 15:27:19 +00:00
public function increasePlayCount(string $songId, User $user): Interaction
2018-08-18 12:27:26 +00:00
{
return tap($this->interaction->firstOrCreate([
'song_id' => $songId,
'user_id' => $user->id,
2018-08-24 15:27:19 +00:00
]), static function (Interaction $interaction): void {
2018-08-18 12:27:26 +00:00
if (!$interaction->exists) {
$interaction->liked = false;
}
$interaction->play_count++;
$interaction->save();
});
}
/**
* Like or unlike a song on behalf of a user.
*
* @return Interaction The affected Interaction object.
*/
2018-08-24 15:27:19 +00:00
public function toggleLike(string $songId, User $user): Interaction
2018-08-18 12:27:26 +00:00
{
return tap($this->interaction->firstOrCreate([
'song_id' => $songId,
'user_id' => $user->id,
2018-08-24 15:27:19 +00:00
]), static function (Interaction $interaction): void {
2018-08-18 12:27:26 +00:00
$interaction->liked = !$interaction->liked;
$interaction->save();
event(new SongLikeToggled($interaction));
});
}
/**
* Like several songs at once as a user.
*
2018-08-24 15:27:19 +00:00
* @param string[] $songIds
2018-08-18 12:27:26 +00:00
*
2018-08-24 15:27:19 +00:00
* @return Interaction[] The array of Interaction objects.
2018-08-18 12:27:26 +00:00
*/
2018-08-24 15:27:19 +00:00
public function batchLike(array $songIds, User $user): array
2018-08-18 12:27:26 +00:00
{
2018-08-24 15:27:19 +00:00
return collect($songIds)->map(function ($songId) use ($user): Interaction {
2018-08-18 12:27:26 +00:00
return tap($this->interaction->firstOrCreate([
'song_id' => $songId,
'user_id' => $user->id,
2018-08-24 15:27:19 +00:00
]), static function (Interaction $interaction): void {
2018-08-18 12:27:26 +00:00
if (!$interaction->exists) {
$interaction->play_count = 0;
}
$interaction->liked = true;
$interaction->save();
event(new SongLikeToggled($interaction));
});
})->all();
}
/**
* Unlike several songs at once.
*
2018-08-24 15:27:19 +00:00
* @param string[] $songIds
2018-08-18 12:27:26 +00:00
*/
2018-08-24 15:27:19 +00:00
public function batchUnlike(array $songIds, User $user): void
2018-08-18 12:27:26 +00:00
{
$this->interaction
->whereIn('song_id', $songIds)
->where('user_id', $user->id)
->get()
2018-08-24 15:27:19 +00:00
->each(static function (Interaction $interaction): void {
2018-08-18 12:27:26 +00:00
$interaction->liked = false;
$interaction->save();
event(new SongLikeToggled($interaction));
}
);
}
2018-08-22 20:25:01 +00:00
/**
* Get all songs favorited by a user.
*/
2018-08-24 15:27:19 +00:00
public function getUserFavorites(User $user): Collection
2018-08-22 20:25:01 +00:00
{
return $this->interaction->where([
'user_id' => $user->id,
2018-08-22 20:25:23 +00:00
'like' => true,
2018-08-22 20:25:01 +00:00
])
->with('song')
->get()
->pluck('song');
}
2018-08-18 12:27:26 +00:00
}