8000 feature #49261 Smsapi - Make "from" optional (szal1k) · symfony/symfony@039e883 · GitHub
[go: up one dir, main page]

Skip to content

Commit 039e883

Browse files
committed
feature #49261 Smsapi - Make "from" optional (szal1k)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- Smsapi - Make "from" optional Smsapi.com API have option to send "eco" messages, default with drawn phone number instead of "from" name defined in Smsapi.com User Panel. So, when from is not set in body of request, in default message is send as "eco". My changes make it possible. | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | Commits ------- 8b14052 Smsapi - Make "from" optional
2 parents d07cd3f + 8b14052 commit 039e883

File tree

6 files changed

+57
-28
lines changed

6 files changed

+57
-28
lines changed

src/Symfony/Component/Notifier/Bridge/Smsapi/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+
* Changing the option `from` to optional to enable sending "eco" messages
8+
49
6.2
510
---
611

src/Symfony/Component/Notifier/Bridge/Smsapi/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ SMSAPI_DSN=smsapi://TOKEN@default?from=FROM&fast=FAST&test=TEST
1212

1313
where:
1414
- `TOKEN` is your API Token (OAuth)
15-
- `FROM` is the sender name
15+
- `FROM` is the sender name (default ""), skip this field to use the cheapest "eco" shipping method.
1616
- `FAST` setting this parameter to "1" (default "0") will result in sending message with the highest priority which ensures the quickest possible time of delivery. Attention! Fast messages cost more than normal messages.
1717
- `TEST` setting this parameter to "1" (default "0") will result in sending message in test mode (message is validated, but not sent).
1818

src/Symfony/Component/Notifier/Bridge/Smsapi/SmsapiTransport.php

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ final class SmsapiTransport extends AbstractTransport
3030
protected const HOST = 'api.smsapi.pl';
3131

3232
private string $authToken;
33-
private string $from;
33+
private string $from = '';
3434
private bool $fast = false;
3535
private bool $test = false;
3636

