Move User dependency out of Song scrobble method

This commit is contained in:
Phan An 2017-04-29 10:38:17 +08:00
parent 328dc88c99
commit 47c36f289e
No known key found for this signature in database
GPG key ID: 4AF3D4E287BF423F
2 changed files with 10 additions and 7 deletions

View file

@ -2,6 +2,7 @@
namespace App\Http\Controllers\API;
use App\Http\Requests\API\Request;
use App\Models\Song;
class ScrobbleController extends Controller
@ -9,13 +10,14 @@ class ScrobbleController extends Controller
/**
* Create a Last.fm scrobble entry for a song.
*
* @param Song $song
* @param string $timestamp The UNIX timestamp when the song started playing.
* @param Request $request
* @param Song $song
* @param string $timestamp The UNIX timestamp when the song started playing.
*
* @return \Illuminate\Http\JsonResponse
*/
public function store(Song $song, $timestamp)
public function store(Request $request, Song $song, $timestamp)
{
return response()->json($song->scrobble($timestamp));
return response()->json($song->scrobble($request->user(), $timestamp));
}
}

View file

@ -74,11 +74,12 @@ class Song extends Model
/**
* Scrobble the song using Last.fm service.
*
* @param User $user
* @param string $timestamp The UNIX timestamp in which the song started playing.
*
* @return mixed
*/
public function scrobble($timestamp)
public function scrobble(User $user, $timestamp)
{
// Don't scrobble the unknown guys. No one knows them.
if ($this->artist->isUnknown()) {
@ -86,7 +87,7 @@ class Song extends Model
}
// If the current user hasn't connected to Last.fm, don't do shit.
if (!$sessionKey = auth()->user()->lastfm_session_key) {
if (!$user->lastfm_session_key) {
return false;
}
@ -95,7 +96,7 @@ class Song extends Model
$this->title,
$timestamp,
$this->album->name === Album::UNKNOWN_NAME ? '' : $this->album->name,
$sessionKey
$user->lastfm_session_key
);
}