koel/app/Services/MediaInformationService.php

65 lines
1.9 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;
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(
2022-08-08 16:00:59 +00:00
private MusicEncyclopedia $encyclopedia,
private MediaMetadataService $mediaMetadataService,
private Cache $cache
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
}
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-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;
}
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-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;
}
}