Skip to content

Commit

Permalink
Merge pull request #2 from ossycodes/enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
ossycodes authored Sep 21, 2023
2 parents 4c21294 + b41bb0a commit 0cfd421
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 138 deletions.
21 changes: 0 additions & 21 deletions .scrutinizer.yml

This file was deleted.

1 change: 0 additions & 1 deletion .styleci.yml

This file was deleted.

23 changes: 0 additions & 23 deletions .travis.yml

This file was deleted.

7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
}
],
"require": {
"php": ">=7.2",
"illuminate/notifications": "~5.5 || ~6.0 || ~7.0 || ~8.0",
"illuminate/support": "~5.5 || ~6.0 || ~7.0 || ~8.0"
"php": ">=7.2.5",
"ossycodes/nigeriabulksms-php": "^1.0",
"illuminate/notifications": "5.5 - 10.0",
"illuminate/support": "5.5 - 10.0"
},
"require-dev": {
"mockery/mockery": "^1.0",
Expand Down
50 changes: 37 additions & 13 deletions src/NigeriabulksmsChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
namespace NotificationChannels\Nigeriabulksms;

use Illuminate\Notifications\Notification;
use Ossycodes\Nigeriabulksms\Objects\TextMessage;
use Ossycodes\Nigeriabulksms\Client as NigeriabulksmsSDK;
use Ossycodes\Nigeriabulksms\Exceptions\BalanceException;
use Ossycodes\Nigeriabulksms\Exceptions\AuthenticateException;
use Ossycodes\Nigeriabulksms\Exceptions\RequestDeniedException;
use NotificationChannels\Nigeriabulksms\Exceptions\InvalidPhonenumber;
use NotificationChannels\Nigeriabulksms\Exceptions\CouldNotSendNotification;

class NigeriabulksmsChannel
{
/** @var \NotificationChannels\Nigeriabulksms\NigeriabulksmsClient */
/** @var NigeriabulksmsSDK */
protected $client;

public function __construct(NigeriabulksmsClient $client)
public function __construct(NigeriabulksmsSDK $client)
{
$this->client = $client;
}
Expand All @@ -21,7 +27,7 @@ public function __construct(NigeriabulksmsClient $client)
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
*
* @return object with response body data if succesful response from API | empty array if not
* @return object with response body data if succesful
* @throws \NotificationChannels\Nigeriabulksms\Exceptions\CouldNotSendNotification
*/
public function send($notifiable, Notification $notification)
Expand All @@ -32,16 +38,34 @@ public function send($notifiable, Notification $notification)
$message->setRecipients($to);
}

$result = $this->client->send($message);

if (isset($result->status) && strtoupper($result->status) == 'OK') {
return $result;
} else if (isset($result->error)) {
// Message failed, check reason.
throw CouldNotSendNotification::serviceRespondedWithAnError("Message failed - error: $result->error");
} else {
// Could not determine the message response.
throw CouldNotSendNotification::serviceRespondedWithAnError("Unable to process request");
if (empty($message->from)) {
$message->setFrom(config('services.nigeriabulksms.sender'));
}

if (empty($message->recipients)) {
throw InvalidPhonenumber::configurationNotSet();
}

try {

$nigeriaBulksmsMessage = new TextMessage();
$nigeriaBulksmsMessage->sender = $message->getFrom();
$nigeriaBulksmsMessage->recipients = $message->getRecipients();
$nigeriaBulksmsMessage->body = $message->getContent();

return $this->client->message->send($message);

} catch (AuthenticateException $e) {
// That means that your username and/or password is incorrect
throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage());
} catch (BalanceException $e) {
// That means that your balance is insufficient
throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage());
} catch (RequestDeniedException $e) {
// That means that you do not have permission to perform this action
throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage());
} catch (\Exception $e) {
throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage());
}
}
}
73 changes: 0 additions & 73 deletions src/NigeriabulksmsClient.php

This file was deleted.

19 changes: 15 additions & 4 deletions src/NigeriabulksmsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace NotificationChannels\Nigeriabulksms;

use Illuminate\Support\ServiceProvider;
use Ossycodes\Nigeriabulksms\Configuration;
use Ossycodes\Nigeriabulksms\Client as NigeriabulksmsSDK;
use NotificationChannels\Nigeriabulksms\Exceptions\InvalidConfiguration;

class NigeriabulksmsServiceProvider extends ServiceProvider
Expand All @@ -13,15 +15,24 @@ class NigeriabulksmsServiceProvider extends ServiceProvider
public function boot()
{
$this->app->when(NigeriabulksmsChannel::class)
->needs(NigeriabulksmsClient::class)
->needs(NigeriabulksmsSDK::class)
->give(function () {
$config = config('services.nigeriabulksms');
$userName = config('services.nigeriabulksms.username');
$password = config('services.nigeriabulksms.password');
$timeout = config('services.nigeriabulksms.timeout', 10);
$connectionTimeout = config('services.nigeriabulksms.connection_timeout', 2);

if (is_null($config)) {
if (is_null($userName) || is_null($password)) {
throw InvalidConfiguration::configurationNotSet();
}

return new NigeriabulksmsClient($config);
$config = Configuration::getDefaultConfiguration()
->setUsername($userName)
->setPassword($password)
->setTimeout($timeout)
->setConnectionTimeout($connectionTimeout);

return new NigeriabulksmsSDK($config);
});
}
}

0 comments on commit 0cfd421

Please sign in to comment.