Skip to content

Commit 4ca176a

Browse files
committed
[Notifier] Add SMS options to MessageBird notifier
1 parent 45113ba commit 4ca176a

File tree

5 files changed

+299
-6
lines changed

5 files changed

+299
-6
lines changed

src/Symfony/Component/Notifier/Bridge/MessageBird/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Add `MessageBirdOptions` class
8+
49
6.2
510
---
611

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\MessageBird;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author gnito-org <https://github.com/gnito-org>
18+
*/
19+
final class MessageBirdOptions implements MessageOptionsInterface
20+
{
21+
private array $options;
22+
23+
public function __construct(array $options = [])
24+
{
25+
$this->options = $options;
26+
}
27+
28+
public function getCreatedDatetime(): ?string
29+
{
30+
return $this->options['created_datetime'] ?? null;
31+
}
32+
33+
public function getDataCoding(): ?string
34+
{
35+
return $this->options['data_coding'] ?? null;
36+
}
37+
38+
public function getFrom(): ?string
39+
{
40+
return $this->options['from'] ?? null;
41+
}
42+
43+
public function getGateway(): ?int
44+
{
45+
return $this->options['gateway'] ?? null;
46+
}
47+
48+
public function getGroupIds(): ?array
49+
{
50+
return $this->options['group_ids'] ?? null;
51+
}
52+
53+
public function getMClass(): ?int
54+
{
55+
return $this->options['m_class'] ?? null;
56+
}
57+
58+
public function getRecipientId(): ?string
59+
{
60+
return $this->options['recipient_id'] ?? null;
61+
}
62+
63+
public function getReference(): ?string
64+
{
65+
return $this->options['reference'] ?? null;
66+
}
67+
68+
public function getReportUrl(): ?string
69+
{
70+
return $this->options['report_url'] ?? null;
71+
}
72+
73+
public function getScheduledDatetime(): ?string
74+
{
75+
return $this->options['scheduled_datetime'] ?? null;
76+
}
77+
78+
public function getShortenUrls(): ?bool
79+
{
80+
return $this->options['shorten_urls'] ?? null;
81+
}
82+
83+
public function getType(): ?string
84+
{
85+
return $this->options['type'] ?? null;
86+
}
87+
88+
public function getTypeDetails(): ?string
89+
{
90+
return $this->options['type_details'] ?? null;
91+
}
92+
93+
public function getValidity(): ?int
94+
{
95+
return $this->options['validity'] ?? null;
96+
}
97+
98+
public function setCreatedDatetime(string $createdDatetime): self
99+
{
100+
$this->options['created_datetime'] = $createdDatetime;
101+
102+
return $this;
103+
}
104+
105+
public function setDataCoding(string $dataCoding): self
106+
{
107+
$this->options['data_coding'] = $dataCoding;
108+
109+
return $this;
110+
}
111+
112+
public function setFrom(string $from): self
113+
{
114+
$this->options['from'] = $from;
115+
116+
return $this;
117+
}
118+
119+
public function setGateway(int $gateway): self
120+
{
121+
$this->options['gateway'] = $gateway;
122+
123+
return $this;
124+
}
125+
126+
public function setGroupIds(array $groupIds): self
127+
{
128+
$this->options['group_ids'] = $groupIds;
129+
130+
return $this;
131+
}
132+
133+
public function setMClass(int $mClass): self
134+
{
135+
$this->options['m_class'] = $mClass;
136+
137+
return $this;
138+
}
139+
140+
public function setRecipientId(string $id): self
141+
{
142+
$this->options['recipient_id'] = $id;
143+
144+
return $this;
145+
}
146+
147+
public function setReference(string $reference): self
148+
{
149+
$this->options['reference'] = $reference;
150+
151+
return $this;
152+
}
153+
154+
public function setReportUrl(string $reportUrl): self
155+
{
156+
$this->options['report_url'] = $reportUrl;
157+
158+
return $this;
159+
}
160+
161+
public function setScheduledDatetime(string $scheduledDatetime): self
162+
{
163+
$this->options['scheduled_datetime'] = $scheduledDatetime;
164+
165+
return $this;
166+
}
167+
168+
public function setShortenUrls(bool $shortenUrls): self
169+
{
170+
$this->options['shorten_urls'] = $shortenUrls;
171+
172+
return $this;
173+
}
174+
175+
public function setType(string $type): self
176+
{
177+
$this->options['type'] = $type;
178+
179+
return $this;
180+
}
181+
182+
public function setTypeDetails(string $typeDetails): self
183+
{
184+
$this->options['type_details'] = $typeDetails;
185+
186+
return $this;
187+
}
188+
189+
public function setValidity(int $validity): self
190+
{
191+
$this->options['validity'] = $validity;
192+
193+
return $this;
194+
}
195+
196+
public function toArray(): array
197+
{
198+
$options = $this->options;
199+
if (isset($options['recipient_id'])) {
200+
unset($options['recipient_id']);
201+
}
202+
203+
return $options;
204+
}
205+
}

