is($folder->user), 'The playlist folder does not belong to the user'); } throw_if($ownSongsOnly && (!$ruleGroups || !License::isPlus()), new InvalidArgumentException( '"Own songs only" option only works with smart playlists and Plus license.' )); 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, ]); if (!$playlist->is_smart && $songs) { $playlist->songs()->sync($songs); } return $playlist; } ); } public function updatePlaylist( Playlist $playlist, string $name, ?Folder $folder = null, ?SmartPlaylistRuleGroupCollection $ruleGroups = null, bool $ownSongsOnly = false ): Playlist { if ($folder) { Assert::true($playlist->user->is($folder->user), 'The playlist folder does not belong to the user'); } throw_if($ownSongsOnly && (!$playlist->is_smart || !License::isPlus()), new InvalidArgumentException( '"Own songs only" option only works with smart playlists and Plus license.' )); $playlist->update([ 'name' => $name, 'rules' => $ruleGroups, 'folder_id' => $folder?->id, 'own_songs_only' => $ownSongsOnly, ]); return $playlist; } public function addSongsToPlaylist(Playlist $playlist, array $songIds): void { $playlist->songs()->syncWithoutDetaching($songIds); } public function removeSongsFromPlaylist(Playlist $playlist, array $songIds): void { $playlist->songs()->detach($songIds); } }