From 91c6342fb92a64b0a942b7602ec3939420a790bf Mon Sep 17 00:00:00 2001 From: Hamoud Alhoqbani Date: Tue, 15 Aug 2017 00:48:17 -0700 Subject: [PATCH 01/16] Create Message Class --- src/MobilyWsMessage.php | 45 +++++++++++++++++++++++++++++++++++++++++ tests/MessageTest.php | 45 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 src/MobilyWsMessage.php create mode 100644 tests/MessageTest.php diff --git a/src/MobilyWsMessage.php b/src/MobilyWsMessage.php new file mode 100644 index 0000000..095a1c3 --- /dev/null +++ b/src/MobilyWsMessage.php @@ -0,0 +1,45 @@ +msg = $msg; + } + + /** + * Set the Content of the SMS message. + * + * @param $msg + * + * @return $this + */ + public function msg($msg) + { + $this->msg = $msg; + + return $this; + } +} diff --git a/tests/MessageTest.php b/tests/MessageTest.php new file mode 100644 index 0000000..01c8f3f --- /dev/null +++ b/tests/MessageTest.php @@ -0,0 +1,45 @@ +assertInstanceOf(MobilyWsMessage::class, $message); + $this->assertEquals('Message content is here', $message->msg); + } + + /** @test */ + public function it_can_set_content() + { + $message = new MobilyWsMessage(); + $message->msg('Message content is here'); + + $this->assertEquals('Message content is here', $message->msg); + } + + /** @test */ + public function it_can_create_new_message() + { + $message = MobilyWsMessage::create('Message content is here'); + + $this->assertEquals('Message content is here', $message->msg); + } + +} From 38718ee1bfd275d8fec6a349f2f5ac9e92bf1318 Mon Sep 17 00:00:00 2001 From: Hamoud Alhoqbani Date: Tue, 15 Aug 2017 01:25:08 -0700 Subject: [PATCH 02/16] Make channel class accept message instance --- src/MobilyWsChannel.php | 25 +++++++++++++++++++------ tests/ChannelTest.php | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/MobilyWsChannel.php b/src/MobilyWsChannel.php index d8126dd..7dd978f 100644 --- a/src/MobilyWsChannel.php +++ b/src/MobilyWsChannel.php @@ -3,8 +3,8 @@ namespace NotificationChannels\MobilyWs; use Illuminate\Events\Dispatcher; -use Illuminate\Notifications\Notification; use Illuminate\Notifications\Events\NotificationFailed; +use Illuminate\Notifications\Notification; use NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification; class MobilyWsChannel @@ -43,11 +43,24 @@ public function send($notifiable, Notification $notification) throw CouldNotSendNotification::withErrorMessage('MobilyWs notifications must have toMobilyWs method'); } $number = $notifiable->routeNotificationFor('MobilyWs') ?: $notifiable->phone_number; - - $response = $this->api->send([ - 'msg' => $notification->toMobilyWs($notifiable), - 'numbers' => $number, - ]); + + if (is_string($message = $notification->toMobilyWs($notifiable))) { + $response = $this->api->send([ + 'msg' => $message, + 'numbers' => $number, + ]); + } elseif ($message instanceof MobilyWsMessage) { + $response = $this->api->sendMessage([ + 'msg' => $message, + 'numbers' => $number, + ]); + } else { + $errorMessage = sprintf("toMobilyWs must return a string or instance of %s. Instance of %s returned %s", + MobilyWsMessage::class, + gettype($message) + ); + throw CouldNotSendNotification::withErrorMessage($errorMessage); + } if ($response['code'] == 1) { return $response['message']; diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index 1c9afb2..5fcae6f 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -9,6 +9,7 @@ use NotificationChannels\MobilyWs\MobilyWsApi; use NotificationChannels\MobilyWs\MobilyWsChannel; use NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification; +use NotificationChannels\MobilyWs\MobilyWsMessage; /** * @property \Mockery\MockInterface api @@ -41,7 +42,7 @@ public function tearDown() } /** @test */ - public function it_can_send_a_notification() + public function it_can_send_a_notification_with_text() { $params = [ 'msg' => 'Text message', @@ -53,6 +54,23 @@ public function it_can_send_a_notification() $response = $this->channel->send($this->notifiable, $this->notification); $this->assertEquals('تمت عملية الإرسال بنجاح', $response); } + + /** @test */ + public function it_can_send_a_notification_with_instance_of_MobilyWsMessage() + { + $notification = new TestNotificationWithMessageInstance($message = "text message content"); + $params = [ + 'msg' => $notification->toMobilyWs($this->notifiable), + 'numbers' => '966550000000', + ]; + + $this->api->shouldReceive('sendMessage')->with($params)->andReturn(['code' => 1, 'message' => 'تمت عملية الإرسال بنجاح']); + + $response = $this->channel->send($this->notifiable, $notification); + $this->assertEquals('تمت عملية الإرسال بنجاح', $response); + } + + /** @test * @expectedException \NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification; @@ -126,3 +144,23 @@ public function toMobilyWs($notifiable) return 'Text message'; } } + +class TestNotificationWithMessageInstance extends Notification +{ + public $message; + + /** + * TestNotificationWithMessageInstance constructor. + * + * @param $message + */ + public function __construct($message) + { + $this->message = $message; + } + + public function toMobilyWs($notifiable) + { + return new mobilyWsMessage($this->message); + } +} From 6fb1b2ed74ba00166e13e327a9b905da1e618a93 Mon Sep 17 00:00:00 2001 From: Hamoud Alhoqbani Date: Tue, 15 Aug 2017 01:34:30 -0700 Subject: [PATCH 03/16] Refactor test to test more messages types --- tests/ChannelTest.php | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index 5fcae6f..e9ce066 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -29,8 +29,6 @@ protected function setUp() $this->channel = new MobilyWsChannel($this->api, $this->events); - $this->notification = new TestNotification(); - $this->notifiable = new TestNotifiable(); } @@ -44,33 +42,33 @@ public function tearDown() /** @test */ public function it_can_send_a_notification_with_text() { + $notificationWithText = new TestNotification('Text message as a string'); $params = [ - 'msg' => 'Text message', + 'msg' => 'Text message as a string', 'numbers' => '966550000000', ]; $this->api->shouldReceive('send')->with($params)->andReturn(['code' => 1, 'message' => 'تمت عملية الإرسال بنجاح']); - $response = $this->channel->send($this->notifiable, $this->notification); + $response = $this->channel->send($this->notifiable, $notificationWithText); $this->assertEquals('تمت عملية الإرسال بنجاح', $response); } - - /** @test */ + + /** @test */ public function it_can_send_a_notification_with_instance_of_MobilyWsMessage() { - $notification = new TestNotificationWithMessageInstance($message = "text message content"); + $messageInstance = new MobilyWsMessage('Text from message instance'); + $notificationWithMessageInstance = new TestNotification($messageInstance); $params = [ - 'msg' => $notification->toMobilyWs($this->notifiable), + 'msg' => $messageInstance, 'numbers' => '966550000000', ]; $this->api->shouldReceive('sendMessage')->with($params)->andReturn(['code' => 1, 'message' => 'تمت عملية الإرسال بنجاح']); - $response = $this->channel->send($this->notifiable, $notification); + $response = $this->channel->send($this->notifiable, $notificationWithMessageInstance); $this->assertEquals('تمت عملية الإرسال بنجاح', $response); } - - /** @test * @expectedException \NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification; @@ -84,7 +82,7 @@ public function it_fires_failure_event_on_failure() $this->api->shouldReceive('send')->with($params)->andReturn(['code' => 5, 'message' => 'كلمة المرور الخاصة بالحساب غير صحيحة']); try { - $this->channel->send($this->notifiable, $this->notification); + $this->channel->send($this->notifiable, new TestNotification("Text message")); } catch (CouldNotSendNotification $e) { $this->events->shouldHaveReceived('fire'); } @@ -100,7 +98,7 @@ public function it_throw_an_exception_when_mobily_ws_return_an_error() $this->api->shouldReceive('send')->with($params)->andReturn(['code' => 3, 'message' => 'رصيدك غير كافي لإتمام عملية الإرسال']); try { - $this->channel->send($this->notifiable, $this->notification); + $this->channel->send($this->notifiable, new TestNotification("Text message")); } catch (CouldNotSendNotification $e) { $this->assertContains('رصيدك غير كافي لإتمام عملية الإرسال', $e->getMessage()); @@ -138,17 +136,9 @@ public function routeNotificationForMobilyWs() } class TestNotification extends Notification -{ - public function toMobilyWs($notifiable) - { - return 'Text message'; - } -} - -class TestNotificationWithMessageInstance extends Notification { public $message; - + /** * TestNotificationWithMessageInstance constructor. * @@ -158,9 +148,9 @@ public function __construct($message) { $this->message = $message; } - + public function toMobilyWs($notifiable) { - return new mobilyWsMessage($this->message); + return $this->message; } } From ab1a6d354b0204abdb29e087d1c87173986261e4 Mon Sep 17 00:00:00 2001 From: Hamoud Alhoqbani Date: Tue, 15 Aug 2017 01:44:18 -0700 Subject: [PATCH 04/16] Add new Test for channel to check throwing exception when message type is not supported --- src/MobilyWsChannel.php | 2 +- tests/ChannelTest.php | 27 +++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/MobilyWsChannel.php b/src/MobilyWsChannel.php index 7dd978f..0787704 100644 --- a/src/MobilyWsChannel.php +++ b/src/MobilyWsChannel.php @@ -55,7 +55,7 @@ public function send($notifiable, Notification $notification) 'numbers' => $number, ]); } else { - $errorMessage = sprintf("toMobilyWs must return a string or instance of %s. Instance of %s returned %s", + $errorMessage = sprintf("toMobilyWs must return a string or instance of %s. Instance of %s returned", MobilyWsMessage::class, gettype($message) ); diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index e9ce066..42f00e0 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -70,6 +70,29 @@ public function it_can_send_a_notification_with_instance_of_MobilyWsMessage() $this->assertEquals('تمت عملية الإرسال بنجاح', $response); } + /** @test */ + public function it_throw_an_exception_when_given_a_message_other_than_string_or_message_instance() + { + $notificationWithArray = new TestNotification($array = ['text message from array']); + $params = [ + 'msg' => $array, + 'numbers' => '966550000000', + ]; + + try { + $this->channel->send($this->notifiable, $notificationWithArray); + } catch (CouldNotSendNotification $e) { + $this->assertContains( + 'toMobilyWs must return a string or instance of NotificationChannels\MobilyWs\MobilyWsMessage. Instance of array returned', + $e->getMessage() + ); + + return; + } + + $this->fail('CouldNotSendNotification exception was not raised'); + } + /** @test * @expectedException \NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification; */ @@ -82,7 +105,7 @@ public function it_fires_failure_event_on_failure() $this->api->shouldReceive('send')->with($params)->andReturn(['code' => 5, 'message' => 'كلمة المرور الخاصة بالحساب غير صحيحة']); try { - $this->channel->send($this->notifiable, new TestNotification("Text message")); + $this->channel->send($this->notifiable, new TestNotification('Text message')); } catch (CouldNotSendNotification $e) { $this->events->shouldHaveReceived('fire'); } @@ -98,7 +121,7 @@ public function it_throw_an_exception_when_mobily_ws_return_an_error() $this->api->shouldReceive('send')->with($params)->andReturn(['code' => 3, 'message' => 'رصيدك غير كافي لإتمام عملية الإرسال']); try { - $this->channel->send($this->notifiable, new TestNotification("Text message")); + $this->channel->send($this->notifiable, new TestNotification('Text message')); } catch (CouldNotSendNotification $e) { $this->assertContains('رصيدك غير كافي لإتمام عملية الإرسال', $e->getMessage()); From 0d4e3e8619c3c97e560569a7d48f9f8a13ef4f01 Mon Sep 17 00:00:00 2001 From: Hamoud Alhoqbani Date: Tue, 15 Aug 2017 02:11:43 -0700 Subject: [PATCH 05/16] Extract method to dispatch request to the api --- src/MobilyWsChannel.php | 45 ++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/src/MobilyWsChannel.php b/src/MobilyWsChannel.php index 0787704..15f0133 100644 --- a/src/MobilyWsChannel.php +++ b/src/MobilyWsChannel.php @@ -39,12 +39,36 @@ public function __construct(MobilyWsApi $mobilyWs, Dispatcher $events) */ public function send($notifiable, Notification $notification) { - if (! method_exists($notification, 'toMobilyWs')) { + if (!method_exists($notification, 'toMobilyWs')) { throw CouldNotSendNotification::withErrorMessage('MobilyWs notifications must have toMobilyWs method'); } + $message = $notification->toMobilyWs($notifiable); $number = $notifiable->routeNotificationFor('MobilyWs') ?: $notifiable->phone_number; - - if (is_string($message = $notification->toMobilyWs($notifiable))) { + // TODO Validate Number + + $response = $this->dispatchRequest($message, $number); + + if ($response['code'] == 1) { + return $response['message']; + } + $this->events->fire( + new NotificationFailed($notifiable, $notification, 'mobily-ws', $response) + ); + + throw CouldNotSendNotification::mobilyWsRespondedWithAnError($response['code'], $response['message']); + } + + /** + * @param $message + * @param $number + * + * @return array + * + * @throws CouldNotSendNotification + */ + private function dispatchRequest($message, $number) + { + if (is_string($message)) { $response = $this->api->send([ 'msg' => $message, 'numbers' => $number, @@ -55,20 +79,13 @@ public function send($notifiable, Notification $notification) 'numbers' => $number, ]); } else { - $errorMessage = sprintf("toMobilyWs must return a string or instance of %s. Instance of %s returned", + $errorMessage = sprintf('toMobilyWs must return a string or instance of %s. Instance of %s returned', MobilyWsMessage::class, - gettype($message) - ); + gettype($message) + ); throw CouldNotSendNotification::withErrorMessage($errorMessage); } - if ($response['code'] == 1) { - return $response['message']; - } - $this->events->fire( - new NotificationFailed($notifiable, $notification, 'mobily-ws', $response) - ); - - throw CouldNotSendNotification::mobilyWsRespondedWithAnError($response['code'], $response['message']); + return $response; } } From 2f8465c21360ca0b49cda6f926d91f0df210461b Mon Sep 17 00:00:00 2001 From: Hamoud Alhoqbani Date: Tue, 15 Aug 2017 02:19:38 -0700 Subject: [PATCH 06/16] Add sendString method to the api to process string message --- src/MobilyWsApi.php | 26 ++++++++++++++++++++------ src/MobilyWsChannel.php | 2 +- tests/ApiTest.php | 4 ++-- tests/ChannelTest.php | 8 ++++---- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/MobilyWsApi.php b/src/MobilyWsApi.php index e36b920..1568327 100644 --- a/src/MobilyWsApi.php +++ b/src/MobilyWsApi.php @@ -25,19 +25,33 @@ public function __construct(MobilyWsConfig $config, HttpClient $http) $this->http = $http; $this->config = $config; } - + /** - * @param array $params + * Send request with string message + * + * @param $params * * @return array + */ + public function sendString($params) + { + $payload = $this->preparePayload($params); + return $this->send($payload); + } + + /** + * Send request to mobily.ws + * + * @param array $payload + * + * @return array + * @throws \NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification + * @internal param array $params * - * @throws CouldNotSendNotification */ - public function send(array $params) + public function send(array $payload) { $endpoint = 'msgSend.php'; - $payload = $this->preparePayload($params); - try { $response = $this->http->post($endpoint, $payload); diff --git a/src/MobilyWsChannel.php b/src/MobilyWsChannel.php index 15f0133..22c184e 100644 --- a/src/MobilyWsChannel.php +++ b/src/MobilyWsChannel.php @@ -69,7 +69,7 @@ public function send($notifiable, Notification $notification) private function dispatchRequest($message, $number) { if (is_string($message)) { - $response = $this->api->send([ + $response = $this->api->sendString([ 'msg' => $message, 'numbers' => $number, ]); diff --git a/tests/ApiTest.php b/tests/ApiTest.php index aff9225..fa44813 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -49,7 +49,7 @@ public function it_can_send_request_to_mobily_ws_api() } /** @test */ - public function it_send_request_with_correct_params() + public function it_send_request_with_correct_params_when_given_string_message() { $container = []; $history = Middleware::history($container); @@ -72,7 +72,7 @@ public function it_send_request_with_correct_params() 'numbers' => '966550000000', ]; - $api->send($params); + $api->sendString($params); /** @var Request $request */ $request = $container[0]['request']; diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index 42f00e0..2927cec 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -40,7 +40,7 @@ public function tearDown() } /** @test */ - public function it_can_send_a_notification_with_text() + public function it_can_send_a_notification_with_string_text() { $notificationWithText = new TestNotification('Text message as a string'); $params = [ @@ -48,7 +48,7 @@ public function it_can_send_a_notification_with_text() 'numbers' => '966550000000', ]; - $this->api->shouldReceive('send')->with($params)->andReturn(['code' => 1, 'message' => 'تمت عملية الإرسال بنجاح']); + $this->api->shouldReceive('sendString')->with($params)->andReturn(['code' => 1, 'message' => 'تمت عملية الإرسال بنجاح']); $response = $this->channel->send($this->notifiable, $notificationWithText); $this->assertEquals('تمت عملية الإرسال بنجاح', $response); @@ -102,7 +102,7 @@ public function it_fires_failure_event_on_failure() 'msg' => 'Text message', 'numbers' => '966550000000', ]; - $this->api->shouldReceive('send')->with($params)->andReturn(['code' => 5, 'message' => 'كلمة المرور الخاصة بالحساب غير صحيحة']); + $this->api->shouldReceive('sendString')->with($params)->andReturn(['code' => 5, 'message' => 'كلمة المرور الخاصة بالحساب غير صحيحة']); try { $this->channel->send($this->notifiable, new TestNotification('Text message')); @@ -118,7 +118,7 @@ public function it_throw_an_exception_when_mobily_ws_return_an_error() 'msg' => 'Text message', 'numbers' => '966550000000', ]; - $this->api->shouldReceive('send')->with($params)->andReturn(['code' => 3, 'message' => 'رصيدك غير كافي لإتمام عملية الإرسال']); + $this->api->shouldReceive('sendString')->with($params)->andReturn(['code' => 3, 'message' => 'رصيدك غير كافي لإتمام عملية الإرسال']); try { $this->channel->send($this->notifiable, new TestNotification('Text message')); From 1f27260987d7a61b7dc8c93e27834b50e112327a Mon Sep 17 00:00:00 2001 From: Hamoud Alhoqbani Date: Tue, 15 Aug 2017 02:30:31 -0700 Subject: [PATCH 07/16] add sendMessage method to the api to process MobilyWsMessage instance --- src/MobilyWsApi.php | 21 +++++++++++++++++++++ src/MobilyWsChannel.php | 5 +---- tests/ApiTest.php | 38 ++++++++++++++++++++++++++++++++++++++ tests/ChannelTest.php | 10 ++++------ 4 files changed, 64 insertions(+), 10 deletions(-) diff --git a/src/MobilyWsApi.php b/src/MobilyWsApi.php index 1568327..c3f0c99 100644 --- a/src/MobilyWsApi.php +++ b/src/MobilyWsApi.php @@ -39,6 +39,27 @@ public function sendString($params) return $this->send($payload); } + /** + * Send request with MobilyWsMessage instance + * + * @param MobilyWsMessage $message + * + * @param $number + * + * @return array + * @internal param $params + */ + public function sendMessage(MobilyWsMessage $message, $number) + { + $params = [ + 'msg' => $message->msg, + 'numbers' => $number, + ]; + + $payload = $this->preparePayload($params); + return $this->send($payload); + } + /** * Send request to mobily.ws * diff --git a/src/MobilyWsChannel.php b/src/MobilyWsChannel.php index 22c184e..6a50a06 100644 --- a/src/MobilyWsChannel.php +++ b/src/MobilyWsChannel.php @@ -74,10 +74,7 @@ private function dispatchRequest($message, $number) 'numbers' => $number, ]); } elseif ($message instanceof MobilyWsMessage) { - $response = $this->api->sendMessage([ - 'msg' => $message, - 'numbers' => $number, - ]); + $response = $this->api->sendMessage($message, $number); } else { $errorMessage = sprintf('toMobilyWs must return a string or instance of %s. Instance of %s returned', MobilyWsMessage::class, diff --git a/tests/ApiTest.php b/tests/ApiTest.php index fa44813..d2de16b 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -12,6 +12,7 @@ use NotificationChannels\MobilyWs\MobilyWsApi; use NotificationChannels\MobilyWs\MobilyWsConfig; use NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification; +use NotificationChannels\MobilyWs\MobilyWsMessage; class ApiTest extends \PHPUnit_Framework_TestCase { @@ -82,6 +83,43 @@ public function it_send_request_with_correct_params_when_given_string_message() $this->assertSame('/api/msgSend.php', $request->getRequestTarget()); $this->assertArraySubset($params, parse_query($request->getBody()->getContents())); } + + /** @test */ + public function it_send_request_with_correct_params_when_given_MobilyWsMessage_instance() + { + $container = []; + $history = Middleware::history($container); + $mock = new MockHandler([ + new Response(200, [], 1), + ]); + $stack = HandlerStack::create($mock); + $stack->push($history); + $options = [ + 'base_uri' => 'http://mobily.ws/api/', + 'handler' => $stack, + ]; + $client = new Client($options); + $mobileWsConfig = new MobilyWsConfig($this->getConfigs()); + $api = new MobilyWsApi($mobileWsConfig, $client); + + $message = new MobilyWsMessage('SMS Text Message'); + $params = [ + 'applicationType' => '68', + 'lang' => '3', + 'msg' => $message->msg, + 'numbers' => $number = '966550000000', + ]; + + $api->sendMessage($message, $number); + + /** @var Request $request */ + $request = $container[0]['request']; + + $this->assertCount(1, $container); + $this->assertSame('POST', $request->getMethod()); + $this->assertSame('/api/msgSend.php', $request->getRequestTarget()); + $this->assertArraySubset($params, parse_query($request->getBody()->getContents())); + } /** @test */ public function it_throw_an_exception_when_response_is_not_ok() diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index 2927cec..cee7682 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -59,13 +59,11 @@ public function it_can_send_a_notification_with_instance_of_MobilyWsMessage() { $messageInstance = new MobilyWsMessage('Text from message instance'); $notificationWithMessageInstance = new TestNotification($messageInstance); - $params = [ - 'msg' => $messageInstance, - 'numbers' => '966550000000', - ]; - - $this->api->shouldReceive('sendMessage')->with($params)->andReturn(['code' => 1, 'message' => 'تمت عملية الإرسال بنجاح']); + $this->api->shouldReceive('sendMessage') + ->with($messageInstance, $this->notifiable->mobile_number) + ->andReturn(['code' => 1, 'message' => 'تمت عملية الإرسال بنجاح']); + $response = $this->channel->send($this->notifiable, $notificationWithMessageInstance); $this->assertEquals('تمت عملية الإرسال بنجاح', $response); } From 96d481166cca5d5db0d9e99cffc83613b37850aa Mon Sep 17 00:00:00 2001 From: Hamoud Alhoqbani Date: Tue, 15 Aug 2017 03:21:49 -0700 Subject: [PATCH 08/16] update readme --- README.md | 80 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 63 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 911bb3f..5d3315b 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,6 @@ [![Latest Version on Packagist](https://img.shields.io/packagist/v/alhoqbani/laravel-mobily-ws-notification.svg?style=flat-square)](https://packagist.org/packages/alhoqbani/laravel-mobily-ws-notification) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) [![Build Status](https://img.shields.io/travis/alhoqbani/laravel-mobily-ws-notification/master.svg?style=flat-square)](https://travis-ci.org/alhoqbani/laravel-mobily-ws-notification) -[![StyleCI](https://styleci.io/repos/100258454/shield)](https://styleci.io/repos/100258454) -[![SensioLabsInsight](https://img.shields.io/sensiolabs/i/:sensio_labs_id.svg?style=flat-square)](https://insight.sensiolabs.com/projects/:sensio_labs_id) -[![Quality Score](https://img.shields.io/scrutinizer/g/alhoqbani/laravel-mobily-ws-notification.svg?style=flat-square)](https://scrutinizer-ci.com/g/alhoqbani/laravel-mobily-ws-notification) [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/alhoqbani/laravel-mobily-ws-notification/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/alhoqbani/laravel-mobily-ws-notification/?branch=master) [![Total Downloads](https://img.shields.io/packagist/dt/alhoqbani/laravel-mobily-ws-notification.svg?style=flat-square)](https://packagist.org/packages/alhoqbani/laravel-mobily-ws-notification) @@ -15,8 +12,13 @@ This package makes it easy to send notifications using [MobilyWs](https://www.mo ## Contents - [Installation](#installation) - - [Setting up the MobilyWs service](#setting-up-the-MobilyWs-service) + - [Package Installation](#package-installation) + - [Set up mobily.ws account](#set-up-mobily.ws-account) - [Usage](#usage) + - [Credentials](#credentials) + - [Create Notification](#create-notification) + - [Routing SMS Notifications](#routing-sms-notifications) + - [Sending SMS](#sending-sms) - [Available Message methods](#available-message-methods) - [TODO](#todo) - [Changelog](#changelog) @@ -27,11 +29,15 @@ This package makes it easy to send notifications using [MobilyWs](https://www.mo ## Installation -Install using composer: + +### Package Installation + +Install the package using composer: ```bash composer require alhoqbani/laravel-mobily-ws-notification ``` -Add service provider to your array of providers in `config/app.php` +Add service provider to your array of providers in `config/app.php` +> You don't need to do this step for laravel 5.5+ ```php NotificationChannels\MobilyWs\MobilyWsServiceProvider::class, ``` @@ -39,35 +45,43 @@ Publish the configuration file: ```bash php artisan vendor:publish --provider="NotificationChannels\MobilyWs\MobilyWsServiceProvider" ``` -### Setting up the Mobily.ws account +### Set up mobily.ws account You must have an account with [MobilyWs](https://www.mobily.ws) to be able to use this package. > This package has no affiliation with mobily.ws whatsoever. -## Usage -### Add your mobily.ws credentials to your `.env` file. -```php -MOBILY_WS_MOBILE= +#### Credentials. +You must add mobily.ws credentials to your `.env` file. + +``` +// Mobile number and password used for log in. +MOBILY_WS_MOBILE= MOBILY_WS_PASSWORD= -// Name/Number of Sender must be approved by mobily.ws for GCC +// name/number of the sender which must be approved by mobily.ws for GCC MOBILY_WS_SENDER= ``` -### Make a new notification class using laravel artisan: +## Usage + +### Create new notification: +Make a new notification class using laravel artisan ```bash php artisan make:notification SmsNewUser ``` -### Configure the notification class to use MobilyWsChannel: +Configure the notification class to use MobilyWsChannel: -The `toMobilyWs` method should return a string of the text message to be sent. +The `toMobilyWs` method should return a string of the text message to be sent or an instance of `MobilyWsMessage`. + +See [Available Message methods](#available-message-methods) for more details. ```php notify(new SmsNewUser()); ``` +### Available Message methods +In your notification, you must define a method `toMobilyWs` which will receive the notifiable entity (e.g User model) and an instance of `MobilyWsMessage`. + +This method should return the text of the message to be sent as an SMS to mobily.ws or an instance of `MobilyWsMessage`. + +```php + /** + * Get the text message of the SMS. + * + * @param mixed $notifiable + * @return string|MobilyWsMessage + */ + public function toMobilyWs($notifiable) + { + return MobilyWsMessage::create("Text message"); + } +``` +You can also pass the message to `MobilyWsMessage` constructor: + +`return new MobilyWsMessage("Text message");` + +or set the text message using the `msg()` method: +```php + public function toMobilyWs($notifiable, MobilyWsMessage $msg) + { + return $msg->text($this->message); + } +``` +Method `toMobilyWs` will receive an instance of `MobilyWsMessage` as the 2nd argument. +#### list of available methods : +> Coming soon. ## TODO - [ ] Validate mobile numbers From 84cedd7da976e4652e98d819c44fc40a673ffff3 Mon Sep 17 00:00:00 2001 From: Hamoud Alhoqbani Date: Tue, 15 Aug 2017 03:37:01 -0700 Subject: [PATCH 09/16] Pass an instance of MobilyWsMessage to toMobilyWs method --- src/MobilyWsChannel.php | 2 +- tests/ChannelTest.php | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/MobilyWsChannel.php b/src/MobilyWsChannel.php index 6a50a06..c49e03f 100644 --- a/src/MobilyWsChannel.php +++ b/src/MobilyWsChannel.php @@ -42,7 +42,7 @@ public function send($notifiable, Notification $notification) if (!method_exists($notification, 'toMobilyWs')) { throw CouldNotSendNotification::withErrorMessage('MobilyWs notifications must have toMobilyWs method'); } - $message = $notification->toMobilyWs($notifiable); + $message = $notification->toMobilyWs($notifiable, new MobilyWsMessage()); $number = $notifiable->routeNotificationFor('MobilyWs') ?: $notifiable->phone_number; // TODO Validate Number diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index cee7682..89ed20a 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -142,6 +142,19 @@ public function it_throw_an_exception_when_toMobilyWs_method_does_not_exist() $this->fail('CouldNotSendNotification exception was not raised'); } + + /** @test */ + public function it_passes_an_instance_of_MobilyWsMessage_when_calling_toMobilyWs_method() + { + $notification = Mockery::mock(TestNotification::class); + + $notification->shouldReceive('toMobilyWs') + ->with($this->notifiable, Mockery::type(MobilyWsMessage::class)) + ->andReturn(new MobilyWsMessage()); + $this->api->shouldReceive('sendMessage')->andReturn(['code' => 1, 'message' => 'ok']); + + $this->channel->send($this->notifiable, $notification); + } } class TestNotifiable From 8cb87d7861834e448af0236c5e11401473395988 Mon Sep 17 00:00:00 2001 From: Hamoud Alhoqbani Date: Tue, 15 Aug 2017 03:39:19 -0700 Subject: [PATCH 10/16] Rename method msg to text --- src/MobilyWsApi.php | 2 +- src/MobilyWsMessage.php | 20 ++++++++++---------- tests/ApiTest.php | 2 +- tests/MessageTest.php | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/MobilyWsApi.php b/src/MobilyWsApi.php index c3f0c99..1739630 100644 --- a/src/MobilyWsApi.php +++ b/src/MobilyWsApi.php @@ -52,7 +52,7 @@ public function sendString($params) public function sendMessage(MobilyWsMessage $message, $number) { $params = [ - 'msg' => $message->msg, + 'msg' => $message->text, 'numbers' => $number, ]; diff --git a/src/MobilyWsMessage.php b/src/MobilyWsMessage.php index 095a1c3..5c30657 100644 --- a/src/MobilyWsMessage.php +++ b/src/MobilyWsMessage.php @@ -5,40 +5,40 @@ class MobilyWsMessage { /** @var string */ - public $msg; + public $text; /** * Create new instance of mobilyWsMessage. * - * @param string $msg + * @param string $text * * @return static */ - public static function create($msg = '') + public static function create($text = '') { - return new static($msg); + return new static($text); } /** * MobilyWsMessage constructor. * - * @param string $msg + * @param string $text */ - public function __construct($msg = '') + public function __construct($text = '') { - $this->msg = $msg; + $this->text = $text; } /** * Set the Content of the SMS message. * - * @param $msg + * @param $text * * @return $this */ - public function msg($msg) + public function text($text) { - $this->msg = $msg; + $this->text = $text; return $this; } diff --git a/tests/ApiTest.php b/tests/ApiTest.php index d2de16b..0987ff0 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -106,7 +106,7 @@ public function it_send_request_with_correct_params_when_given_MobilyWsMessage_i $params = [ 'applicationType' => '68', 'lang' => '3', - 'msg' => $message->msg, + 'msg' => $message->text, 'numbers' => $number = '966550000000', ]; diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 01c8f3f..7ba8ff6 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -22,16 +22,16 @@ public function it_can_set_message_content_when_constructing() $message = new MobilyWsMessage('Message content is here'); $this->assertInstanceOf(MobilyWsMessage::class, $message); - $this->assertEquals('Message content is here', $message->msg); + $this->assertEquals('Message content is here', $message->text); } /** @test */ public function it_can_set_content() { $message = new MobilyWsMessage(); - $message->msg('Message content is here'); + $message->text('Message content is here'); - $this->assertEquals('Message content is here', $message->msg); + $this->assertEquals('Message content is here', $message->text); } /** @test */ @@ -39,7 +39,7 @@ public function it_can_create_new_message() { $message = MobilyWsMessage::create('Message content is here'); - $this->assertEquals('Message content is here', $message->msg); + $this->assertEquals('Message content is here', $message->text); } } From 3333a47bed9a2b27ceae7138f0981487381972c3 Mon Sep 17 00:00:00 2001 From: Hamoud Alhoqbani Date: Tue, 15 Aug 2017 05:31:18 -0700 Subject: [PATCH 11/16] Add time method to set the message time --- src/MobilyWsApi.php | 2 +- src/MobilyWsMessage.php | 33 +++++++++++++++++++++++ tests/MessageTest.php | 58 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/src/MobilyWsApi.php b/src/MobilyWsApi.php index 1739630..ad001fc 100644 --- a/src/MobilyWsApi.php +++ b/src/MobilyWsApi.php @@ -106,7 +106,7 @@ protected function preparePayload($params) 'msg' => $params['msg'], 'numbers' => $params['numbers'], // For development to avoid charges -// 'dateSend' => \Carbon\Carbon::parse('+1 month')->format('m/d/Y'), + 'dateSend' => \Carbon\Carbon::parse('+1 month')->format('m/d/Y'), ]; return array_merge( diff --git a/src/MobilyWsMessage.php b/src/MobilyWsMessage.php index 5c30657..0af2eb4 100644 --- a/src/MobilyWsMessage.php +++ b/src/MobilyWsMessage.php @@ -2,11 +2,19 @@ namespace NotificationChannels\MobilyWs; +use Carbon\Carbon; +use DateTime; +use DateTimeInterface; +use NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification; + class MobilyWsMessage { /** @var string */ public $text; + /** @var Carbon */ + public $time; + /** * Create new instance of mobilyWsMessage. * @@ -42,4 +50,29 @@ public function text($text) return $this; } + + /** + * @param DateTime|Carbon|int $time + * + * @return $this + * + * @throws CouldNotSendNotification + */ + public function time($time) + { + if ($time instanceof DateTimeInterface) { + return $this->time($time->getTimestamp()); + } + + if (is_numeric($time)) { + + $this->time = Carbon::createFromTimestamp($time); + + return $this; + } + + throw CouldNotSendNotification::withErrorMessage( + sprintf('Time must be a timestamp or an object implementing DateTimeInterface. %s is given', gettype($time)) + ); + } } diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 7ba8ff6..a547ea5 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -2,6 +2,8 @@ namespace NotificationChannels\MobilyWs\Test; +use Carbon\Carbon; +use NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification; use NotificationChannels\MobilyWs\MobilyWsMessage; class MessageTest extends \PHPUnit_Framework_TestCase @@ -42,4 +44,60 @@ public function it_can_create_new_message() $this->assertEquals('Message content is here', $message->text); } + /** @test */ + public function it_can_set_delay_from_Carbon_instance() + { + $carbon = Carbon::parse('+ 1 week'); + $message = MobilyWsMessage::create('Message content is here'); + $message->time($carbon); + + $this->assertEquals($carbon, $message->time); + $this->assertInstanceOf(Carbon::class, $message->time); + } + + /** @test */ + public function it_can_set_delay_from_DateTime_instance() + { + $carbon = Carbon::parse('+ 1 week'); + $dt = new \DateTime($carbon->toDateTimeString()); + $this->assertEquals($dt, $carbon); + + $message = MobilyWsMessage::create('Message content is here'); + $message->time($dt); + + $this->assertEquals($carbon, $message->time); + $this->assertInstanceOf(Carbon::class, $message->time); + } + + /** @test */ + public function it_can_set_delay_from_timestamp() + { + $carbon = Carbon::parse('+ 1 week'); + + $message = MobilyWsMessage::create('Message content is here'); + $message->time($carbon->timestamp); + + $this->assertEquals($carbon, $message->time); + $this->assertInstanceOf(Carbon::class, $message->time); + } + + /** @test */ + public function it_throw_an_exception_when_given_invalid_time() + { + $message = MobilyWsMessage::create('Message content is here'); + + try { + $message->time(['invalid time format']); + } catch (CouldNotSendNotification $exception) { + $this->assertContains( + "Time must be a timestamp or an object implementing DateTimeInterface. array is given", + $exception->getMessage() + ); + + return; + } + + $this->fail('An exception should be thrown when invalid time format is given'); + } + } From 97ca87802092f9a9824c34684db65c93fb21bace Mon Sep 17 00:00:00 2001 From: Hamoud Alhoqbani Date: Tue, 15 Aug 2017 05:44:04 -0700 Subject: [PATCH 12/16] Add timeSend and dateSend getters --- src/MobilyWsMessage.php | 20 ++++++++++++++++++++ tests/MessageTest.php | 14 ++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/MobilyWsMessage.php b/src/MobilyWsMessage.php index 0af2eb4..bd48283 100644 --- a/src/MobilyWsMessage.php +++ b/src/MobilyWsMessage.php @@ -52,6 +52,7 @@ public function text($text) } /** + * Set the message scheduled date and time * @param DateTime|Carbon|int $time * * @return $this @@ -75,4 +76,23 @@ public function time($time) sprintf('Time must be a timestamp or an object implementing DateTimeInterface. %s is given', gettype($time)) ); } + + /** + * Get the message schedule date + * + * @return string + */ + public function dateSend() + { + return $this->time->format("m/d/Y"); + } + + /** + * Get the message schedule time + * @return string + */ + public function timeSend() + { + return $this->time->format("H:i:s"); + } } diff --git a/tests/MessageTest.php b/tests/MessageTest.php index a547ea5..6fd44bf 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -100,4 +100,18 @@ public function it_throw_an_exception_when_given_invalid_time() $this->fail('An exception should be thrown when invalid time format is given'); } + /** @test */ + public function it_return_date_and_time_in_a_proper_mobily_ws_required_format() + { + $expectedDate = "08/15/2017"; + $expectedTime = "23:15:30"; + $carbon = Carbon::parse('08/15/2017 23:15:30'); + + $message = new MobilyWsMessage(); + $message->time($carbon); + + $this->assertEquals($expectedDate, $message->dateSend()); + $this->assertEquals($expectedTime, $message->timeSend()); + } + } From ad12f3711b057a7dcfd287f01304dde35794261c Mon Sep 17 00:00:00 2001 From: Hamoud Alhoqbani Date: Tue, 15 Aug 2017 05:50:10 -0700 Subject: [PATCH 13/16] Fix date and time getters when there time is not set --- src/MobilyWsMessage.php | 21 +++++++++++++-------- tests/MessageTest.php | 9 +++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/MobilyWsMessage.php b/src/MobilyWsMessage.php index bd48283..79520d5 100644 --- a/src/MobilyWsMessage.php +++ b/src/MobilyWsMessage.php @@ -52,7 +52,8 @@ public function text($text) } /** - * Set the message scheduled date and time + * Set the message scheduled date and time. + * * @param DateTime|Carbon|int $time * * @return $this @@ -66,7 +67,6 @@ public function time($time) } if (is_numeric($time)) { - $this->time = Carbon::createFromTimestamp($time); return $this; @@ -76,23 +76,28 @@ public function time($time) sprintf('Time must be a timestamp or an object implementing DateTimeInterface. %s is given', gettype($time)) ); } - + /** - * Get the message schedule date + * Get the message schedule date. * * @return string */ public function dateSend() { - return $this->time->format("m/d/Y"); + if ($this->time) { + return $this->time->format('m/d/Y'); + } } - + /** - * Get the message schedule time + * Get the message schedule time. + * * @return string */ public function timeSend() { - return $this->time->format("H:i:s"); + if ($this->time) { + return $this->time->format('H:i:s'); + } } } diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 6fd44bf..b16993a 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -114,4 +114,13 @@ public function it_return_date_and_time_in_a_proper_mobily_ws_required_format() $this->assertEquals($expectedTime, $message->timeSend()); } + /** @test */ + public function it_return_null_when_there_time_is_not_set() + { + $message = new MobilyWsMessage(); + + $this->assertNull($message->dateSend()); + $this->assertNull($message->timeSend()); + } + } From b907f14c749240ad9d8264facf8fe41416b5eff0 Mon Sep 17 00:00:00 2001 From: Hamoud Alhoqbani Date: Tue, 15 Aug 2017 05:59:35 -0700 Subject: [PATCH 14/16] Add dateSend and timeSend params to the request --- src/MobilyWsApi.php | 10 ++++------ tests/ApiTest.php | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/MobilyWsApi.php b/src/MobilyWsApi.php index ad001fc..fa0ade1 100644 --- a/src/MobilyWsApi.php +++ b/src/MobilyWsApi.php @@ -54,6 +54,8 @@ public function sendMessage(MobilyWsMessage $message, $number) $params = [ 'msg' => $message->text, 'numbers' => $number, + 'dateSend' => $message->dateSend(), + 'timeSend' => $message->timeSend(), ]; $payload = $this->preparePayload($params); @@ -97,17 +99,13 @@ public function send(array $payload) */ protected function preparePayload($params) { - $form = [ + $form = array_merge([ 'mobile' => $this->config->mobile, 'password' => $this->config->password, 'applicationType' => $this->config->applicationType, 'lang' => $this->config->lang, 'sender' => $this->config->sender, - 'msg' => $params['msg'], - 'numbers' => $params['numbers'], - // For development to avoid charges - 'dateSend' => \Carbon\Carbon::parse('+1 month')->format('m/d/Y'), - ]; + ], $params); return array_merge( ['form_params' => $form], diff --git a/tests/ApiTest.php b/tests/ApiTest.php index 0987ff0..b9220e8 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -2,6 +2,7 @@ namespace NotificationChannels\MobilyWs\Test; +use Carbon\Carbon; use GuzzleHttp\Client; use GuzzleHttp\Middleware; use GuzzleHttp\HandlerStack; @@ -120,6 +121,42 @@ public function it_send_request_with_correct_params_when_given_MobilyWsMessage_i $this->assertSame('/api/msgSend.php', $request->getRequestTarget()); $this->assertArraySubset($params, parse_query($request->getBody()->getContents())); } + + + /** @test */ + public function it_send_date_and_time_with_request_when_message_time_is_set() + { + $container = []; + $history = Middleware::history($container); + $mock = new MockHandler([ + new Response(200, [], 1), + ]); + $stack = HandlerStack::create($mock); + $stack->push($history); + $options = [ + 'base_uri' => 'http://mobily.ws/api/', + 'handler' => $stack, + ]; + $client = new Client($options); + $mobileWsConfig = new MobilyWsConfig($this->getConfigs()); + $api = new MobilyWsApi($mobileWsConfig, $client); + + $message = new MobilyWsMessage('SMS Text Message'); + $message->time(Carbon::parse("+1 week")); + $params = [ + 'msg' => $message->text, + 'numbers' => $number = '966550000000', + 'timeSend' => $message->timeSend(), + 'dateSend' => $message->dateSend(), + ]; + + $api->sendMessage($message, $number); + + /** @var Request $request */ + $request = $container[0]['request']; + + $this->assertArraySubset($params, parse_query($request->getBody()->getContents())); + } /** @test */ public function it_throw_an_exception_when_response_is_not_ok() From c75c0497254ab161e7e7cb9a555faf752f119939 Mon Sep 17 00:00:00 2001 From: Hamoud Alhoqbani Date: Tue, 15 Aug 2017 06:36:42 -0700 Subject: [PATCH 15/16] update readme --- README.md | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5d3315b..0083ba2 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ This package makes it easy to send notifications using [MobilyWs](https://www.mo - [Create Notification](#create-notification) - [Routing SMS Notifications](#routing-sms-notifications) - [Sending SMS](#sending-sms) + - [Scheduled SMS](#scheduled-sms) - [Available Message methods](#available-message-methods) - [TODO](#todo) - [Changelog](#changelog) @@ -149,6 +150,24 @@ use App\Notifications\SmsNewUser; $user->notify(new SmsNewUser()); ``` + +### Scheduled SMS +[MobilyWs](https://www.mobily.ws) api allows for sending scheduled message which will be sent on the defined date/time. + +> Please note that if you define time in the past, the message will be sent immediately by mobily.ws. +This library will not check if the defined time is in the future. + +You can define the time on which the message should be sent by mobily.ws by calling `time` method on the MobilyWsMessage instance. +```php + public function toMobilyWs($notifiable) + { + return (new MobilyWsMessage) + ->text("Message text") + ->time(Carbon::parse("+1 week); + } +``` +The time method accepts either a DateTime object or a timestamp. + ### Available Message methods In your notification, you must define a method `toMobilyWs` which will receive the notifiable entity (e.g User model) and an instance of `MobilyWsMessage`. @@ -179,16 +198,19 @@ or set the text message using the `msg()` method: ``` Method `toMobilyWs` will receive an instance of `MobilyWsMessage` as the 2nd argument. #### list of available methods : -> Coming soon. +`text()` To add the content of the text message +'time()' To set time of the scheduled sms. ## TODO - [ ] Validate mobile numbers - [ ] Validate text messages type and length -- [ ] Verify method `toMobilyWs` existence and config file. -- [ ] Add the option to send Scheduled SMS -- [ ] Add the the reset of params (MsgID, msgKey, deleteKey, timeSend, dateSend) +- [ ] Validate given time is in the future. +- [x] Verify method `toMobilyWs` existence and config file. +- [x] Add the option to send Scheduled SMS +- [ ] Add the the rest of params (MsgID, msgKey, deleteKey, ~~timeSend~~, ~~dateSend~~) - [ ] Translate mobily.ws error messages - [ ] Create artisan command to made mobily.ws notifications +- [ ] Add list of fired event to the documentation. ## Changelog From f0d4ecb880cdc9e1e6a2fbc8fabafc791f2de855 Mon Sep 17 00:00:00 2001 From: Hamoud Alhoqbani Date: Tue, 15 Aug 2017 09:20:31 -0700 Subject: [PATCH 16/16] Add artisan command to make new MobilyWs notifications --- README.md | 39 ++++++--- .../MobilyWsNotificationMakeCommand.php | 86 +++++++++++++++++++ src/Console/stubs/notification.stub | 49 +++++++++++ src/MobilyWsServiceProvider.php | 18 ++++ 4 files changed, 179 insertions(+), 13 deletions(-) create mode 100644 src/Console/MobilyWsNotificationMakeCommand.php create mode 100644 src/Console/stubs/notification.stub diff --git a/README.md b/README.md index 0083ba2..62da351 100644 --- a/README.md +++ b/README.md @@ -68,9 +68,14 @@ MOBILY_WS_SENDER= ### Create new notification: Make a new notification class using laravel artisan ```bash -php artisan make:notification SmsNewUser +php artisan make:notification UserRegistered ``` -Configure the notification class to use MobilyWsChannel: +and configure the notification class to use MobilyWsChannel. + +Or you could use our custom artisan command: +```bash +php artisan mobilyws:notification UserRegistered +``` The `toMobilyWs` method should return a string of the text message to be sent or an instance of `MobilyWsMessage`. @@ -84,7 +89,7 @@ use Illuminate\Notifications\Notification; use NotificationChannels\MobilyWs\MobilyWsChannel; use NotificationChannels\MobilyWs\MobilyWsMessage; -class SmsNewUser extends Notification +class UserRegistered extends Notification { /** * Get the notification's delivery channels. @@ -98,14 +103,16 @@ class SmsNewUser extends Notification } /** - * Get the text message of the SMS. + * Get the text message representation of the notification * - * @param mixed $notifiable - * @return string|MobilyWsMessage + * @param mixed $notifiable + * @param \NotificationChannels\MobilyWs\MobilyWsMessage $msg + * + * @return \NotificationChannels\MobilyWs\MobilyWsMessage|string */ - public function toMobilyWs($notifiable) + public function toMobilyWs($notifiable, MobilyWsMessage $msg) { - return "Dear $notifiable->name , Thank for your business with us"; + return "Dear $notifiable->name, welcome to our website"; } } ``` @@ -146,13 +153,15 @@ For example, `9665xxxxxxxx` ### Sending SMS: ```php -use App\Notifications\SmsNewUser; +use App\Notifications\UserRegistered; + +$user = App\User::first(); -$user->notify(new SmsNewUser()); +$user->notify(new UserRegistered()); ``` ### Scheduled SMS -[MobilyWs](https://www.mobily.ws) api allows for sending scheduled message which will be sent on the defined date/time. +[MobilyWs](https://www.mobily.ws) Api allows for sending scheduled message which will be sent on the defined date/time. > Please note that if you define time in the past, the message will be sent immediately by mobily.ws. This library will not check if the defined time is in the future. @@ -166,7 +175,7 @@ You can define the time on which the message should be sent by mobily.ws by call ->time(Carbon::parse("+1 week); } ``` -The time method accepts either a DateTime object or a timestamp. +The `time` method accepts either a DateTime object or a timestamp. ### Available Message methods In your notification, you must define a method `toMobilyWs` which will receive the notifiable entity (e.g User model) and an instance of `MobilyWsMessage`. @@ -174,11 +183,15 @@ In your notification, you must define a method `toMobilyWs` which will receive t This method should return the text of the message to be sent as an SMS to mobily.ws or an instance of `MobilyWsMessage`. ```php +option('force')) { + return; + } + } + + /** + * Build the class with the given name. + * + * @param string $name + * @return string + */ + protected function buildClass($name) + { + return parent::buildClass($name); + } + + /** + * Get the stub file for the generator. + * + * @return string + */ + protected function getStub() + { + return __DIR__.'/stubs/notification.stub'; + } + + /** + * Get the default namespace for the class. + * + * @param string $rootNamespace + * @return string + */ + protected function getDefaultNamespace($rootNamespace) + { + return $rootNamespace.'\Notifications'; + } + + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return [ + ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the notification already exists.'], + ]; + } +} diff --git a/src/Console/stubs/notification.stub b/src/Console/stubs/notification.stub new file mode 100644 index 0000000..db2d16d --- /dev/null +++ b/src/Console/stubs/notification.stub @@ -0,0 +1,49 @@ +text('some text') + ->time(Carbon::parse("+1 day")); + } +} diff --git a/src/MobilyWsServiceProvider.php b/src/MobilyWsServiceProvider.php index 81518f8..4097954 100644 --- a/src/MobilyWsServiceProvider.php +++ b/src/MobilyWsServiceProvider.php @@ -4,6 +4,7 @@ use GuzzleHttp\Client; use Illuminate\Support\ServiceProvider; +use NotificationChannels\MobilyWs\Console\MobilyWsNotificationMakeCommand; use NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification; class MobilyWsServiceProvider extends ServiceProvider @@ -33,4 +34,21 @@ public function boot() __DIR__.'/../config/mobilyws.php' => config_path('mobilyws.php'), ]); } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->app->singleton( + 'command.mobilyws.notification', + function ($app) { + return new MobilyWsNotificationMakeCommand($app['files']); + } + ); + $this->commands('command.mobilyws.notification'); + } + }