From 5f3b5efa7720e58d56e4acb7057696b62bf6ff89 Mon Sep 17 00:00:00 2001 From: ossycodes Date: Thu, 21 Sep 2023 18:13:00 +0100 Subject: [PATCH 1/7] support for laravel 9 and 10 also use official sdk --- composer.json | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 53387bc..f0d6e5e 100644 --- a/composer.json +++ b/composer.json @@ -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", From 01c2af4f6cccc4d95e0944c2a108b2d9acc3fbca Mon Sep 17 00:00:00 2001 From: ossycodes Date: Thu, 21 Sep 2023 18:14:12 +0100 Subject: [PATCH 2/7] switch to official sdk --- src/NigeriabulksmsChannel.php | 49 +++++++++++++----- src/NigeriabulksmsClient.php | 73 --------------------------- src/NigeriabulksmsServiceProvider.php | 19 +++++-- 3 files changed, 51 insertions(+), 90 deletions(-) delete mode 100644 src/NigeriabulksmsClient.php diff --git a/src/NigeriabulksmsChannel.php b/src/NigeriabulksmsChannel.php index 32a4aaa..166ad11 100644 --- a/src/NigeriabulksmsChannel.php +++ b/src/NigeriabulksmsChannel.php @@ -3,14 +3,19 @@ namespace NotificationChannels\Nigeriabulksms; use Illuminate\Notifications\Notification; +use Ossycodes\Nigeriabulksms\Objects\TextMessage; +use Ossycodes\Nigeriabulksms\Client as NigeriabulksmsSDK; +use Ossycodes\Nigeriabulksms\Exceptions\AuthenticateException; +use Ossycodes\Nigeriabulksms\Exceptions\BalanceException; +use Ossycodes\Nigeriabulksms\Exceptions\RequestDeniedException; 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; } @@ -21,7 +26,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) @@ -32,16 +37,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->setRecipients(); + $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->getErrorMessage()); + } catch (BalanceException $e) { + // That means that your balance is insufficient + throw CouldNotSendNotification::serviceRespondedWithAnError($e->getErrorMessage()); + } catch (RequestDeniedException $e) { + // That means that you do not have permission to perform this action + throw CouldNotSendNotification::serviceRespondedWithAnError($e->getErrorMessage()); + } catch (\Exception $e) { + throw CouldNotSendNotification::serviceRespondedWithAnError($e->getErrorMessage()); } } } diff --git a/src/NigeriabulksmsClient.php b/src/NigeriabulksmsClient.php deleted file mode 100644 index a7b56ae..0000000 --- a/src/NigeriabulksmsClient.php +++ /dev/null @@ -1,73 +0,0 @@ -config = $config; - } - - /** - * Send the Message. - * @param NigeriabulksmsMessage $message - * @return - * @throws CouldNotSendNotification - */ - public function send(NigeriabulksmsMessage $message) - { - if (empty($message->from)) { - $message->setFrom($this->config["sender"]); - } - - if (empty($message->recipients)) { - throw InvalidPhonenumber::configurationNotSet(); - } - - try { - - $username = $this->config["username"]; - - $password = $this->config["password"]; - - $sender = $message->getFrom(); - - $smsMessage = $message->getContent(); - - $recipients = $message->getrecipients(); - - $api_url = 'http://portal.nigeriabulksms.com/api/'; - - //Create the message data - $data = array('username'=>$username, 'password'=>$password, 'sender'=>$sender, 'message'=>$smsMessage, 'mobiles'=>$recipients); - - //URL encode the message data - $data = http_build_query($data); - - //Send the message - $ch = curl_init(); // Initialize a cURL connection - - curl_setopt($ch,CURLOPT_URL, $api_url); - curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch,CURLOPT_POST, true); - curl_setopt($ch,CURLOPT_POSTFIELDS, $data); - - $result = curl_exec($ch); - - $result = json_decode($result); //error & errno - - return $result; - - } catch (Exception $exception) { - throw CouldNotSendNotification::serviceRespondedWithAnError($exception); - } - } -} diff --git a/src/NigeriabulksmsServiceProvider.php b/src/NigeriabulksmsServiceProvider.php index e4c5d21..7fcf8f3 100644 --- a/src/NigeriabulksmsServiceProvider.php +++ b/src/NigeriabulksmsServiceProvider.php @@ -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 @@ -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); }); } } From c4be1f3dda4360af3a4c4ca828959f31f0ec5ab0 Mon Sep 17 00:00:00 2001 From: ossycodes Date: Thu, 21 Sep 2023 18:18:37 +0100 Subject: [PATCH 3/7] wip --- src/NigeriabulksmsChannel.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/NigeriabulksmsChannel.php b/src/NigeriabulksmsChannel.php index 166ad11..d5baaf7 100644 --- a/src/NigeriabulksmsChannel.php +++ b/src/NigeriabulksmsChannel.php @@ -46,7 +46,7 @@ public function send($notifiable, Notification $notification) } try { - + $nigeriaBulksmsMessage = new TextMessage(); $nigeriaBulksmsMessage->sender = $message->getFrom(); $nigeriaBulksmsMessage->recipients = $message->setRecipients(); @@ -56,15 +56,15 @@ public function send($notifiable, Notification $notification) } catch (AuthenticateException $e) { // That means that your username and/or password is incorrect - throw CouldNotSendNotification::serviceRespondedWithAnError($e->getErrorMessage()); + throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage()); } catch (BalanceException $e) { // That means that your balance is insufficient - throw CouldNotSendNotification::serviceRespondedWithAnError($e->getErrorMessage()); + throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage()); } catch (RequestDeniedException $e) { // That means that you do not have permission to perform this action - throw CouldNotSendNotification::serviceRespondedWithAnError($e->getErrorMessage()); + throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage()); } catch (\Exception $e) { - throw CouldNotSendNotification::serviceRespondedWithAnError($e->getErrorMessage()); + throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage()); } } } From f5af4583908bbc4573aa045d6c87ae4f5ce3e207 Mon Sep 17 00:00:00 2001 From: ossycodes Date: Thu, 21 Sep 2023 18:21:05 +0100 Subject: [PATCH 4/7] wip --- src/NigeriabulksmsChannel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NigeriabulksmsChannel.php b/src/NigeriabulksmsChannel.php index d5baaf7..a82a043 100644 --- a/src/NigeriabulksmsChannel.php +++ b/src/NigeriabulksmsChannel.php @@ -49,7 +49,7 @@ public function send($notifiable, Notification $notification) $nigeriaBulksmsMessage = new TextMessage(); $nigeriaBulksmsMessage->sender = $message->getFrom(); - $nigeriaBulksmsMessage->recipients = $message->setRecipients(); + $nigeriaBulksmsMessage->recipients = $message->getrecipients(); $nigeriaBulksmsMessage->body = $message->getContent(); return $this->client->message->send($message); From 2cbd5179e690fef122c28b227df06d2ffaaef4ef Mon Sep 17 00:00:00 2001 From: ossycodes Date: Thu, 21 Sep 2023 18:27:38 +0100 Subject: [PATCH 5/7] wip --- src/NigeriabulksmsChannel.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/NigeriabulksmsChannel.php b/src/NigeriabulksmsChannel.php index a82a043..4e5f6e5 100644 --- a/src/NigeriabulksmsChannel.php +++ b/src/NigeriabulksmsChannel.php @@ -5,9 +5,10 @@ use Illuminate\Notifications\Notification; use Ossycodes\Nigeriabulksms\Objects\TextMessage; use Ossycodes\Nigeriabulksms\Client as NigeriabulksmsSDK; -use Ossycodes\Nigeriabulksms\Exceptions\AuthenticateException; 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 From 07ff54d7408ed5a5950bdf77ec23861fccc04e28 Mon Sep 17 00:00:00 2001 From: ossycodes Date: Thu, 21 Sep 2023 18:28:34 +0100 Subject: [PATCH 6/7] wip --- src/NigeriabulksmsChannel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NigeriabulksmsChannel.php b/src/NigeriabulksmsChannel.php index 4e5f6e5..543de25 100644 --- a/src/NigeriabulksmsChannel.php +++ b/src/NigeriabulksmsChannel.php @@ -50,7 +50,7 @@ public function send($notifiable, Notification $notification) $nigeriaBulksmsMessage = new TextMessage(); $nigeriaBulksmsMessage->sender = $message->getFrom(); - $nigeriaBulksmsMessage->recipients = $message->getrecipients(); + $nigeriaBulksmsMessage->recipients = $message->getRecipients(); $nigeriaBulksmsMessage->body = $message->getContent(); return $this->client->message->send($message); From b41bb0a47ba9b2dc915baeb631feae14f39c562a Mon Sep 17 00:00:00 2001 From: ossycodes Date: Thu, 21 Sep 2023 18:29:34 +0100 Subject: [PATCH 7/7] cleanup --- .scrutinizer.yml | 21 --------------------- .styleci.yml | 1 - .travis.yml | 23 ----------------------- 3 files changed, 45 deletions(-) delete mode 100644 .scrutinizer.yml delete mode 100644 .styleci.yml delete mode 100644 .travis.yml diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index 6fad5be..0000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,21 +0,0 @@ -filter: - excluded_paths: [tests/*] - -checks: - php: - remove_extra_empty_lines: true - remove_php_closing_tag: true - remove_trailing_whitespace: true - fix_use_statements: - remove_unused: true - preserve_multiple: false - preserve_blanklines: true - order_alphabetically: true - fix_php_opening_tag: true - fix_linefeed: true - fix_line_ending: true - fix_identation_4spaces: true - fix_doc_comments: true - -tools: - external_code_coverage: true diff --git a/.styleci.yml b/.styleci.yml deleted file mode 100644 index 0285f17..0000000 --- a/.styleci.yml +++ /dev/null @@ -1 +0,0 @@ -preset: laravel diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 72a3670..0000000 --- a/.travis.yml +++ /dev/null @@ -1,23 +0,0 @@ -language: php - -php: - - 7.2 - - 7.3 - - 7.4 - -env: - matrix: - - COMPOSER_FLAGS="--prefer-lowest" - - COMPOSER_FLAGS="" - -before_script: - - travis_retry composer self-update - - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source - -script: - - composer validate - - composer test:coverage - -after_script: - - wget https://scrutinizer-ci.com/ocular.phar - - php ocular.phar code-coverage:upload --format=php-clover coverage.clover