calliostro/last-fm-client-bundle
Installation:
composer require calliostro/last-fm-client-bundle
Ensure calliostro/lastfm-client (v2.0.0+) is also installed as a dependency.
Configuration: Publish the default config:
php bin/console config:dump-reference calliostro_lastfm
Update config/packages/calliostro_lastfm.yaml with your API key and rate-limiting settings:
calliostro_lastfm:
api_key: '%env(LASTFM_API_KEY)%'
rate_limiter: true
limit: 1000 # requests/hour
First Use Case:
Inject Calliostro\LastFm\LastFmClient into a service/controller:
use Calliostro\LastFm\LastFmClient;
class ArtistController
{
public function __construct(private LastFmClient $lastFmClient) {}
public function show(string $artistName)
{
$info = $this->lastFmClient->getArtistInfo($artistName);
return response()->json($info);
}
}
public function __construct(
private LastFmClient $lastFmClient,
private RateLimiterInterface $rateLimiter
) {}
container->get(); use constructor injection or #[Inject] attribute (Symfony 6.4+).rate_limiter: true). The bundle integrates with symfony/rate-limiter:
calliostro_lastfm:
rate_limiter:
enabled: true
limit: 1000
interval: 3600 # seconds
LastFmClient or using middleware.$tracks = $lastFmClient->getRecentTracks('username');
$albums = $lastFmClient->getTopAlbums('artist', limit: 5);
try/catch with Calliostro\LastFm\Exception\LastFmException:
try {
$lastFmClient->getArtistInfo('nonexistent_artist');
} catch (LastFmException $e) {
// Handle API errors (e.g., rate limits, invalid requests)
}
LastFmClient in tests:
$this->mock(LastFmClient::class)->shouldReceive('getArtistInfo')->andReturn($mockData);
$this->messageBus->dispatch(new FetchArtistInfo($artistName));
$cache = $this->cacheItemPool->getItem('lastfm_artist_' . $artistName);
if (!$cache->isHit()) {
$cache->set($lastFmClient->getArtistInfo($artistName));
$cache->expiresAfter(3600);
}
Namespace Update:
Calliostro\LastFm\LastFmServiceCalliostro\LastFm\LastFmClient (capital F).Configuration:
lastfm key in config.calliostro_lastfm key.php bin/console config:dump-reference calliostro_lastfm to regenerate config.Dependency:
calliostro/php-lastfm-api with calliostro/lastfm-client (v2.0.0+).Migration:
Rate Limiting:
RateLimiter logs for throttling issues:
bin/console debug:container symfony_rate_limiter
LastFmClient or using middleware.API Errors:
calliostro_lastfm:
debug: true
batch() method to reduce HTTP calls:
$results = $lastFmClient->batch([
'getArtistInfo' => ['artist' => 'Arctic Monkeys'],
'getTopTracks' => ['artist' => 'Arctic Monkeys', 'period' => '7day'],
]);
$cacheKey = 'lastfm_top_artists_' . $period;
if (!$cached = cache()->get($cacheKey)) {
$cached = $lastFmClient->getTopArtists($period);
cache()->put($cacheKey, $cached, now()->addHours(1));
}
Custom Clients:
Extend LastFmClient to add domain-specific methods:
class CustomLastFmClient extends LastFmClient
{
public function getArtistTopFans(string $artistName, int $limit = 10): array
{
return $this->call('artist.getTopFans', [
'artist' => $artistName,
'limit' => $limit,
]);
}
}
Register as a service:
services:
App\Service\CustomLastFmClient:
arguments:
$client: '@calliostro_lastfm.client'
Middleware: Add request/response middleware:
$lastFmClient->getMiddlewareStack()->add(
new AddUserAgentMiddleware('MyApp/1.0')
);
Event Listeners:
Subscribe to LastFmClientEvents (e.g., BeforeRequest, AfterResponse) for logging/auditing:
$dispatcher->addListener(LastFmClientEvents::BEFORE_REQUEST, function (BeforeRequestEvent $event) {
// Log request details
});
How can I help you explore Laravel packages today?