10000 [Notifier] Add SMS options to OrangeSms notifier · symfony/symfony@69b0cd4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 69b0cd4

Browse files
committed
[Notifier] Add SMS options to OrangeSms notifier
1 parent dc803d7 commit 69b0cd4

File tree

5 files changed

+102
-3
lines changed

5 files changed

+102
-3
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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\OrangeSms;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author gnito-org <https://github.com/gnito-org>
18+
*/
19+
final class OrangeSmsOptions implements MessageOptionsInterface
20+
{
21+
private array $options;
22+
23+
public function __construct(array $options = [])
24+
{
25+
$this->options = $options;
26+
}
27+
28+
public function getFrom(): ?string
29+
{
30+
return $this->options['from'] ?? null;
31+
}
32+
33+
public function getRecipientId(): ?string
34+
{
35+
return $this->options['recipient_id'] ?? null;
36+
}
37+
38+
public function setFrom(string $from): self
39+
{
40+
$this->options['from'] = $from;
41+
42+
return $this;
43+
}
44+
45+
public function setRecipientId(string $id): self
46+
{
47+
$this->options['recipient_id'] = $id;
48+
49+
return $this;
50+
}
51+
52+
public function toArray(): array
53+
{
54+
$options = $this->options;
55+
if (isset($options['recipient_id'])) {
56+
unset($options['recipient_id']);
57+
}
58+
59+
return $options;
60+
}
61+
}

src/Symfony/Component/Notifier/Bridge/OrangeSms/OrangeSmsTransport.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __toString(): string
5050

5151
public function supports(MessageInterface $message): bool
5252
{
53-
return $message instanceof SmsMessage;
53+
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof OrangeSmsOptions);
5454
}
5555

5656
public function doSend(MessageInterface $message): SentMessage
@@ -61,7 +61,11 @@ public function doSend(MessageInterface $message): SentMessage
6161

6262
$from = $message->getFrom() ?: $this->from;
6363

64-
$url = 'https://'.$this->getEndpoint().'/smsmessaging/v1/outbound/'.urlencode('tel:'.$from).'/requests';
64+
$opts = $message->getOptions();
65+
$options = $opts ? $opts->toArray() : [];
66+
$options['from'] = $options['from'] ?? $from;
67+
68+
$url = 'https://'.$this->getEndpoint().'/smsmessaging/v1/outbound/'.urlencode('tel:'.$options['from']).'/requests';
6569
$headers = [
6670
'Authorization' => 'Bearer '.$this->getAccessToken(),
6771
'Content-Type' => 'application/json',
@@ -70,7 +74,7 @@ public function doSend(MessageInterface $message): SentMessage
7074
$payload = [
7175
'outboundSMSMessageRequest' => [
7276
'address' => 'tel:'.$message->getPhone(),
73-
'senderAddress' => 'tel:'.$from,
77+
'senderAddress' => 'tel:'.$options['from'],
7478
'outboundSMSTextMessage' => [
7579
'message' => $message->getSubject(),
7680
],
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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\OrangeSms\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\OrangeSms\OrangeSmsOptions;
16+
17+
class OrangeSmsOptionsTest extends TestCase
18+
{
19+
public function testOrangeSmsOptions()
20+
{
21+
$orangeSmsOptions = (new OrangeSmsOptions())->setFrom('test_from')->setRecipientId('test_recipient');
22+
23+
self::assertSame([
24+
'from' => 'test_from',
25+
], $orangeSmsOptions->toArray());
26+
}
27+
}

src/Symfony/Component/Notifier/Bridge/OrangeSms/Tests/OrangeSmsTransportTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

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

14+
use Symfony\Component\Notifier\Bridge\OrangeSms\OrangeSmsOptions;
1415
use Symfony\Component\Notifier\Bridge\OrangeSms\OrangeSmsTransport;
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('+243899999999', 'Hello World!')];
37+
yield [new SmsMessage('+243899999999', 'Hello World!'), 'from', new OrangeSmsOptions(['from' => 'foo'])];
3638
}
3739

3840
public function unsupportedMessagesProvider(): iterable

0 commit comments

Comments
 (0)
0