├── .env.example ├── config └── sms.php └── src ├── Contracts └── SMS.php ├── Drivers ├── Driver.php ├── NexmoDriver.php ├── NullDriver.php └── TwilioDriver.php ├── Exceptions └── SmsException.php ├── Facade └── Sms.php ├── SmsManager.php └── SmsServiceProvider.php /.env.example: -------------------------------------------------------------------------------- 1 | NEXMO_KEY= 2 | NEXMO_SECRET= 3 | NEXMO_SMS_FROM= 4 | 5 | TWILIO_KEY= 6 | TWILIO_SECRET= 7 | TWILIO_SMS_FROM= -------------------------------------------------------------------------------- /config/sms.php: -------------------------------------------------------------------------------- 1 | env('SMS_DRIVER', 'nexmo'), 15 | 16 | /* 17 | |-------------------------------------------------------------------------- 18 | | Nexmo Driver Configuration 19 | |-------------------------------------------------------------------------- 20 | | 21 | | We specify key, secret, and the number messages will be sent from. 22 | | 23 | */ 24 | 'nexmo' => [ 25 | 'key' => env('NEXMO_KEY', ''), 26 | 'secret' => env('NEXMO_SECRET', ''), 27 | 'from' => env('NEXMO_SMS_FROM', '') 28 | ], 29 | 30 | /* 31 | |-------------------------------------------------------------------------- 32 | | Twilio Driver Configuration 33 | |-------------------------------------------------------------------------- 34 | | 35 | | We specify key, secret, and the number messages will be sent from. 36 | | 37 | */ 38 | 'twilio' => [ 39 | 'key' => env('TWILIO_KEY', ''), 40 | 'secret' => env('TWILIO_SECRET', ''), 41 | 'from' => env('TWILIO_SMS_FROM', '') 42 | ], 43 | ]; -------------------------------------------------------------------------------- /src/Contracts/SMS.php: -------------------------------------------------------------------------------- 1 | recipient = $recipient; 44 | 45 | return $this; 46 | } 47 | 48 | /** 49 | * Set the content of the message. 50 | * 51 | * @param string $message 52 | * 53 | * @throws \App\Components\Sms\Exceptions\SmsException 54 | * 55 | * @return $this 56 | */ 57 | public function content(string $message) 58 | { 59 | throw_if(empty($message), SmsException::class, 'Message text is required'); 60 | 61 | $this->message = $message; 62 | 63 | return $this; 64 | } 65 | } -------------------------------------------------------------------------------- /src/Drivers/NexmoDriver.php: -------------------------------------------------------------------------------- 1 | client = $nexmo; 33 | $this->from = $from; 34 | } 35 | 36 | /** 37 | * {@inheritdoc} 38 | */ 39 | public function send() 40 | { 41 | return $this->client->message()->send([ 42 | 'type' => 'text', 43 | 'from' => $this->from, 44 | 'to' => $this->recipient, 45 | 'text' => trim($this->message) 46 | ]); 47 | } 48 | } -------------------------------------------------------------------------------- /src/Drivers/NullDriver.php: -------------------------------------------------------------------------------- 1 | client = $twilio; 33 | $this->from = $from; 34 | } 35 | 36 | /** 37 | * {@inheritdoc} 38 | */ 39 | public function send() 40 | { 41 | return $this->client->messages->create( 42 | $this->recipient, [ 43 | 'from' => $this->from, 44 | 'body' => trim($this->message) 45 | ] 46 | ); 47 | } 48 | } -------------------------------------------------------------------------------- /src/Exceptions/SmsException.php: -------------------------------------------------------------------------------- 1 | driver($name); 24 | } 25 | 26 | /** 27 | * Create a Nexmo SMS driver instance. 28 | * 29 | * @return \App\Components\Sms\Drivers\NexmoDriver 30 | */ 31 | public function createNexmoDriver() 32 | { 33 | return new NexmoDriver( 34 | $this->createNexmoClient(), 35 | $this->app['config']['sms.nexmo.from'] 36 | ); 37 | } 38 | 39 | /** 40 | * Create a Twilio SMS driver instance. 41 | * 42 | * @return \App\Components\Sms\Drivers\TwilioDriver 43 | */ 44 | public function createTwilioDriver() 45 | { 46 | return new TwilioDriver( 47 | $this->createTwilioClient(), 48 | $this->app['config']['sms.twilio.from'] 49 | ); 50 | } 51 | 52 | /** 53 | * Create the Nexmo client. 54 | * 55 | * @return \Nexmo\Client 56 | */ 57 | protected function createNexmoClient() 58 | { 59 | return new NexmoClient( 60 | new NexmoBasicCredentials( 61 | $this->app['config']['sms.nexmo.key'], 62 | $this->app['config']['sms.nexmo.secret'] 63 | ) 64 | ); 65 | } 66 | 67 | /** 68 | * Create the Twilio client. 69 | * 70 | * @return \Twilio\Rest\Client 71 | */ 72 | protected function createTwilioClient() 73 | { 74 | return new TwilioClient( 75 | $this->app['config']['sms.twilio.key'], 76 | $this->app['config']['sms.twilio.secret'] 77 | ); 78 | } 79 | 80 | 81 | /** 82 | * Create a Null SMS driver instance. 83 | * 84 | * @return \App\Components\Sms\Drivers\NullDriver 85 | */ 86 | public function createNullDriver() 87 | { 88 | return new NullDriver; 89 | } 90 | 91 | /** 92 | * Get the default SMS driver name. 93 | * 94 | * @return string 95 | */ 96 | public function getDefaultDriver() 97 | { 98 | return $this->app['config']['sms.default'] ?? 'null'; 99 | } 100 | } -------------------------------------------------------------------------------- /src/SmsServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->singleton('sms', function ($app) { 25 | return new SmsManager($app); 26 | }); 27 | } 28 | 29 | /** 30 | * Get the services provided by the provider. 31 | * 32 | * @return array 33 | */ 34 | public function provides() 35 | { 36 | return ['sms']; 37 | } 38 | } --------------------------------------------------------------------------------