2024-03-22 12:55:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Integrations\Lastfm\Requests;
|
|
|
|
|
2024-03-22 14:34:13 +00:00
|
|
|
use App\Http\Integrations\Lastfm\Concerns\FormatsLastFmText;
|
2024-03-22 12:55:25 +00:00
|
|
|
use App\Models\Album;
|
|
|
|
use App\Values\AlbumInformation;
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
use Saloon\Enums\Method;
|
|
|
|
use Saloon\Http\Request;
|
|
|
|
use Saloon\Http\Response;
|
|
|
|
|
|
|
|
final class GetAlbumInfoRequest extends Request
|
|
|
|
{
|
|
|
|
use FormatsLastFmText;
|
|
|
|
|
|
|
|
protected Method $method = Method::GET;
|
|
|
|
|
2024-04-18 14:36:28 +00:00
|
|
|
public function __construct(private readonly Album $album)
|
2024-03-22 12:55:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function resolveEndpoint(): string
|
|
|
|
{
|
|
|
|
return '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @return array<mixed> */
|
|
|
|
protected function defaultQuery(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'method' => 'album.getInfo',
|
|
|
|
'artist' => $this->album->artist->name,
|
|
|
|
'album' => $this->album->name,
|
|
|
|
'autocorrect' => 1,
|
|
|
|
'format' => 'json',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createDtoFromResponse(Response $response): ?AlbumInformation
|
|
|
|
{
|
|
|
|
$album = object_get($response->object(), 'album');
|
|
|
|
|
|
|
|
if (!$album) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AlbumInformation::make(
|
|
|
|
url: object_get($album, 'url'),
|
|
|
|
cover: Arr::get(object_get($album, 'image', []), '0.#text'),
|
|
|
|
wiki: [
|
|
|
|
'summary' => self::formatLastFmText(object_get($album, 'wiki.summary')),
|
|
|
|
'full' => self::formatLastFmText(object_get($album, 'wiki.content')),
|
|
|
|
],
|
|
|
|
tracks: array_map(static fn ($track): array => [
|
|
|
|
'title' => $track->name,
|
|
|
|
'length' => (int) $track->duration,
|
|
|
|
'url' => $track->url,
|
|
|
|
], object_get($album, 'tracks.track', []))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|