vonage/nexmo-bridge
Composer autoloader bridge that aliases legacy Nexmo (\Nexmo) classes, traits, and interfaces to their Vonage namespace equivalents. Use it to migrate existing code to vonage/client or vonage/client-core without refactoring everything at once.
Installation
composer require vonage/nexmo-bridge
Add to composer.json if using legacy Nexmo namespace:
"extra": {
"nexmo-bridge": {
"apiKey": "YOUR_API_KEY",
"apiSecret": "YOUR_API_SECRET"
}
}
First Use Case: Legacy Code Migration
Replace Nexmo\Client with Vonage\Client in existing code:
// Before (Nexmo)
$client = new Nexmo\Client\Credentials\Basic('key', 'secret');
// After (Vonage + Bridge)
$client = new Vonage\Client\Credentials\Basic(config('nexmo-bridge.apiKey'), config('nexmo-bridge.apiSecret'));
Key Files to Review
vendor/vonage/nexmo-bridge/src/Vonage/Nexmo/Client.php (Core bridge class)config/nexmo-bridge.php (Default config, if published)Use use statements to alias Vonage classes under Nexmo for seamless migration:
use Vonage\Client as NexmoClient;
use Vonage\Sms\Message as NexmoMessage;
Publish the config for customization:
php artisan vendor:publish --provider="Vonage\NexmoBridge\NexmoBridgeServiceProvider"
Update .env:
VONAGE_API_KEY=your_key
VONAGE_API_SECRET=your_secret
Bind the Vonage client in AppServiceProvider:
public function register()
{
$this->app->singleton('nexmo', function ($app) {
return new Vonage\Client($app['config']['nexmo-bridge']);
});
}
Leverage the bridge’s static method wrappers for common operations:
// Legacy-style call (internally uses Vonage SDK)
$response = Nexmo\Client::sendSms('from', 'to', 'Hello!');
Mock the Vonage client in tests:
$mock = Mockery::mock('alias:Vonage\Client');
$mock->shouldReceive('sms()->send')->once();
$this->app->instance('nexmo', $mock);
Namespace Collisions
Avoid mixing Nexmo\ and Vonage\ namespaces in the same file. Use aliases or fully qualify names:
// ❌ Ambiguous
$client = new Nexmo\Client(...);
// ✅ Explicit
$client = new Vonage\Client(...);
Deprecated Method Warnings The bridge logs deprecation warnings for Nexmo methods. Monitor logs for migration prompts:
[NexmoBridge] Method Nexmo\Client::sendSms() is deprecated. Use Vonage\Sms\Sms::send().
Configuration Overrides
Bridge config takes precedence over .env if not published. Explicitly publish to avoid surprises:
// config/nexmo-bridge.php
'apiKey' => env('VONAGE_API_KEY', 'fallback_key'),
Enable Verbose Logging
Add to config/logging.php:
'channels' => [
'nexmo' => [
'driver' => 'single',
'path' => storage_path('logs/nexmo.log'),
'level' => 'debug',
],
],
Then log bridge events:
\Vonage\NexmoBridge\Logger::debug('Custom event', ['data' => $data]);
Check HTTP Requests
Use Laravel’s tap to inspect Vonage requests:
$client->sms()->send()->tap(function ($response) {
\Log::debug('Response:', $response->getBody());
});
Custom Bridge Classes
Extend Vonage\NexmoBridge\Client to add legacy-specific logic:
class CustomNexmoClient extends \Vonage\NexmoBridge\Client {
public function legacySend($params) {
return $this->sms()->send($params['from'], $params['to'], $params['text']);
}
}
Event Listeners
Listen for bridge events (e.g., nexmo.bridge.method.called):
public function handle(NexmoBridgeEvent $event) {
\Log::info("Called {$event->method} with args: " . json_encode($event->args));
}
Fallback for Missing Methods
Override handleMissingMethod in a custom client:
public function __call($method, $args) {
if (strpos($method, 'nexmo_') === 0) {
return $this->fallbackToVonage(substr($method, 6), $args);
}
throw new \BadMethodCallException("Method {$method} not found.");
}
How can I help you explore Laravel packages today?