2018-08-18 13:19:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Models\Album;
|
|
|
|
use App\Models\Artist;
|
2024-02-23 18:36:02 +00:00
|
|
|
use App\Services\Contracts\MusicEncyclopedia;
|
2022-07-08 14:53:04 +00:00
|
|
|
use App\Values\AlbumInformation;
|
|
|
|
use App\Values\ArtistInformation;
|
2022-08-08 16:00:59 +00:00
|
|
|
use Illuminate\Cache\Repository as 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,
|
|
|
|
private readonly MediaMetadataService $mediaMetadataService,
|
|
|
|
private readonly Cache $cache
|
2022-06-10 10:47:46 +00:00
|
|
|
) {
|
2018-08-18 13:19:40 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 14:53:04 +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
|
|
|
}
|
|
|
|
|
2022-08-08 16:00:59 +00:00
|
|
|
if ($this->cache->has('album.info.' . $album->id)) {
|
|
|
|
return $this->cache->get('album.info.' . $album->id);
|
2018-08-19 11:08:16 +00:00
|
|
|
}
|
2018-08-18 13:19:40 +00:00
|
|
|
|
2022-08-08 16:00:59 +00:00
|
|
|
$info = $this->encyclopedia->getAlbumInformation($album) ?: AlbumInformation::make();
|
|
|
|
|
|
|
|
attempt_unless($album->has_cover, function () use ($info, $album): void {
|
|
|
|
$this->mediaMetadataService->tryDownloadAlbumCover($album);
|
|
|
|
$info->cover = $album->cover;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->cache->put('album.info.' . $album->id, $info, now()->addWeek());
|
|
|
|
|
2018-08-18 13:19:40 +00:00
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
|
2022-07-08 14:53:04 +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
|
|
|
}
|
|
|
|
|
2022-08-08 16:00:59 +00:00
|
|
|
if ($this->cache->has('artist.info.' . $artist->id)) {
|
|
|
|
return $this->cache->get('artist.info.' . $artist->id);
|
2018-08-19 11:08:16 +00:00
|
|
|
}
|
2018-08-18 13:19:40 +00:00
|
|
|
|
2022-08-08 16:00:59 +00:00
|
|
|
$info = $this->encyclopedia->getArtistInformation($artist) ?: ArtistInformation::make();
|
|
|
|
|
|
|
|
attempt_unless($artist->has_image, function () use ($artist, $info): void {
|
|
|
|
$this->mediaMetadataService->tryDownloadArtistImage($artist);
|
|
|
|
$info->image = $artist->image;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->cache->put('artist.info.' . $artist->id, $info, now()->addWeek());
|
|
|
|
|
2018-08-18 13:19:40 +00:00
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
}
|