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

Skip to content

Commit 186036b

Browse files
committed
[Notifier] Add SMS options to Esendex notifier
1 parent 45113ba commit 186036b

File tree

4 files changed

+116
-12
lines changed
  • src/Symfony/Component/Notifier/Bridge/Esendex

4 files changed

+116
-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+
}

0 commit comments

Comments
 (0)
0