2021-10-08 16:23:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2024-01-12 14:41:02 +00:00
|
|
|
use App\Facades\License;
|
2021-10-08 16:23:45 +00:00
|
|
|
use App\Models\Playlist;
|
2022-11-27 15:29:29 +00:00
|
|
|
use App\Models\PlaylistFolder as Folder;
|
2021-10-08 16:23:45 +00:00
|
|
|
use App\Models\User;
|
2022-11-27 15:29:29 +00:00
|
|
|
use App\Values\SmartPlaylistRuleGroupCollection;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2024-01-12 14:41:02 +00:00
|
|
|
use InvalidArgumentException;
|
2022-11-27 15:29:29 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2021-10-08 16:23:45 +00:00
|
|
|
|
|
|
|
class PlaylistService
|
|
|
|
{
|
2022-11-27 15:29:29 +00:00
|
|
|
public function createPlaylist(
|
|
|
|
string $name,
|
|
|
|
User $user,
|
|
|
|
?Folder $folder = null,
|
|
|
|
array $songs = [],
|
2024-01-12 14:41:02 +00:00
|
|
|
?SmartPlaylistRuleGroupCollection $ruleGroups = null,
|
|
|
|
bool $ownSongsOnly = false
|
2022-11-27 15:29:29 +00:00
|
|
|
): Playlist {
|
|
|
|
if ($folder) {
|
|
|
|
Assert::true($user->is($folder->user), 'The playlist folder does not belong to the user');
|
|
|
|
}
|
2021-10-08 16:23:45 +00:00
|
|
|
|
2024-01-12 14:41:02 +00:00
|
|
|
throw_if($ownSongsOnly && (!$ruleGroups || !License::isPlus()), new InvalidArgumentException(
|
|
|
|
'"Own songs only" option only works with smart playlists and Plus license.'
|
|
|
|
));
|
2021-10-08 16:23:45 +00:00
|
|
|
|
2024-01-12 14:41:02 +00:00
|
|
|
return DB::transaction(
|
|
|
|
static function () use ($name, $user, $songs, $folder, $ruleGroups, $ownSongsOnly): Playlist {
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = $user->playlists()->create([
|
|
|
|
'name' => $name,
|
|
|
|
'rules' => $ruleGroups,
|
|
|
|
'own_songs_only' => $ownSongsOnly,
|
|
|
|
'folder_id' => $folder?->id,
|
|
|
|
]);
|
2022-11-27 15:29:29 +00:00
|
|
|
|
2024-01-12 14:41:02 +00:00
|
|
|
if (!$playlist->is_smart && $songs) {
|
|
|
|
$playlist->songs()->sync($songs);
|
|
|
|
}
|
2022-11-27 15:29:29 +00:00
|
|
|
|
2024-01-12 14:41:02 +00:00
|
|
|
return $playlist;
|
2022-11-27 15:29:29 +00:00
|
|
|
}
|
2024-01-12 14:41:02 +00:00
|
|
|
);
|
2021-10-08 16:23:45 +00:00
|
|
|
}
|
2022-07-04 10:39:02 +00:00
|
|
|
|
2022-11-27 15:29:29 +00:00
|
|
|
public function updatePlaylist(
|
|
|
|
Playlist $playlist,
|
|
|
|
string $name,
|
|
|
|
?Folder $folder = null,
|
|
|
|
?SmartPlaylistRuleGroupCollection $ruleGroups = null,
|
2024-01-12 14:41:02 +00:00
|
|
|
bool $ownSongsOnly = false
|
2022-11-27 15:29:29 +00:00
|
|
|
): Playlist {
|
|
|
|
if ($folder) {
|
|
|
|
Assert::true($playlist->user->is($folder->user), 'The playlist folder does not belong to the user');
|
|
|
|
}
|
|
|
|
|
2024-01-12 14:41:02 +00:00
|
|
|
throw_if($ownSongsOnly && (!$playlist->is_smart || !License::isPlus()), new InvalidArgumentException(
|
|
|
|
'"Own songs only" option only works with smart playlists and Plus license.'
|
|
|
|
));
|
|
|
|
|
2022-08-10 14:56:01 +00:00
|
|
|
$playlist->update([
|
|
|
|
'name' => $name,
|
2022-11-27 15:29:29 +00:00
|
|
|
'rules' => $ruleGroups,
|
|
|
|
'folder_id' => $folder?->id,
|
2024-01-12 14:41:02 +00:00
|
|
|
'own_songs_only' => $ownSongsOnly,
|
2022-08-10 14:56:01 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
return $playlist;
|
|
|
|
}
|
|
|
|
|
2022-07-04 10:39:02 +00:00
|
|
|
public function addSongsToPlaylist(Playlist $playlist, array $songIds): void
|
|
|
|
{
|
|
|
|
$playlist->songs()->syncWithoutDetaching($songIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function removeSongsFromPlaylist(Playlist $playlist, array $songIds): void
|
|
|
|
{
|
|
|
|
$playlist->songs()->detach($songIds);
|
|
|
|
}
|
2021-10-08 16:23:45 +00:00
|
|
|
}
|