8000 [Notifier] Add SMS options to FortySixElks notifier · symfony/symfony@aa5c6fb · GitHub
[go: up one dir, main page]

Skip to content

Commit aa5c6fb

Browse files
committed
[Notifier] Add SMS options to FortySixElks notifier
1 parent 45113ba commit aa5c6fb

File tree

5 files changed

+104
-7
lines changed

5 files changed

+104
-7
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
CHANGELOG
22
=========
33

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

7-
* Use `SmsMessage->from` when defined
12+
* Use `SmsMessage->from` when defined
813

914
6.1
1015
---
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\FortySixElks;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author gnito-org <https://github.com/gnito-org>
18+
*/
19+
final class FortySixElksOptions 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/FortySixElks/FortySixElksTransport.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __toString(): string
4848

4949
public function supports(MessageInterface $message): bool
5050
{
51-
return $message instanceof SmsMessage;
51+
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof FortySixElksOptions);
5252
}
5353

5454
protected function doSend(MessageInterface $message): SentMessage
@@ -59,14 +59,16 @@ protected function doSend(MessageInterface $message): SentMessage
5959

6060
$from = $message->getFrom() ?: $this->from;
6161

62+
$opts = $message->getOptions();
63+
$options = $opts ? $opts->toArray() : [];
64+
$options['from'] = $options['from'] ?? $from;
65+
$options['to'] = $message->getPhone();
66+
$options['message'] = $message->getSubject();
67+
6268
$endpoint = sprintf('https://%s/a1/sms', self::HOST);
6369
$response = $this->client->request('POST', $endpoint, [
64-
'body' => [
65-
'from' => $from,
66-
'to' => $message->getPhone(),
67-
'message' => $message->getSubject(),
68-
],
6970
'auth_basic' => [$this->apiUsername, $this->apiPassword],
71+
'body' => array_filter($options),
7072
]);
7173

7274
try {
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\FortySixElks\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\FortySixElks\FortySixElksOptions;
16+
17+
class FortySixElksOptionsTest extends TestCase
18+
{
19+
public function testFortySixElksOptions()
20+
{
21+
$fortySixElksOptions = (new FortySixElksOptions())->setFrom('test_from')->setRecipientId('test_recipient');
22+
23+
self::assertSame([
24+
'from' => 'test_from',
25+
], $fortySixElksOptions->toArray());
26+
}
27+
}

src/Symfony/Component/Notifier/Bridge/FortySixElks/Tests/FortySixElksTransportTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Notifier\Bridge\FortySixElks\Tests;
1313

1414
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\Notifier\Bridge\FortySixElks\FortySixElksOptions;
1516
use Symfony\Component\Notifier\Bridge\FortySixElks\FortySixElksTransport;
1617
use Symfony\Component\Notifier\Exception\TransportException;
1718
use Symfony\Component\Notifier\Message\ChatMessage;
@@ -37,6 +38,7 @@ public function toStringProvider(): iterable
3738
public function supportedMessagesProvider(): iterable
3839
{
3940
yield [new SmsMessage('+46701111111', 'Hello!')];
41+
yield [new SmsMessage('+46701111111', 'Hello!', 'from', new FortySixElksOptions(['from' => 'foo']))];
4042
}
4143

4244
public function unsupportedMessagesProvider(): iterable

0 commit comments

Comments
 (0)
0