Skip to content

Commit

Permalink
feat: decouple artist/album and the media information
Browse files Browse the repository at this point in the history
  • Loading branch information
phanan committed Jul 8, 2022
1 parent 7123d67 commit 08e4953
Show file tree
Hide file tree
Showing 33 changed files with 326 additions and 321 deletions.
19 changes: 2 additions & 17 deletions app/Events/AlbumInformationFetched.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,14 @@
namespace App\Events;

use App\Models\Album;
use App\Values\AlbumInformation;
use Illuminate\Queue\SerializesModels;

class AlbumInformationFetched extends Event
{
use SerializesModels;

private Album $album;
private array $information;

public function __construct(Album $album, array $information)
{
$this->album = $album;
$this->information = $information;
}

public function getAlbum(): Album
{
return $this->album;
}

/** @return array<mixed> */
public function getInformation(): array
public function __construct(public Album $album, public AlbumInformation $information)
{
return $this->information;
}
}
19 changes: 2 additions & 17 deletions app/Events/ArtistInformationFetched.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,14 @@
namespace App\Events;

use App\Models\Artist;
use App\Values\ArtistInformation;
use Illuminate\Queue\SerializesModels;

class ArtistInformationFetched
{
use SerializesModels;

private Artist $artist;
private array $information;

public function __construct(Artist $artist, array $information)
{
$this->artist = $artist;
$this->information = $information;
}

public function getArtist(): Artist
{
return $this->artist;
}

/** @return array<mixed> */
public function getInformation(): array
public function __construct(public Artist $artist, public ArtistInformation $information)
{
return $this->information;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class AlbumController extends Controller
{
public function show(Album $album)
{
return response()->json($this->mediaInformationService->getAlbumInformation($album));
return response()->json($this->mediaInformationService->getAlbumInformation($album)?->toArray() ?: []);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class ArtistController extends Controller
{
public function show(Artist $artist)
{
return response()->json($this->mediaInformationService->getArtistInformation($artist));
return response()->json($this->mediaInformationService->getArtistInformation($artist)?->toArray() ?: []);
}
}
14 changes: 6 additions & 8 deletions app/Http/Controllers/API/MediaInformation/SongController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@

class SongController extends Controller
{
private YouTubeService $youTubeService;

public function __construct(MediaInformationService $mediaInformationService, YouTubeService $youTubeService)
{
public function __construct(
protected MediaInformationService $mediaInformationService,
private YouTubeService $youTubeService
) {
parent::__construct($mediaInformationService);

$this->youTubeService = $youTubeService;
}

public function show(Song $song)
{
return response()->json([
'lyrics' => $song->lyrics,
'album_info' => $this->mediaInformationService->getAlbumInformation($song->album),
'artist_info' => $this->mediaInformationService->getArtistInformation($song->artist),
'album_info' => $this->mediaInformationService->getAlbumInformation($song->album)?->toArray() ?: [],
'artist_info' => $this->mediaInformationService->getArtistInformation($song->artist)?->toArray() ?: [],
'youtube' => $this->youTubeService->searchVideosRelatedToSong($song),
]);
}
Expand Down
13 changes: 3 additions & 10 deletions app/Http/Controllers/V6/API/AlbumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@
use App\Models\Album;
use App\Models\User;
use App\Repositories\AlbumRepository;
use App\Services\MediaInformationService;
use Illuminate\Contracts\Auth\Authenticatable;

class AlbumController extends Controller
{
/** @param User $user */
public function __construct(
private AlbumRepository $albumRepository,
private MediaInformationService $informationService,
private ?Authenticatable $user
) {
public function __construct(private AlbumRepository $albumRepository, private ?Authenticatable $user)
{
}

public function index()
Expand All @@ -32,9 +28,6 @@ public function index()

public function show(Album $album)
{
$album = $this->albumRepository->getOne($album->id, $this->user);
$album->information = $this->informationService->getAlbumInformation($album);

return AlbumResource::make($album);
return AlbumResource::make($this->albumRepository->getOne($album->id, $this->user));
}
}
5 changes: 1 addition & 4 deletions app/Http/Controllers/V6/API/ArtistController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ public function index()

public function show(Artist $artist)
{
$artist = $this->artistRepository->getOne($artist->id, $this->user);
$artist->information = $this->informationService->getArtistInformation($artist);

return ArtistResource::make($artist);
return ArtistResource::make($this->artistRepository->getOne($artist->id, $this->user));
}
}
15 changes: 15 additions & 0 deletions app/Http/Controllers/V6/API/FetchAlbumInformationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Http\Controllers\V6\API;

use App\Http\Controllers\API\Controller;
use App\Models\Album;
use App\Services\MediaInformationService;

class FetchAlbumInformationController extends Controller
{
public function __invoke(Album $album, MediaInformationService $informationService)
{
return response()->json($informationService->getAlbumInformation($album));
}
}
15 changes: 15 additions & 0 deletions app/Http/Controllers/V6/API/FetchArtistInformationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Http\Controllers\V6\API;

use App\Http\Controllers\API\Controller;
use App\Models\Artist;
use App\Services\MediaInformationService;

class FetchArtistInformationController extends Controller
{
public function __invoke(Artist $artist, MediaInformationService $informationService)
{
return response()->json($informationService->getArtistInformation($artist));
}
}
2 changes: 0 additions & 2 deletions app/Http/Resources/AlbumResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Models\Album;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Arr;

class AlbumResource extends JsonResource
{
Expand All @@ -27,7 +26,6 @@ public function toArray($request): array
'length' => $this->album->length,
'play_count' => (int) $this->album->play_count,
'song_count' => (int) $this->album->song_count,
'info' => Arr::wrap($this->album->information),
];
}
}
2 changes: 0 additions & 2 deletions app/Http/Resources/ArtistResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Models\Artist;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Arr;

class ArtistResource extends JsonResource
{
Expand All @@ -26,7 +25,6 @@ public function toArray($request): array
'song_count' => (int) $this->artist->song_count,
'album_count' => (int) $this->artist->album_count,
'created_at' => $this->artist->created_at,
'info' => Arr::wrap($this->artist->information),
];
}
}
16 changes: 4 additions & 12 deletions app/Listeners/DownloadAlbumCover.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,17 @@

class DownloadAlbumCover
{
private MediaMetadataService $mediaMetadataService;

public function __construct(MediaMetadataService $mediaMetadataService)
public function __construct(private MediaMetadataService $mediaMetadataService)
{
$this->mediaMetadataService = $mediaMetadataService;
}

public function handle(AlbumInformationFetched $event): void
{
$info = $event->getInformation();
$album = $event->getAlbum();

$image = array_get($info, 'image');

// If our current album has no cover, and Last.fm has one, steal it?
if (!$album->has_cover && $image && ini_get('allow_url_fopen')) {
if (!$event->album->has_cover && $event->information->cover && ini_get('allow_url_fopen')) {
try {
$this->mediaMetadataService->downloadAlbumCover($album, $image);
} catch (Throwable $e) {
$this->mediaMetadataService->downloadAlbumCover($event->album, $event->information->cover);
} catch (Throwable) {
}
}
}
Expand Down
16 changes: 4 additions & 12 deletions app/Listeners/DownloadArtistImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,17 @@

class DownloadArtistImage
{
private MediaMetadataService $mediaMetadataService;

public function __construct(MediaMetadataService $mediaMetadataService)
public function __construct(private MediaMetadataService $mediaMetadataService)
{
$this->mediaMetadataService = $mediaMetadataService;
}

public function handle(ArtistInformationFetched $event): void
{
$info = $event->getInformation();
$artist = $event->getArtist();

$image = array_get($info, 'image');

// If our artist has no image, and Last.fm has one, we steal it?
if (!$artist->has_image && $image && ini_get('allow_url_fopen')) {
if (!$event->artist->has_image && $event->information->image && ini_get('allow_url_fopen')) {
try {
$this->mediaMetadataService->downloadArtistImage($artist, $image);
} catch (Throwable $e) {
$this->mediaMetadataService->downloadArtistImage($event->artist, $event->information->image);
} catch (Throwable) {
}
}
}
Expand Down
Loading

0 comments on commit 08e4953

Please sign in to comment.