App\services\Lastfm::getAlbumInfo PHP Method

getAlbumInfo() public method

Get information about an album.
public getAlbumInfo ( string $name, string $artistName ) : array | false
$name string Name of the album
$artistName string Name of the artist
return array | false
    public function getAlbumInfo($name, $artistName)
    {
        if (!$this->enabled()) {
            return false;
        }
        $name = urlencode($name);
        $artistName = urlencode($artistName);
        try {
            $cacheKey = md5("lastfm_album_{$name}_{$artistName}");
            if ($response = Cache::get($cacheKey)) {
                $response = simplexml_load_string($response);
            } else {
                if ($response = $this->get("?method=album.getInfo&autocorrect=1&album={$name}&artist={$artistName}")) {
                    Cache::put($cacheKey, $response->asXML(), 24 * 60 * 7);
                }
            }
            $response = json_decode(json_encode($response), true);
            if (!$response || !($album = array_get($response, 'album'))) {
                return false;
            }
            return ['url' => array_get($album, 'url'), 'image' => count($album['image']) > 3 ? $album['image'][3] : $album['image'][0], 'wiki' => ['summary' => $this->formatText(array_get($album, 'wiki.summary')), 'full' => $this->formatText(array_get($album, 'wiki.content'))], 'tracks' => array_map(function ($track) {
                return ['title' => $track['name'], 'length' => (int) $track['duration'], 'url' => $track['url']];
            }, array_get($album, 'tracks.track', []))];
        } catch (Exception $e) {
            Log::error($e);
            return false;
        }
    }

Usage Example

コード例 #1
0
 public function testGetAlbumInfoFailed()
 {
     $client = m::mock(Client::class, ['get' => new Response(400, [], file_get_contents(dirname(__FILE__) . '/blobs/lastfm/album-notfound.xml'))]);
     $api = new Lastfm(null, null, $client);
     $this->assertFalse($api->getAlbumInfo('foo', 'bar'));
 }