koel/app/Services/MediaInformationService.php

67 lines
1.6 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;
class MediaInformationService
{
private $lastfmService;
public function __construct(LastfmService $lastfmService)
{
$this->lastfmService = $lastfmService;
}
/**
* Get extra information about an album from Last.fm.
*
2018-08-24 15:27:19 +00:00
* @return array|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));
// The album may have been updated.
$album->refresh();
$info['cover'] = $album->cover;
}
2018-08-18 13:19:40 +00:00
return $info;
}
/**
* Get extra information about an artist from Last.fm.
*
2018-08-24 15:27:19 +00:00
* @return array|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));
// The artist may have been updated.
$artist->refresh();
$info['image'] = $artist->image;
}
2018-08-18 13:19:40 +00:00
return $info;
}
}