Skip to content

Commit 07bd450

Browse files
committed
Merge branch 'danielebuso-custom-sender'
2 parents 1311bf0 + d3c9c98 commit 07bd450

File tree

5 files changed

+112
-22
lines changed

5 files changed

+112
-22
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,22 @@ class AccountApproved extends Notification
8080

8181
return SnsMessage::create()
8282
->body("Your {$notifiable->service} account was approved!")
83-
->transactional();
83+
->transactional()
84+
->sender("MyStore");
8485

8586
// or
8687

8788
return SnsMessage::create([
8889
'body' => "Your {$notifiable->service} account was approved!",
89-
'transactional' => true
90+
'transactional' => true,
91+
'sender' => "MyStore"
9092
]);
9193

9294
// or
9395

9496
return SnsMessage::create([
9597
'body' => "Your {$notifiable->service} account was approved!"
96-
])->promotional();
98+
])->promotional()->sender("MyStore");
9799
}
98100
}
99101
```
@@ -116,6 +118,7 @@ public function routeNotificationForSns()
116118
- `body('')`: Accepts a string value for the notification body. Messages with more than 140 characters will be split into multiple messages by SNS without breaking any words.
117119
- `promotional(bool)`: Sets the SMS attribute as the promotional delivery type (default). Optimizes the delivery for lower costs.
118120
- `transactional(bool)`: Sets the SMS attribute as the transactional delivery type. Optimizes the delivery to achieve the highest reliability (it also costs more).
121+
- `sender(string)`: Sets the SMS sender id. It can be up to 11 characters with no spaces.
119122

120123
More information about the SMS Attributes can be found on the [AWS SNS Docs](https://docs.aws.amazon.com/pt_br/sdk-for-php/v3/developer-guide/sns-examples-sending-sms.html#get-sms-attributes).
121124
It's important to know that the attributes set on the message will override the

src/Sns.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,26 @@ public function __construct(SnsService $snsService)
2525
*/
2626
public function send(SnsMessage $message, $destination)
2727
{
28+
$attributes = [
29+
'AWS.SNS.SMS.SMSType' => [
30+
'DataType' => 'String',
31+
'StringValue' => $message->getDeliveryType(),
32+
],
33+
];
34+
35+
if (! empty($message->getSender())) {
36+
$attributes += [
37+
'AWS.SNS.SMS.SenderID' => [
38+
'DataType' => 'String',
39+
'StringValue' => $message->getSender(),
40+
],
41+
];
42+
}
43+
2844
$parameters = [
2945
'Message' => $message->getBody(),
3046
'PhoneNumber' => $destination,
31-
'MessageAttributes' => [
32-
'AWS.SNS.SMS.SMSType' => [
33-
'DataType' => 'String',
34-
'StringValue' => $message->getDeliveryType(),
35-
],
36-
],
47+
'MessageAttributes' => $attributes,
3748
];
3849

3950
return $this->snsService->publish($parameters);

src/SnsMessage.php

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,25 @@ class SnsMessage
99
const TRANSACTIONAL_SMS_TYPE = 'Transactional';
1010

1111
/**
12-
* The default delivery type for the SMS message.
12+
* The body of the message.
13+
*
14+
* @var string
15+
*/
16+
protected $body = '';
17+
18+
/**
19+
* The delivery type of the message.
1320
*
1421
* @var bool
1522
*/
1623
protected $promotional = true;
1724

1825
/**
19-
* The body of the message.
26+
* The sender identification of the message.
2027
*
2128
* @var string
2229
*/
23-
protected $body = '';
30+
protected $sender = '';
2431

2532
public function __construct($content)
2633
{
@@ -40,6 +47,7 @@ public function __construct($content)
4047
/**
4148
* Creates a new instance of the message.
4249
*
50+
* @param array $data
4351
* @return SnsMessage
4452
*/
4553
public static function create(array $data = [])
@@ -50,6 +58,7 @@ public static function create(array $data = [])
5058
/**
5159
* Sets the message body.
5260
*
61+
* @param string $content
5362
* @return $this
5463
*/
5564
public function body(string $content)
@@ -70,36 +79,61 @@ public function getBody()
7079
}
7180

7281
/**
73-
* Get the SMS delivery type.
82+
* Sets the message delivery type as promotional.
7483
*
75-
* @return string
84+
* @param bool $active
85+
* @return $this
7686
*/
77-
public function getDeliveryType()
87+
public function promotional(bool $active = true)
7888
{
79-
return $this->promotional ? self::PROMOTIONAL_SMS_TYPE : self::TRANSACTIONAL_SMS_TYPE;
89+
$this->promotional = $active;
90+
91+
return $this;
8092
}
8193

8294
/**
83-
* Sets the SMS delivery type as promotional.
95+
* Sets the message delivery type as transactional.
8496
*
97+
* @param bool $active
8598
* @return $this
8699
*/
87-
public function promotional(bool $active = true)
100+
public function transactional(bool $active = true)
88101
{
89-
$this->promotional = $active;
102+
$this->promotional = ! $active;
90103

91104
return $this;
92105
}
93106

94107
/**
95-
* Sets the SMS delivery type as transactional.
108+
* Get the message delivery type.
109+
*
110+
* @return string
111+
*/
112+
public function getDeliveryType()
113+
{
114+
return $this->promotional ? self::PROMOTIONAL_SMS_TYPE : self::TRANSACTIONAL_SMS_TYPE;
115+
}
116+
117+
/**
118+
* Sets the message sender identification.
96119
*
120+
* @param string $sender
97121
* @return $this
98122
*/
99-
public function transactional(bool $active = true)
123+
public function sender(string $sender)
100124
{
101-
$this->promotional = ! $active;
125+
$this->sender = $sender;
102126

103127
return $this;
104128
}
129+
130+
/**
131+
* Get the message sender identification.
132+
*
133+
* @return string
134+
*/
135+
public function getSender()
136+
{
137+
return $this->sender;
138+
}
105139
}

tests/SnsMessageTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,30 @@ public function the_sms_delivery_type_can_be_explicitly_as_promotional()
6161
$this->assertEquals('Promotional', $message->getDeliveryType());
6262
}
6363

64+
/** @test */
65+
public function the_default_sms_sender_id_is_empty()
66+
{
67+
$message = SnsMessage::create();
68+
$this->assertEmpty($message->getSender());
69+
}
70+
71+
/** @test */
72+
public function the_sms_sender_id_can_be_changed_using_a_proper_method()
73+
{
74+
$message = SnsMessage::create()->sender('Test');
75+
$this->assertEquals('Test', $message->getSender());
76+
}
77+
6478
/** @test */
6579
public function it_can_accept_all_the_contents_when_constructing_a_message()
6680
{
6781
$message = SnsMessage::create([
6882
'body' => 'My mass body',
6983
'transactional' => true,
84+
'sender' => 'Test',
7085
]);
7186
$this->assertEquals('My mass body', $message->getBody());
7287
$this->assertEquals('Transactional', $message->getDeliveryType());
88+
$this->assertEquals('Test', $message->getSender());
7389
}
7490
}

tests/SnsTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,30 @@ public function it_can_send_a_transactional_sms_message_to_sns()
8181

8282
$this->sns->send($message, '+22222222222');
8383
}
84+
85+
/** @test */
86+
public function it_can_send_a_sms_message_with_sender_id()
87+
{
88+
$message = new SnsMessage(['body' => 'Message text', 'sender' => 'CompanyInc']);
89+
90+
$this->snsService->shouldReceive('publish')
91+
->atLeast()->once()
92+
->with([
93+
'Message' => 'Message text',
94+
'PhoneNumber' => '+33333333333',
95+
'MessageAttributes' => [
96+
'AWS.SNS.SMS.SMSType' => [
97+
'DataType' => 'String',
98+
'StringValue' => 'Promotional',
99+
],
100+
'AWS.SNS.SMS.SenderID' => [
101+
'DataType' => 'String',
102+
'StringValue' => 'CompanyInc',
103+
],
104+
],
105+
])
106+
->andReturn(true);
107+
108+
$this->sns->send($message, '+33333333333');
109+
}
84110
}

0 commit comments

Comments
 (0)