Skip to content

Commit 632508b

Browse files
committed
Two events for success and failure
`SMSSentSuccessfullyEvent` and `SMSSendingFailedEvent`
1 parent 550c1c2 commit 632508b

File tree

6 files changed

+87
-1
lines changed

6 files changed

+87
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes to `CMSMS` will be documented in this file
88
- Changed CM endpoint
99
#### Added
1010
- Added config value for encoding detection type
11+
- Two events for success and failure: `SMSSentSuccessfullyEvent` and `SMSSendingFailedEvent`
1112

1213
## [3.3.0] - 2024-03-22
1314
#### Added

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ public function routeNotificationForCmsms()
108108
- `tariff()`: Accepts a integer value for your message tariff. The unit is eurocent. Requires the `originator` to be set to a specific value. Contact CM for this tariff value. CM also must enable this feature for your contract manually.
109109
- `multipart($minimum, $maximum)`: Accepts a 0 to 8 integer range which allows multipart messages. See the [documentation from CM](https://dashboard.onlinesmsgateway.com/docs#send-a-message-multipart) for more information.
110110

111+
### Available events
112+
- `SMSSentSuccessfullyEvent`: This event will be fired after the message was sent. The event will contain the payload we have sent to CM.
113+
- `SMSSendingFailedEvent`: This event will be fired if the message was not sent. The event will contain the response body we received from CM.
114+
115+
111116
## Changelog
112117

113118
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

src/CmsmsClient.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
namespace NotificationChannels\Cmsms;
66

77
use GuzzleHttp\Client as GuzzleClient;
8+
use GuzzleHttp\Exception\GuzzleException;
89
use Illuminate\Support\Arr;
10+
use NotificationChannels\Cmsms\Events\SMSSendingFailedEvent;
11+
use NotificationChannels\Cmsms\Events\SMSSentSuccessfullyEvent;
912
use NotificationChannels\Cmsms\Exceptions\CouldNotSendNotification;
13+
use NotificationChannels\Cmsms\Exceptions\InvalidMessage;
1014

1115
class CmsmsClient
1216
{
@@ -18,14 +22,21 @@ public function __construct(
1822
) {
1923
}
2024

25+
/**
26+
* @throws InvalidMessage
27+
* @throws GuzzleException
28+
* @throws CouldNotSendNotification
29+
*/
2130
public function send(CmsmsMessage $message, string $recipient): void
2231
{
2332
if (empty($message->getOriginator())) {
2433
$message->originator(config('services.cmsms.originator'));
2534
}
2635

36+
$payload = $this->buildMessageJson($message, $recipient);
37+
2738
$response = $this->client->request('POST', static::GATEWAY_URL, [
28-
'body' => $this->buildMessageJson($message, $recipient),
39+
'body' => $payload,
2940
'headers' => [
3041
'accept' => 'application/json',
3142
'content-type' => 'application/json',
@@ -38,8 +49,11 @@ public function send(CmsmsMessage $message, string $recipient): void
3849
$body = $response->getBody()->getContents();
3950
$errorCode = Arr::get(json_decode($body, true), 'errorCode');
4051
if ($errorCode !== 0) {
52+
SMSSendingFailedEvent::dispatch($body);
4153
throw CouldNotSendNotification::serviceRespondedWithAnError($body);
4254
}
55+
56+
SMSSentSuccessfullyEvent::dispatch($payload);
4357
}
4458

4559
/**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace NotificationChannels\Cmsms\Events;
4+
5+
use Illuminate\Foundation\Events\Dispatchable;
6+
7+
class SMSSendingFailedEvent
8+
{
9+
use Dispatchable;
10+
11+
public function __construct(public string $response)
12+
{
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace NotificationChannels\Cmsms\Events;
4+
5+
use Illuminate\Foundation\Events\Dispatchable;
6+
7+
class SMSSentSuccessfullyEvent
8+
{
9+
use Dispatchable;
10+
11+
public function __construct(public string $payload)
12+
{
13+
}
14+
}

tests/CmsmsClientTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
use GuzzleHttp\Client;
66
use GuzzleHttp\Psr7\Response;
7+
use Illuminate\Support\Facades\Bus;
8+
use Illuminate\Support\Facades\Event;
79
use Mockery;
810
use NotificationChannels\Cmsms\CmsmsClient;
911
use NotificationChannels\Cmsms\CmsmsMessage;
12+
use NotificationChannels\Cmsms\Events\SMSSendingFailedEvent;
13+
use NotificationChannels\Cmsms\Events\SMSSentSuccessfullyEvent;
1014
use NotificationChannels\Cmsms\Exceptions\CouldNotSendNotification;
1115
use Orchestra\Testbench\TestCase;
1216

@@ -123,4 +127,38 @@ public function it_includes_reference_data()
123127
$this->assertTrue(isset($messageJsonObject->messages->msg[0]->reference));
124128
$this->assertEquals('ABC', $messageJsonObject->messages->msg[0]->reference);
125129
}
130+
131+
/** @test */
132+
public function it_dispatches_a_success_event()
133+
{
134+
Event::fake();
135+
136+
$this->guzzle
137+
->shouldReceive('request')
138+
->once()
139+
->andReturn(new Response(200, [], '{"details": "Created 1 message(s)", "errorCode": 0}'));
140+
141+
$this->client->send($this->message, '00301234');
142+
143+
Event::assertDispatched(SMSSentSuccessfullyEvent::class);
144+
}
145+
146+
/** @test */
147+
public function it_dispatches_a_failure_event()
148+
{
149+
Event::fake();
150+
151+
$this->guzzle
152+
->shouldReceive('request')
153+
->once()
154+
->andReturn(new Response(200, [], '{"details": "Some error message", "errorCode": 1}'));
155+
156+
try {
157+
$this->client->send($this->message, '00301234');
158+
} catch (CouldNotSendNotification $e) {
159+
// Do nothing, we know about the exception
160+
}
161+
162+
Event::assertDispatched(SMSSendingFailedEvent::class);
163+
}
126164
}

0 commit comments

Comments
 (0)