8000 feature #48575 [Notifier] Add SMS options to Esendex notifier (gnito-… · symfony/symfony@9d51aeb · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d51aeb

Browse files
feature #48575 [Notifier] Add SMS options to Esendex notifier (gnito-org)
This PR was merged into the 6.3 branch. Discussion ---------- [Notifier] Add SMS options to Esendex notifier | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | | License | MIT | Doc PR | N/A <!-- Replace this notice by a short README for your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Commits ------- c25cea7 [Notifier] Add SMS options to Esendex notifier
2 parents 35a4b64 + c25cea7 commit 9d51aeb

File tree

5 files changed

+118
-12
lines changed

5 files changed

+118
-12
lines changed

src/Symfony/Component/Notifier/Bridge/Esendex/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 `EsendexOptions` class
8+
49
6.2
510
---
611

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

src/Symfony/Component/Notifier/Bridge/Esendex/EsendexTransport.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __toString(): string
4949

5050
public function supports(MessageInterface $message): bool
5151
{
52-
return $message instanceof SmsMessage;
52+
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof EsendexOptions);
5353
}
5454

5555
protected function doSend(MessageInterface $message): SentMessage
@@ -58,26 +58,24 @@ protected function doSend(MessageInterface $message): SentMessage
5858
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
5959
}
6060

61-
$messageData = [
61+
$from = $message->getFrom() ?: $this->from;
62+
63+
$opts = $message->getOptions();
64+
$options = $opts ? $opts->toArray() : [];
65+
$options['from'] = $options['from'] ?? $from;
66+
$options['messages'] = [
6267
'to' => $message->getPhone(),
6368
'body' => $message->getSubject(),
6469
];
65-
66-
if ('' !== $message->getFrom()) {
67-
$messageData['from'] = $message->getFrom();
68-
} elseif (null !== $this->from) {
69-
$messageData['from'] = $this->from;
70-
}
70+
$options['accountreference'] = $options['account_reference'] ?? $this->accountReference;
71+
unset($options['account_reference']);
7172

7273
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/v1.0/messagedispatcher', [
7374
'auth_basic' => sprintf('%s:%s', $this->email, $this->password),
7475
'headers' => [
7576
'Accept' => 'application/json',
7677
],
77-
'json' => [
78-
'accountreference' => $this->accountReference,
79-
'messages' => [$messageData],
80-
],
78+
'json' => array_filter($options),
8179
]);
8280

8381
try {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\Esendex\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Esendex\EsendexOptions;
16+
17+
class EsendexOptionsTest extends TestCase
18+
{
19+
public function testEsendexOptions()
20+
{
21+
$esendexOptions = (new EsendexOptions())->setFrom('test_from')->setAccountReference('test_account_reference')->setRecipientId('test_recipient');
22+
23+
self::assertSame([
24+
'from' => 'test_from',
25+
'account_reference' => 'test_account_reference',
26+
], $esendexOptions->toArray());
27+
}
28+
}

src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexTransportTest.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\Esendex\Tests;
1313

1414
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\Notifier\Bridge\Esendex\EsendexOptions;
1516
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransport;
1617
use Symfony\Component\Notifier\Exception\TransportException;
1718
use Symfony\Component\Notifier\Message\ChatMessage;
@@ -36,6 +37,7 @@ public static function toStringProvider(): iterable
3637
public static function supportedMessagesProvider(): iterable
3738
{
3839
yield [new SmsMessage('0611223344', 'Hello!')];
40+
yield [new SmsMessage('0611223344', 'Hello!', 'from', new EsendexOptions(['from' => 'foo']))];
3941
}
4042

4143
public static function unsupportedMessagesProvider(): iterable

0 commit comments

Comments
 (0)
0