koel/app/Services/MediaInformationService.php

67 lines
1.7 KiB
PHP
Raw Normal View History

2018-08-18 13:19:40 +00:00
<?php
namespace App\Services;
2018-08-19 09:05:33 +00:00
use App\Events\AlbumInformationFetched;
use App\Events\ArtistInformationFetched;
2018-08-18 13:19:40 +00:00
use App\Models\Album;
use App\Models\Artist;
2022-06-10 10:47:46 +00:00
use App\Repositories\AlbumRepository;
use App\Repositories\ArtistRepository;
2018-08-18 13:19:40 +00:00
class MediaInformationService
{
2022-06-10 10:47:46 +00:00
public function __construct(
private LastfmService $lastfmService,
private AlbumRepository $albumRepository,
private ArtistRepository $artistRepository
) {
2018-08-18 13:19:40 +00:00
}
/**
* Get extra information about an album from Last.fm.
*
2020-12-22 20:11:22 +00:00
* @return array<mixed>|null the album info in an array format, or null on failure
2018-08-18 13:19:40 +00:00
*/
2018-08-24 15:27:19 +00:00
public function getAlbumInformation(Album $album): ?array
2018-08-18 13:19:40 +00:00
{
if ($album->is_unknown) {
2018-08-24 15:27:19 +00:00
return null;
2018-08-18 13:19:40 +00:00
}
$info = $this->lastfmService->getAlbumInformation($album->name, $album->artist->name);
2018-08-18 13:19:40 +00:00
if ($info) {
event(new AlbumInformationFetched($album, $info));
2022-06-10 10:47:46 +00:00
// The album cover may have been updated.
$info['cover'] = $this->albumRepository->getOneById($album->id)->cover;
}
2018-08-18 13:19:40 +00:00
return $info;
}
/**
* Get extra information about an artist from Last.fm.
*
2020-12-22 20:11:22 +00:00
* @return array<mixed>|null the artist info in an array format, or null on failure
2018-08-18 13:19:40 +00:00
*/
2018-08-24 15:27:19 +00:00
public function getArtistInformation(Artist $artist): ?array
2018-08-18 13:19:40 +00:00
{
if ($artist->is_unknown) {
2018-08-24 15:27:19 +00:00
return null;
2018-08-18 13:19:40 +00:00
}
$info = $this->lastfmService->getArtistInformation($artist->name);
2018-08-18 13:19:40 +00:00
if ($info) {
event(new ArtistInformationFetched($artist, $info));
2022-06-10 10:47:46 +00:00
// The artist image may have been updated.
$info['image'] = $this->artistRepository->getOneById($artist->id)->image;
}
2018-08-18 13:19:40 +00:00
return $info;
}
}