Skip to content

Commit fc83262

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

File tree

5 files changed

+147
-6
lines changed

5 files changed

+147
-6
lines changed

src/Symfony/Component/Notifier/Bridge/GatewayApi/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+
* Use `GatewayApiOptions` class
8+
49
6.2
510
---
611

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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\GatewayApi;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author gnito-org <https://github.com/gnito-org>
18+
*/
19+
final class GatewayApiOptions implements MessageOptionsInterface
20+
{
21+
private array $options;
22+
23+
public function __construct(array $options = [])
24+
{
25+
$this->options = $options;
26+
}
27+
28+
public function getClass(): ?int
29+
{
30+
return $this->options['class'] ?? null;
31+
}
32+
33+
public function getUserRef(): ?string
34+
{
35+
return $this->options['user_ref'] ?? null;
36+
}
37+
38+
public function getCallbackUrl(): ?string
39+
{
40+
return $this->options['callback_url'] ?? null;
41+
}
42+
43+
public function getFrom(): ?string
44+
{
45+
return $this->options['from'] ?? null;
46+
}
47+
48+
public function getRecipientId(): ?string
49+
{
50+
return $this->options['recipient_id'] ?? null;
51+
}
52+
53+
public function setClass(int $class): self
54+
{
55+
$this->options['class'] = $class;
56+
57+
return $this;
58+
}
59+
60+
public function setUserRef(string $userRef): self
61+
{
62+
$this->options['user_ref'] = $userRef;
63+
64+
return $this;
65+
}
66+
67+
public function setCallbackUrl(string $callbackUrl): self
68+
{
69+
$this->options['callback_url'] = $callbackUrl;
70+
71+
return $this;
72+
}
73+
74+
public function setFrom(string $from): self
75+
{
76+
$this->options['from'] = $from;
77+
78+
return $this;
79+
}
80+
81+
public function setRecipientId(string $id): self
82+
{
83+
$this->options['recipient_id'] = $id;
84+
85+
return $this;
86+
}
87+
88+
public function toArray(): array
89+
{
90+
$options = $this->options;
91+
if (isset($options['recipient_id'])) {
92+
unset($options['recipient_id']);
93+
}
94+
95+
return $options;
96+
}
97+
}

src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiTransport.php

+13-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 GatewayApiOptions);
5050
}
5151

5252
protected function doSend(MessageInterface $message): SentMessage
@@ -57,15 +57,22 @@ 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['sender'] = $options['from'] ?? $from;
63+
$options['recipients'] = [['msisdn' => $message->getPhone()]];
64+
$options['message'] = $message->getSubject();
65+
66+
if (isset($options['user_ref'])) {
67+
$options['userref'] = $options['user_ref'];
68+
unset($options['user_ref']);
69+
}
70+
6071
$endpoint = sprintf('https://%s/rest/mtsms', $this->getEndpoint());
6172

6273
$response = $this->client->request('POST', $endpoint, [
6374
'auth_basic' => [$this->authToken, ''],
64-
'json' => [
65-
'sender' => $from,
66-
'recipients' => [['msisdn' => $message->getPhone()]],
67-
'message' => $message->getSubject(),
68-
],
75+
'json' => array_filter($options),
6976
]);
7077

7178
try {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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\GatewayApi\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiOptions;
16+
17+
class GatewayApiOptionsTest extends TestCase
18+
{
19+
public function testGatewayApiOptions()
20+
{
21+
$gatewayApiOptions = (new GatewayApiOptions())->setFrom('test_from')->setClass('test_class')->setCallbackUrl('test_callback_url')->setUserRef('test_user_ref')->setRecipientId('test_recipient');
22+
23+
self::assertSame([
24+
'from' => 'test_from',
25+
'class' => 'test_class',
26+
'callback_url' => 'test_callback_url',
27+
'user_ref' => 'test_user_ref',
28+
], $gatewayApiOptions->toArray());
29+
}
30+
}

src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiTransportTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Notifier\Bridge\GatewayApi\Tests;
1313

1414
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiOptions;
1516
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransport;
1617
use Symfony\Component\Notifier\Message\ChatMessage;
1718
use Symfony\Component\Notifier\Message\MessageInterface;
@@ -40,6 +41,7 @@ public function toStringProvider(): iterable
4041
public function supportedMessagesProvider(): iterable
4142
{
4243
yield [new SmsMessage('0611223344', 'Hello!')];
44+
yield [new SmsMessage('0611223344', 'Hello!', 'from', new GatewayApiOptions(['from' => 'foo']))];
4345
}
4446

4547
public function unsupportedMessagesProvider(): iterable

0 commit comments

Comments
 (0)