koel/app/Services/MediaInformationService.php

57 lines
1.5 KiB
PHP
Raw Normal View History

2018-08-18 15:19:40 +02:00
<?php
namespace App\Services;
2018-08-19 11:05:33 +02:00
use App\Events\AlbumInformationFetched;
use App\Events\ArtistInformationFetched;
2018-08-18 15:19:40 +02:00
use App\Models\Album;
use App\Models\Artist;
2022-06-10 12:47:46 +02:00
use App\Repositories\AlbumRepository;
use App\Repositories\ArtistRepository;
use App\Values\AlbumInformation;
use App\Values\ArtistInformation;
2018-08-18 15:19:40 +02:00
class MediaInformationService
{
2022-06-10 12:47:46 +02:00
public function __construct(
private LastfmService $lastfmService,
private AlbumRepository $albumRepository,
private ArtistRepository $artistRepository
) {
2018-08-18 15:19:40 +02:00
}
public function getAlbumInformation(Album $album): ?AlbumInformation
2018-08-18 15:19:40 +02:00
{
if ($album->is_unknown) {
2018-08-24 17:27:19 +02:00
return null;
2018-08-18 15:19:40 +02:00
}
$info = $this->lastfmService->getAlbumInformation($album->name, $album->artist->name);
2018-08-18 15:19:40 +02:00
if ($info) {
event(new AlbumInformationFetched($album, $info));
2022-06-10 12:47:46 +02:00
// The album cover may have been updated.
$info->cover = $this->albumRepository->getOneById($album->id)->cover;
}
2018-08-18 15:19:40 +02:00
return $info;
}
public function getArtistInformation(Artist $artist): ?ArtistInformation
2018-08-18 15:19:40 +02:00
{
if ($artist->is_unknown) {
2018-08-24 17:27:19 +02:00
return null;
2018-08-18 15:19:40 +02:00
}
$info = $this->lastfmService->getArtistInformation($artist->name);
2018-08-18 15:19:40 +02:00
if ($info) {
event(new ArtistInformationFetched($artist, $info));
2022-06-10 12:47:46 +02:00
// The artist image may have been updated.
$info->image = $this->artistRepository->getOneById($artist->id)->image;
}
2018-08-18 15:19:40 +02:00
return $info;
}
}