koel/app/Services/SpotifyService.php

53 lines
1.2 KiB
PHP
Raw Normal View History

2022-07-16 22:42:29 +00:00
<?php
namespace App\Services;
use App\Http\Integrations\Spotify\SpotifyClient;
2022-07-16 22:42:29 +00:00
use App\Models\Album;
use App\Models\Artist;
use Illuminate\Support\Arr;
class SpotifyService
{
2024-04-18 14:36:28 +00:00
public function __construct(private readonly SpotifyClient $client)
2022-07-16 22:42:29 +00:00
{
}
public static function enabled(): bool
{
return config('koel.spotify.client_id') && config('koel.spotify.client_secret');
}
public function tryGetArtistImage(Artist $artist): ?string
{
if (!static::enabled()) {
return null;
}
2022-07-18 11:00:37 +00:00
if ($artist->is_various || $artist->is_unknown) {
return null;
}
2022-07-16 22:42:29 +00:00
return Arr::get(
$this->client->search($artist->name, 'artist', ['limit' => 1]),
'artists.items.0.images.0.url'
);
}
public function tryGetAlbumCover(Album $album): ?string
{
if (!static::enabled()) {
return null;
}
if ($album->is_unknown || $album->artist->is_unknown || $album->artist->is_various) {
return null;
}
return Arr::get(
$this->client->search("$album->name artist:{$album->artist->name}", 'album', ['limit' => 1]),
2022-07-16 22:42:29 +00:00
'albums.items.0.images.0.url'
);
}
}