koel/app/Services/MediaInformationService.php

60 lines
1.8 KiB
PHP
Raw Normal View History

2018-08-18 13:19:40 +00:00
<?php
namespace App\Services;
use App\Models\Album;
use App\Models\Artist;
use App\Services\Contracts\MusicEncyclopedia;
use App\Values\AlbumInformation;
use App\Values\ArtistInformation;
2024-06-04 13:35:00 +00:00
use Illuminate\Support\Facades\Cache;
2018-08-18 13:19:40 +00:00
class MediaInformationService
{
2022-06-10 10:47:46 +00:00
public function __construct(
2024-04-18 14:36:28 +00:00
private readonly MusicEncyclopedia $encyclopedia,
2024-06-04 13:35:00 +00:00
private readonly MediaMetadataService $mediaMetadataService
2022-06-10 10:47:46 +00:00
) {
2018-08-18 13:19:40 +00:00
}
public function getAlbumInformation(Album $album): ?AlbumInformation
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
}
2024-06-04 13:35:00 +00:00
return Cache::remember("album.info.$album->id", now()->addWeek(), function () use ($album): AlbumInformation {
$info = $this->encyclopedia->getAlbumInformation($album) ?: AlbumInformation::make();
2018-08-18 13:19:40 +00:00
rescue_unless($album->has_cover, function () use ($info, $album): void {
2024-06-04 13:35:00 +00:00
$this->mediaMetadataService->tryDownloadAlbumCover($album);
$info->cover = $album->cover;
});
2022-08-08 16:00:59 +00:00
2024-06-04 13:35:00 +00:00
return $info;
2022-08-08 16:00:59 +00:00
});
2018-08-18 13:19:40 +00:00
}
public function getArtistInformation(Artist $artist): ?ArtistInformation
2018-08-18 13:19:40 +00:00
{
2022-08-08 16:00:59 +00:00
if ($artist->is_unknown || $artist->is_various) {
2018-08-24 15:27:19 +00:00
return null;
2018-08-18 13:19:40 +00:00
}
2024-06-04 13:35:00 +00:00
return Cache::remember(
"artist.info.$artist->id",
now()->addWeek(),
function () use ($artist): ArtistInformation {
$info = $this->encyclopedia->getArtistInformation($artist) ?: ArtistInformation::make();
2022-08-08 16:00:59 +00:00
rescue_unless($artist->has_image, function () use ($artist, $info): void {
2024-06-04 13:35:00 +00:00
$this->mediaMetadataService->tryDownloadArtistImage($artist);
$info->image = $artist->image;
});
2022-08-08 16:00:59 +00:00
2024-06-04 13:35:00 +00:00
return $info;
}
);
2018-08-18 13:19:40 +00:00
}
}