Last active
October 19, 2024 05:55
-
-
Save shawnlindstrom/42443d090e6d56a5a93c3ec90c46935b to your computer and use it in GitHub Desktop.
Twilio Service Provider for Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use Illuminate\Support\ServiceProvider; | |
use Twilio\Rest\Client; | |
class TwilioServiceProvider extends ServiceProvider | |
{ | |
public function register() | |
{ | |
$this->app->singleton(Client::class, fn() => | |
new Client( | |
$app->config['services.twilio.sid'], | |
$app->config['services.twilio.token'] | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Future me here. Abstract all your Twilio calls and inject the Client into those services or whatever you want to call them. Never put yourself in a situation where you have to mock the Client in order to test. It's not a flex if you can even pull it off. It's bad implementation. Code that is easy to test is objectively better than code that is hard to test or untestable.
Use it in your controller as a real-time facade or just use DI (preferrably);
Then don't forget to test (oversimplified example):
Personally, i don't use real-time facades. Mocking the service directly is almost as simple.