diff --git a/CONFIGURATION.md b/CONFIGURATION.md new file mode 100644 index 0000000..958d7a9 --- /dev/null +++ b/CONFIGURATION.md @@ -0,0 +1,75 @@ +# Configuration + +Please follow this steps and you are live with in a mere seconds. + +## General Configuration + +1. On notification classes add SMS channel on the like this. + ```php + public function via(object $notifiable): array + { + return ['sms', '...other channel']; + } + ``` +2. On the `.env` file please add this configuration. + ```shell + SMS_LOG=false + SMS_DRIVER="twilio" + SMS_ACCOUNT_MODE="sandbox" + SMS_FROM_NAME="${APP_NAME}" + ``` + +## Driver Configuration + +Depending on driver option you choose, add these API credentials +after existing general configuration variables. + +| Driver | Credentials | +|------------------|-------------------------------------------------------------------------| +| `africastalking` | `SMS_AFRICA_TALKING_API_KEY=null`
`SMS_AFRICA_TALKING_USERNAME=null` | +| `clickatell` | `SMS_CLICKATELL_API_KEY=null` | +| `clicksend` | `SMS_CLICKSEND_USERNAME=null`
`SMS_CLICKSEND_PASSWORD=null` | +| `infobip` | `SMS_INFOBIP_API_TOKEN=null` | +| `messagebird` | `SMS_MESSAGE_BIRD_ACCESS_KEY=null` | +| `smsbroadcast` | `SMS_SMSBROADCAST_USERNAME=null`
`SMS_SMSBROADCAST_PASSWORD=null` | +| `telnyx` | `SMS_TELNYX_API_TOKEN=null` | +| `twilio` | `SMS_TWILIO_USERNAME=null`
`SMS_TWILIO_PASSWORD=null` | + + +## Notification Class + +On the notification class the `via()` will look like this +after adding the sms channel +```php +public function via(object $notifiable): array +{ + return ['sms', '...other']; +} +``` +OR + +```php +use Laraflow\Sms\SmsChannel; + +public function via(object $notifiable): array +{ + return [SmsChannel::class, '...other']; +} +``` + +And the message prepare method should be named `toSms` and +return type is `SmsMessage` class instance. +Such example is given below. + +```php +use Laraflow\Sms\SmsMessage; + +public function toSms(object $notifiable): SmsMessage +{ + return (new SmsMessage) + ->to('88012345678910') + ->message('Hello from Laraflow SMS') + ->vendor('telnyx') //Optional, will overwrite config file + ->from('Laraflow'); //Optional, will overwrite config file +} +``` diff --git a/README.md b/README.md index 726a4a0..b1888a4 100644 --- a/README.md +++ b/README.md @@ -181,43 +181,6 @@ return [ ]; ``` -# Configuration - -Please follow this steps and you are live with in a mere seconds. - -## General Configuration - -1. On notification classes add SMS channel on the like this. - ```php - public function via(object $notifiable): array - { - return ['sms', '...other channel']; - } - ``` -2. On the `.env` file please add this configuration. - ```shell - SMS_LOG=false - SMS_DRIVER="twilio" - SMS_ACCOUNT_MODE="sandbox" - SMS_FROM_NAME="${APP_NAME}" - ``` - -## Driver Configuration - -Depending on driver option you choose, add these API credentials after existing general configuration variables. - -| Driver | Credentials | -|------------------|-------------------------------------------------------------------------| -| `africastalking` | `SMS_AFRICA_TALKING_API_KEY=null`
`SMS_AFRICA_TALKING_USERNAME=null` | -| `clickatell` | `SMS_CLICKATELL_API_KEY=null` | -| `clicksend` | `SMS_CLICKSEND_USERNAME=null`
`SMS_CLICKSEND_PASSWORD=null` | -| `infobip` | `SMS_INFOBIP_API_TOKEN=null` | -| `messagebird` | `SMS_MESSAGE_BIRD_ACCESS_KEY=null` | -| `smsbroadcast` | `SMS_SMSBROADCAST_USERNAME=null`
`SMS_SMSBROADCAST_PASSWORD=null` | -| `telnyx` | `SMS_TELNYX_API_TOKEN=null` | -| `twilio` | `SMS_TWILIO_USERNAME=null`
`SMS_TWILIO_PASSWORD=null` | - - ### Auditory This document was last updated at {docsify-updated}. diff --git a/_sidebar.md b/_sidebar.md index 82f4a2d..577f087 100644 --- a/_sidebar.md +++ b/_sidebar.md @@ -3,8 +3,9 @@ - [Installation](README.md#installation) - Configuration - - [General Configuration](README.md#general-configuration) - - [Driver Configuration](README.md#driver-configuration) + - [General Configuration](CONFIGURATION.md#general-configuration) + - [Driver Configuration](CONFIGURATION.md#driver-configuration) + - [Notification Class](CONFIGURATION.md#notification-class) - Support - [Changelog](CHANGELOG.md) diff --git a/index.html b/index.html index 5b5d22e..3c028ef 100644 --- a/index.html +++ b/index.html @@ -14,6 +14,9 @@ width: 56px !important; height: auto; } + .markdown-section table { + display: table !important; + } diff --git a/src/SmsChannel.php b/src/SmsChannel.php index 4d6051c..02d9370 100644 --- a/src/SmsChannel.php +++ b/src/SmsChannel.php @@ -4,6 +4,7 @@ use BadMethodCallException; use Exception; +use Illuminate\Http\Client\Response; use Illuminate\Notifications\Notification; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Log; @@ -19,22 +20,19 @@ class SmsChannel */ private mixed $driver; - public $response; + public Response $response; - /** - * @throws DriverNotFoundException|ValidationException|InvalidArgumentException - */ - public function __construct() + private function initDriver(string $driver = null): void { - $active = config('sms.default'); + $active = $driver ?? config('sms.default'); if ($active == null) { - throw new InvalidArgumentException('No SMS vendor driver configured as default.'); + throw new InvalidArgumentException('No SMS vendor driver configured.'); } - $driver = config("sms.vendors.{$active}.driver"); + $driverClass = config("sms.vendors.{$active}.driver"); - if ($driver == null || ! class_exists($driver)) { + if ($driverClass == null || ! class_exists($driverClass)) { throw new DriverNotFoundException("No driver configuration found by `{$active}` name."); } @@ -42,13 +40,31 @@ public function __construct() $config = config("sms.vendors.{$active}.{$mode}", []); - $this->driver = App::make($driver); + $this->driver = App::make($driverClass); $this->driver->setConfig($config); $this->driver->mode = $mode; } + private function logSmsResponse(): void + { + if (config('sms.log', false)) { + Log::debug('SMS Vendor Response: ', ['status_code' => $this->response->status(), 'response' => $this->response->body()]); + } + } + + private function validate(SmsMessage $message): void + { + if ($message->getReceiver() == null || strlen($message->getReceiver()) == 0) { + throw new InvalidArgumentException('Message recipient(s) is empty.'); + } + + if ($message->getContent() == null || strlen($message->getContent()) == 0) { + throw new InvalidArgumentException('Message content is empty.'); + } + } + /** * Send the given notification. * @@ -62,9 +78,13 @@ public function send(object $notifiable, Notification $notification): void } try { - + /** + * @var SmsMessage $message + */ $message = $notification->toSms($notifiable); + $this->initDriver($message->getDriver()); + $this->validate($message); $this->response = $this->driver->send($message); @@ -77,26 +97,4 @@ public function send(object $notifiable, Notification $notification): void : throw new Exception($exception->getMessage(), 0, $exception); } } - - private function logSmsResponse(): void - { - if (config('sms.log', false)) { - Log::debug('SMS Vendor Response: ', ['status_code' => $this->response->status(), 'response' => $this->response->body()]); - } - } - - /** - * this function will validate if the given content satisfy the - * driver required params. - */ - private function validate(SmsMessage $message): void - { - if ($message->getReceiver() == null || strlen($message->getReceiver()) == 0) { - throw new InvalidArgumentException('Message recipient(s) is empty.'); - } - - if ($message->getContent() == null || strlen($message->getContent()) == 0) { - throw new InvalidArgumentException('Message content is empty.'); - } - } } diff --git a/src/SmsMessage.php b/src/SmsMessage.php index f1385e8..4c3244d 100644 --- a/src/SmsMessage.php +++ b/src/SmsMessage.php @@ -2,6 +2,8 @@ namespace Laraflow\Sms; +use Laraflow\Sms\Exceptions\DriverNotFoundException; + class SmsMessage { private ?string $receiver; @@ -10,6 +12,8 @@ class SmsMessage private ?string $content; + private ?string $driver; + public function getReceiver(): ?string { return $this->receiver; @@ -20,7 +24,7 @@ public function getContent(): ?string return $this->content; } - public function getSender() + public function getSender(): ?string { if ($this->sender == null) { @@ -30,16 +34,26 @@ public function getSender() return $this->sender; } + public function getDriver(): ?string + { + if ($this->driver == null) { + + $this->driver = config('sms.default'); + } + + return $this->driver; + } + public function to($receiver): self { - $this->receiver = (string) $receiver; + $this->receiver = (string)$receiver; return $this; } public function message($content): self { - $this->content = (string) $content; + $this->content = (string)$content; return $this; } @@ -50,4 +64,15 @@ public function from($from = null): self return $this; } + + public function vendor($name = null): self + { + $this->driver = ($name != null) ? $name : config('sms.default'); + + if (config("sms.vendors.{$this->driver}.driver") == null) { + throw new DriverNotFoundException("No driver configuration found by `{$this->driver}` name."); + } + + return $this; + } }