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 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", diff --git a/src/NigeriabulksmsChannel.php b/src/NigeriabulksmsChannel.php index 32a4aaa..543de25 100644 --- a/src/NigeriabulksmsChannel.php +++ b/src/NigeriabulksmsChannel.php @@ -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; } @@ -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) @@ -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()); } } } 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); }); } }