2016-12-11 13:08:30 +00:00
|
|
|
<?php
|
|
|
|
|
2020-09-07 20:43:23 +00:00
|
|
|
namespace App\Http\Controllers;
|
2016-12-11 13:08:30 +00:00
|
|
|
|
2017-12-09 18:34:27 +00:00
|
|
|
use App\Http\Requests\API\ViewSongOnITunesRequest;
|
2016-12-11 13:08:30 +00:00
|
|
|
use App\Models\Album;
|
2020-12-22 20:11:22 +00:00
|
|
|
use App\Services\ITunesService;
|
2020-09-07 20:43:23 +00:00
|
|
|
use App\Services\TokenManager;
|
|
|
|
use Illuminate\Http\Response;
|
2016-12-11 13:08:30 +00:00
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
class ITunesController extends Controller
|
2016-12-11 13:08:30 +00:00
|
|
|
{
|
2022-07-29 06:47:10 +00:00
|
|
|
public function __construct(private ITunesService $iTunesService, private TokenManager $tokenManager)
|
2018-08-19 14:40:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-09 18:34:27 +00:00
|
|
|
public function viewSong(ViewSongOnITunesRequest $request, Album $album)
|
2016-12-11 13:08:30 +00:00
|
|
|
{
|
2020-09-07 20:43:23 +00:00
|
|
|
abort_unless(
|
|
|
|
(bool) $this->tokenManager->getUserFromPlainTextToken($request->api_token),
|
|
|
|
Response::HTTP_UNAUTHORIZED
|
|
|
|
);
|
|
|
|
|
2018-08-19 14:40:25 +00:00
|
|
|
$url = $this->iTunesService->getTrackUrl($request->q, $album->name, $album->artist->name);
|
2021-06-05 10:47:56 +00:00
|
|
|
abort_unless((bool) $url, 404, "Koel can't find such a song on iTunes Store.");
|
2017-04-29 02:55:41 +00:00
|
|
|
|
|
|
|
return redirect($url);
|
2016-12-11 13:08:30 +00:00
|
|
|
}
|
|
|
|
}
|