37-
public function __construct(#[\SensitiveParameter] string $authToken, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
37+
public function __construct(#[\SensitiveParameter] string $authToken, string $from = '', HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
3838
{
3939
$this->authToken = $authToken;
4040
$this->from = $from;
@@ -64,14 +64,25 @@ public function setTest(bool $test): static
6464

6565
public function __toString(): string
6666
{
67-
$dsn = sprintf('smsapi://%s?from=%s', $this->getEndpoint(), $this->from);
67+
$dsn = sprintf('smsapi://%s', $this->getEndpoint());
68+
$params = [];
69+
70+
if ('' !== $this->from) {
71+
$params['from'] = $this->from;
72+
}
6873

6974
if ($this->fast) {
70-
$dsn .= sprintf('&fast=%d', (int) $this->fast);
75+
$params['fast'] = (int) $this->fast;
7176
}
7277

7378
if ($this->test) {
74-
$dsn .= sprintf('&test=%d', (int) $this->test);
79+
$params['test'] = (int) $this->test;
80+
}
81+
82+
$query = http_build_query($params, '', '&');
83+
84+
if ('' !== $query) {
85+
$dsn .= sprintf('?%s', $query);
7586
}
7687

7788
return $dsn;
@@ -88,20 +99,27 @@ protected function doSend(MessageInterface $message): SentMessage
8899
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
89100
}
90101

102+
// default request body
103+
$body = [
104+
'to' => $message->getPhone(),
105+
'message' => $message->getSubject(),
106+
'fast' => $this->fast,
107+
'format' => 'json',
108+
'encoding' => 'utf-8',
109+
'test' => $this->test,
110+
];
111+
91112
$from = $message->getFrom() ?: $this->from;
92113

114+
// if from is not empty add it to request body
115+
if ('' !== $from) {
116+
$body['from'] = $from;
117+
}
118+
93119
$endpoint = sprintf('https://%s/sms.do', $this->getEndpoint());
94120
$response = $this->client->request('POST', $endpoint, [
95121
'auth_bearer' => $this->authToken,
96-
'body' => [
97-
'from' => $from,
98-
'to' => $message->getPhone(),
99-
'message' => $message->getSubject(),
100-
'fast' => $this->fast,
101-
'format' => 'json',
102-
'encoding' => 'utf-8',
103-
'test' => $this->test,
104-
],
122+
'body' => $body,
105123
]);
106124

107125
try {

src/Symfony/Component/Notifier/Bridge/Smsapi/SmsapiTransportFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function create(Dsn $dsn): SmsapiTransport
2929
}
3030

3131
$authToken = $this->getUser($dsn);
32-
$from = $dsn->getRequiredOption('from');
32+
$from = $dsn->getOption('from', '');
3333
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
3434
$fast = filter_var($dsn->getOption('fast', false), \FILTER_VALIDATE_BOOL);
3535
$test = filter_var($dsn->getOption('test', false), \FILTER_VALIDATE_BOOL);

src/Symfony/Component/Notifier/Bridge/Smsapi/Tests/SmsapiTransportFactoryTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public function createFactory(): SmsapiTransportFactory
2323

2424
public function createProvider(): iterable
2525
{
26+
yield [
27+
'smsapi://host.test',
28+
'smsapi://token@host.test',
29+
];
30+
2631
yield [
2732
'smsapi://host.test?from=testFrom',
2833
'smsapi://token@host.test?from=testFrom',
@@ -71,6 +76,9 @@ public function createProvider(): iterable
7176

7277
public function supportsProvider(): iterable
7378
{
79+
yield [true, 'smsapi://host'];
80+
yield [true, 'smsapi://host?fast=1'];
81+
yield [true, 'smsapi://host?test=1'];
7482
yield [true, 'smsapi://host?from=testFrom'];
7583
yield [true, 'smsapi://host?from=testFrom&fast=1'];
7684
yield [true, 'smsapi://host?from=testFrom&test=1'];
@@ -82,14 +90,8 @@ public function incompleteDsnProvider(): iterable
8290
yield 'missing token' => ['smsapi://host.test?from=testFrom'];
8391
}
8492

85-
public function missingRequiredOptionProvider(): iterable
86-
{
87-
yield 'missing option: from' => ['smsapi://token@host'];
88-
}
89-
9093
public function unsupportedSchemeProvider(): iterable
9194
{
9295
yield ['somethingElse://token@host?from=testFrom'];
93-
yield ['somethingElse://token@host']; // missing "from" option
9496
}
9597
}

src/Symfony/Component/Notifier/Bridge/Smsapi/Tests/SmsapiTransportTest.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,21 @@
2323

2424
final class SmsapiTransportTest extends TransportTestCase
2525
{
26-
public function createTransport(HttpClientInterface $client = null, bool $fast = false, bool $test = false): SmsapiTransport
26+
public function createTransport(HttpClientInterface $client = null, string $from = '', bool $fast = false, bool $test = false): SmsapiTransport
2727
{
28-
return (new SmsapiTransport('testToken', 'testFrom', $client ?? $this->createMock(HttpClientInterface::class)))->setHost('test.host')->setFast($fast)->setTest($test);
28+
return (new SmsapiTransport('testToken', $from, $client ?? $this->createMock(HttpClientInterface::class)))->setHost('test.host')->setFast($fast)->setTest($test);
2929
}
3030

3131
public function toStringProvider(): iterable
3232
{
33-
yield ['smsapi://test.host?from=testFrom', $this->createTransport()];
34-
yield ['smsapi://test.host?from=testFrom&fast=1', $this->createTransport(null, true)];
35-
yield ['smsapi://test.host?from=testFrom&test=1', $this->createTransport(null, false, true)];
36-
yield ['smsapi://test.host?from=testFrom&fast=1&test=1', $this->createTransport(null, true, true)];
33+
yield ['smsapi://test.host', $this->createTransport()];
34+
yield ['smsapi://test.host?fast=1', $this->createTransport(null, '', true)];
35+
yield ['smsapi://test.host?test=1', $this->createTransport(null, '', false, true)];
36+
yield ['smsapi://test.host?fast=1&test=1', $this->createTransport(null, '', true, true)];
37+
yield ['smsapi://test.host?from=testFrom', $this->createTransport(null, 'testFrom')];
38+
yield ['smsapi://test.host?from=testFrom&fast=1', $this->createTransport(null, 'testFrom', true)];
39+
yield ['smsapi://test.host?from=testFrom&test=1', $this->createTransport(null, 'testFrom', false, true)];
40+
yield ['smsapi://test.host?from=testFrom&fast=1&test=1', $this->createTransport(null, 'testFrom', true, true)];
3741
}
3842

3943
public function supportedMessagesProvider(): iterable

0 commit comments

Comments
 (0)
0