src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdTransport.php

+48-6
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __toString(): string
4646

4747
public function supports(MessageInterface $message): bool
4848
{
49-
return $message instanceof SmsMessage;
49+
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof MessageBirdOptions);
5050
}
5151

5252
protected function doSend(MessageInterface $message): SentMessage
@@ -57,14 +57,56 @@ protected function doSend(MessageInterface $message): SentMessage
5757

5858
$from = $message->getFrom() ?: $this->from;
5959

60+
$opts = $message->getOptions();
61+
$options = $opts ? $opts->toArray() : [];
62+
$options['originator'] = $options['from'] ?? $from;
63+
$options['recipients'] = [$message->getPhone()];
64+
$options['body'] = $message->getSubject();
65+
66+
if (isset($options['group_ids'])) {
67+
$options['groupIds'] = $options['group_ids'];
68+
unset($options['group_ids']);
69+
}
70+
71+
if (isset($options['report_url'])) {
72+
$options['reportUrl'] = $options['report_url'];
73+
unset($options['report_url']);
74+
}
75+
76+
if (isset($options['type_details'])) {
77+
$options['typeDetails'] = $options['type_details'];
78+
unset($options['type_details']);
79+
}
80+
81+
if (isset($options['data_coding'])) {
82+
$options['datacoding'] = $options['data_coding'];
83+
unset($options['data_coding']);
84+
}
85+
86+
if (isset($options['m_class'])) {
87+
$options['mclass'] = $options['m_class'];
88+
unset($options['m_class']);
89+
}
90+
91+
if (isset($options['shorten_urls'])) {
92+
$options['shortenUrls'] = $options['shorten_urls'];
93+
unset($options['shorten_urls']);
94+
}
95+
96+
if (isset($options['scheduled_datetime'])) {
97+
$options['scheduledDatetime'] = $options['scheduled_datetime'];
98+
unset($options['scheduled_datetime']);
99+
}
100+
101+
if (isset($options['created_datetime'])) {
102+
$options['createdDatetime'] = $options['created_datetime'];
103+
unset($options['created_datetime']);
104+
}
105+
60106
$endpoint = sprintf('https://%s/messages', $this->getEndpoint());
61107
$response = $this->client->request('POST', $endpoint, [
62108
'auth_basic' => 'AccessKey:'.$this->token,
63-
'body' => [
64-
'originator' => $from,
65-
'recipients' => $message->getPhone(),
66-
'body' => $message->getSubject(),
67-
],
109+
'body' => array_filter($options),
68110
]);
69111

70112
try {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\MessageBird\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\MessageBird\MessageBirdOptions;
16+
17+
class MessageBirdOptionsTest extends TestCase
18+
{
19+
public function testMessageBirdOptions()
20+
{
21+
$messageBirdOptions = (new MessageBirdOptions())->setFrom('test_from')->setType('test_type')->setScheduledDatetime('test_scheduled_datetime')->setCreatedDatetime('test_created_datetime')->setRecipientId('test_recipient')->setDataCoding('test_data_coding')->setGateway(999)->setGroupIds(['test_group_ids'])->setMClass(888)->setReference('test_reference')->setReportUrl('test_report_url')->setShortenUrls(true)->setTypeDetails('test_type_details')->setValidity(777);
22+
23+
self::assertSame([
24+
'from' => 'test_from',
25+
'type' => 'test_type',
26+
'scheduled_datetime' => 'test_scheduled_datetime',
27+
'created_datetime' => 'test_created_datetime',
28+
'data_coding' => 'test_data_coding',
29+
'gateway' => 999,
30+
'group_ids' => ['test_group_ids'],
31+
'm_class' => 888,
32+
'reference' => 'test_reference',
33+
'report_url' => 'test_report_url',
34+
'shorten_urls' => true,
35+
'type_details' => 'test_type_details',
36+
'validity' => 777,
37+
], $messageBirdOptions->toArray());
38+
}
39+
}

src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdTransportTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\MessageBird\Tests;
1313

14+
use Symfony\Component\Notifier\Bridge\MessageBird\MessageBirdOptions;
1415
use Symfony\Component\Notifier\Bridge\MessageBird\MessageBirdTransport;
1516
use Symfony\Component\Notifier\Message\ChatMessage;
1617
use Symfony\Component\Notifier\Message\MessageInterface;
@@ -33,6 +34,7 @@ public function toStringProvider(): iterable
3334
public function supportedMessagesProvider(): iterable
3435
{
3536
yield [new SmsMessage('0611223344', 'Hello!')];
37+
yield [new SmsMessage('0611223344', 'Hello!', 'from', new MessageBirdOptions(['from' => 'foo']))];
3638
}
3739

3840
public function unsupportedMessagesProvider(): iterable

0 commit comments

Comments
 (0)