Skip to content
Snippets Groups Projects
Commit d14111b3 authored by Benoît Harrault's avatar Benoît Harrault
Browse files

Merge branch '59-catch-spotifywebapiexception' into 'master'

Resolve "Catch SpotifyWebAPIException"

Closes #59

See merge request !53
parents 280ba111 4587f67b
No related branches found
No related tags found
1 merge request!53Resolve "Catch SpotifyWebAPIException"
This diff is collapsed.
......@@ -4,15 +4,14 @@ namespace App\Controller;
use App\Service\SpotifyApiService;
use App\Service\SpotifyPlaylistService;
use SpotifyWebAPI\SpotifyWebAPI;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ArtistController extends AbstractController
{
/** @var SpotifyWebAPI */
private $spotifyApi;
private SpotifyWebAPI $spotifyApi;
public function __construct(SpotifyApiService $spotifyApiService)
{
......@@ -24,10 +23,8 @@ class ArtistController extends AbstractController
/**
* @Route("/artists/top", name="get-artists-top")
*
* @return Response
*/
public function getArtists(): Response
public function getArtists(): JsonResponse
{
$rawTopArtists = $this->spotifyApi->getMyTop(
'artists',
......
......@@ -23,8 +23,6 @@ class AuthenticationController extends AbstractController
/**
* @Route("/auth", name="authenticate")
*
* @return RedirectResponse
*/
public function authenticate(): RedirectResponse
{
......@@ -46,8 +44,6 @@ class AuthenticationController extends AbstractController
/**
* @Route("/callback", name="callback")
*
* @return RedirectResponse
*/
public function callback(Request $request): RedirectResponse
{
......@@ -77,8 +73,6 @@ class AuthenticationController extends AbstractController
/**
* @Route("/logout", name="logout")
*
* @return RedirectResponse
*/
public function logout(): RedirectResponse
{
......
......@@ -4,17 +4,14 @@ namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
{
/**
* @Route("/", name="homepage")
*
* @return Response
*/
public function index(): Response
public function index(): RedirectResponse
{
return new RedirectResponse($this->generateUrl('playlist-generator'));
}
......
......@@ -3,6 +3,8 @@
namespace App\Controller;
use App\Service\SpotifyApiService;
use SpotifyWebAPI\SpotifyWebAPI;
use SpotifyWebAPI\SpotifyWebAPIException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
......@@ -10,8 +12,7 @@ use Symfony\Component\Routing\Annotation\Route;
class NowPlayingController extends AbstractController
{
/** @var SpotifyWebAPI */
private $spotifyApi;
private SpotifyWebAPI $spotifyApi;
public function __construct(SpotifyApiService $spotifyApiService)
{
......@@ -20,20 +21,22 @@ class NowPlayingController extends AbstractController
/**
* @Route("/now-playing", name="now-playing")
*
* @return Response
*/
public function index(): Response
{
try {
$me = $this->spotifyApi->me();
} catch (SpotifyWebAPIException $e) {
return $this->render('error.html.twig');
}
return $this->render('now-playing/index.html.twig', [
'user' => $this->spotifyApi->me()
'user' => $me,
]);
}
/**
* @Route("/now-playing/data", name="now-playing-data")
*
* @return JsonResponse
*/
public function update(): JsonResponse
{
......
......@@ -4,6 +4,8 @@ namespace App\Controller;
use App\Service\SpotifyApiService;
use App\Service\SpotifyPlaylistService;
use SpotifyWebAPI\SpotifyWebAPI;
use SpotifyWebAPI\SpotifyWebAPIException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
......@@ -13,11 +15,8 @@ use Symfony\Component\Routing\Annotation\Route;
class PlaylistController extends AbstractController
{
/** @var SpotifyWebAPI */
private $spotifyApi;
/** @var SpotifyPlaylistService */
private $spotifyPlaylistService;
private SpotifyWebAPI $spotifyApi;
private SpotifyPlaylistService $spotifyPlaylistService;
public function __construct(SpotifyApiService $spotifyApiService, SpotifyPlaylistService $spotifyPlaylistService)
{
......@@ -30,12 +29,14 @@ class PlaylistController extends AbstractController
/**
* @Route("/playlist-generator", name="playlist-generator")
*
* @return Response
*/
public function playlistGeneratorIndex(): Response
{
$me = $this->spotifyApi->me();
try {
$me = $this->spotifyApi->me();
} catch (SpotifyWebAPIException $e) {
return $this->render('error.html.twig');
}
return $this->render('playlist-generator/index.html.twig', [
'user' => $me,
......@@ -44,12 +45,11 @@ class PlaylistController extends AbstractController
/**
* @Route("/playlists/user", name="get-user-playlists")
*
* @return Response
*/
public function getPlaylists(): Response
public function getPlaylists(): JsonResponse
{
$me = $this->spotifyApi->me();
$rawPlaylists = $this->spotifyApi->getUserPlaylists(
$me->id,
[
......@@ -71,8 +71,6 @@ class PlaylistController extends AbstractController
/**
* @Route("/show-playlist-content/{playlistId}", name="show-playlist-content")
*e
* @return Response
*/
public function showPlaylistContent(string $playlistId): Response
{
......@@ -88,10 +86,8 @@ class PlaylistController extends AbstractController
/**
* @Route("/generate-playlist-from-playlists", name="generate-playlist-from-playlists")
*
* @return Response
*/
public function generatePlaylistFromPlaylists(Request $request): Response
public function generatePlaylistFromPlaylists(Request $request): RedirectResponse
{
/** @var array */
$selectedPlaylists = $request->query->get('selected-playlist');
......@@ -122,7 +118,7 @@ class PlaylistController extends AbstractController
$recommendations = $this->spotifyApi->getRecommendations([
'seed_tracks' => [$track->track->id],
'limit' => ($generateLongPlaylist ? 50 : 30)
'limit' => ($generateLongPlaylist ? 50 : 30),
]);
$this->spotifyPlaylistService->printLog(' -> Got ' . count($recommendations->tracks) . ' recommendatations.');
foreach ($recommendations->tracks as $recommendedTrack) {
......@@ -149,7 +145,7 @@ class PlaylistController extends AbstractController
$newPlaylist = $this->spotifyPlaylistService->createPlaylistWithRandomTracks(
$this->spotifyApi,
$recommendedTrackIds,
$generateLongPlaylist ? SpotifyPlaylistService::TRACKS_COUNT_IN_LONG_PLAYLIST : SpotifyPlaylistService::TRACKS_COUNT_IN_SHORT_PLAYLIST
$generateLongPlaylist ? SpotifyPlaylistService::TRACKS_COUNT_IN_LONG_PLAYLIST : SpotifyPlaylistService::TRACKS_COUNT_IN_SHORT_PLAYLIST,
);
$this->addFlash('infos', $this->spotifyPlaylistService->getCreatedPlaylistInformationMessage($newPlaylist));
......@@ -159,10 +155,8 @@ class PlaylistController extends AbstractController
/**
* @Route("/generate-playlist-from-top-artists", name="generate-playlist-from-top-artists")
*
* @return Response
*/
public function generatePlaylistFromTopArtists(Request $request): Response
public function generatePlaylistFromTopArtists(Request $request): RedirectResponse
{
// Swith "random" or "selected"
if (null !== $request->query->get('generate-playlist-random-top-artists')) {
......@@ -174,11 +168,7 @@ class PlaylistController extends AbstractController
return new RedirectResponse($this->generateUrl('playlist-generator'));
}
/**
*
* @return Response
*/
private function generatePlaylistFromSelectedTopArtists(Request $request): Response
private function generatePlaylistFromSelectedTopArtists(Request $request): RedirectResponse
{
/** @var array */
$selectedArtists = $request->query->get('selected-artist');
......@@ -225,11 +215,7 @@ class PlaylistController extends AbstractController
return new RedirectResponse($this->generateUrl('show-playlist-content', ['playlistId' => $newPlaylist->id]));
}
/**
*
* @return Response
*/
private function generatePlaylistFromRandomTopArtists(Request $request): Response
private function generatePlaylistFromRandomTopArtists(Request $request): RedirectResponse
{
$countInTopArtists = random_int(4, 6);
$countInLessTopArtists = random_int(4, 6);
......@@ -284,10 +270,8 @@ class PlaylistController extends AbstractController
/**
* @Route("/generate-quick-playlist", name="generate-quick-playlist")
*
* @return Response
*/
public function generateQuickPlaylist(Request $request): Response
public function generateQuickPlaylist(Request $request): RedirectResponse
{
// Swith between "daily mixes" or "tambouille"
if (null !== $request->query->get('generate-quick-playlist-from-daily-mixes')) {
......@@ -299,11 +283,7 @@ class PlaylistController extends AbstractController
return new RedirectResponse($this->generateUrl('playlist-generator'));
}
/**
*
* @return Response
*/
private function generatePlaylistFromDailyMixes(Request $request): Response
private function generatePlaylistFromDailyMixes(Request $request): RedirectResponse
{
$generateLongPlaylist = (null !== $request->query->get('check-quick-create-long-playlist'));
......@@ -319,11 +299,7 @@ class PlaylistController extends AbstractController
return new RedirectResponse($this->generateUrl('show-playlist-content', ['playlistId' => $newPlaylist->id]));
}
/**
*
* @return Response
*/
private function generateplaylistTambouilleMix(Request $request): Response
private function generateplaylistTambouilleMix(Request $request): RedirectResponse
{
$generateLongPlaylist = (null !== $request->query->get('check-quick-create-long-playlist'));
......
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>{% block title %}Spotify playlist generator{% endblock %}</title>
<link rel="shortcut icon" type="image/png" href="/favicon.png"/>
{% block stylesheets %}{{ encore_entry_link_tags('app') }}{% endblock %}
</head>
<body>
<div class="container-fluid mb-5 mt-2">
<div class="alert alert-warning mt-2">Something went wrong...</div>
</div>
{% block javascripts %}{{ encore_entry_script_tags('app') }}{% endblock %}
</body>
</html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment