Skip to content

Commit

Permalink
notification class vendor swap added
Browse files Browse the repository at this point in the history
  • Loading branch information
hafijul233 committed Apr 7, 2024
1 parent 095430f commit ff93b12
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 75 deletions.
75 changes: 75 additions & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
@@ -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`<br>`SMS_AFRICA_TALKING_USERNAME=null` |
| `clickatell` | `SMS_CLICKATELL_API_KEY=null` |
| `clicksend` | `SMS_CLICKSEND_USERNAME=null`<br>`SMS_CLICKSEND_PASSWORD=null` |
| `infobip` | `SMS_INFOBIP_API_TOKEN=null` |
| `messagebird` | `SMS_MESSAGE_BIRD_ACCESS_KEY=null` |
| `smsbroadcast` | `SMS_SMSBROADCAST_USERNAME=null`<br>`SMS_SMSBROADCAST_PASSWORD=null` |
| `telnyx` | `SMS_TELNYX_API_TOKEN=null` |
| `twilio` | `SMS_TWILIO_USERNAME=null`<br>`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
}
```
37 changes: 0 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`<br>`SMS_AFRICA_TALKING_USERNAME=null` |
| `clickatell` | `SMS_CLICKATELL_API_KEY=null` |
| `clicksend` | `SMS_CLICKSEND_USERNAME=null`<br>`SMS_CLICKSEND_PASSWORD=null` |
| `infobip` | `SMS_INFOBIP_API_TOKEN=null` |
| `messagebird` | `SMS_MESSAGE_BIRD_ACCESS_KEY=null` |
| `smsbroadcast` | `SMS_SMSBROADCAST_USERNAME=null`<br>`SMS_SMSBROADCAST_PASSWORD=null` |
| `telnyx` | `SMS_TELNYX_API_TOKEN=null` |
| `twilio` | `SMS_TWILIO_USERNAME=null`<br>`SMS_TWILIO_PASSWORD=null` |


### Auditory

This document was last updated at <strong><i>{docsify-updated}</i></strong>.
5 changes: 3 additions & 2 deletions _sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
width: 56px !important;
height: auto;
}
.markdown-section table {
display: table !important;
}
</style>
</head>
<body>
Expand Down
64 changes: 31 additions & 33 deletions src/SmsChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,36 +20,51 @@ 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.");
}

$mode = config('sms.mode', 'sandbox');

$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.
*
Expand All @@ -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);
Expand All @@ -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.');
}
}
}
31 changes: 28 additions & 3 deletions src/SmsMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Laraflow\Sms;

use Laraflow\Sms\Exceptions\DriverNotFoundException;

class SmsMessage
{
private ?string $receiver;
Expand All @@ -10,6 +12,8 @@ class SmsMessage

private ?string $content;

private ?string $driver;

public function getReceiver(): ?string
{
return $this->receiver;
Expand All @@ -20,7 +24,7 @@ public function getContent(): ?string
return $this->content;
}

public function getSender()
public function getSender(): ?string
{
if ($this->sender == null) {

Expand All @@ -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;
}
Expand All @@ -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;
}
}

0 comments on commit ff93b12

Please sign in to comment.