8000 Smsapi - Make "from" optional by szal1k · Pull Request #49261 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Smsapi - Make "from" optional #49261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Smsapi - Make "from" optional
  • Loading branch information
szal1k authored and fabpot committed Feb 10, 2023
commit 8b14052fec49a0925fd59707cbb4a0dfffa5683c
5 changes: 5 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Smsapi/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Changing the option `from` to optional to enable sending "eco" messages

6.2
---

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Notifier/Bridge/Smsapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ SMSAPI_DSN=smsapi://TOKEN@default?from=FROM&fast=FAST&test=TEST

where:
- `TOKEN` is your API Token (OAuth)
- `FROM` is the sender name
- `FROM` is the sender name (default ""), skip this field to use the cheapest "eco" shipping method.
- `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.
- `TEST` setting this parameter to "1" (default "0") will result in sending message in test mode (message is validated, but not sent).

Expand Down
46 changes: 32 additions & 14 deletions src/Symfony/Component/Notifier/Bridge/Smsapi/SmsapiTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ final class SmsapiTransport extends AbstractTransport
protected const HOST = 'api.smsapi.pl';

private string $authToken;
private string $from;
private string $from = '';
private bool $fast = false;
private bool $test = false;

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

public function __toString(): string
{
$dsn = sprintf('smsapi://%s?from=%s', $this->getEndpoint(), $this->from);
$dsn = sprintf('smsapi://%s', $this->getEndpoint());
$params = [];

if ('' !== $this->from) {
$params['from'] = $this->from;
}

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

if ($this->test) {
$dsn .= sprintf('&test=%d', (int) $this->test);
$params['test'] = (int) $this->test;
}

$query = http_build_query($params, '', '&');

if ('' !== $query) {
$dsn .= sprintf('?%s', $query);
}

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

// default request body
$body = [
'to' => $message->getPhone(),
'message' => $message->getSubject(),
'fast' => $this->fast,
'format' => 'json',
'encoding' => 'utf-8',
'test' => $this->test,
];

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

// if from is not empty add it to request body
if ('' !== $from) {
$body['from'] = $from;
}

$endpoint = sprintf('https://%s/sms.do', $this->getEndpoint());
$response = $this->client->request('POST', $endpoint, [
'auth_bearer' => $this->authToken,
'body' => [
'from' => $from,
'to' => $message->getPhone(),
'message' => $message->getSubject(),
'fast' => $this->fast,
'format' => 'json',
'encoding' => 'utf-8',
'test' => $this->test,
],
'body' => $body,
]);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function create(Dsn $dsn): SmsapiTransport
}

$authToken = $this->getUser($dsn);
$from = $dsn->getRequiredOption('from');
$from = $dsn->getOption('from', '');
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$fast = filter_var($dsn->getOption('fast', false), \FILTER_VALIDATE_BOOL);
$test = filter_var($dsn->getOption('test', false), \FILTER_VALIDATE_BOOL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public function createFactory(): SmsapiTransportFactory

public function createProvider(): iterable
{
yield [
'smsapi://host.test',
'smsapi://token@host.test',
];

yield [
'smsapi://host.test?from=testFrom',
'smsapi://token@host.test?from=testFrom',
Expand Down Expand Up @@ -71,6 +76,9 @@ public function createProvider(): iterable

public function supportsProvider(): iterable
{
yield [true, 'smsapi://host'];
yield [true, 'smsapi://host?fast=1'];
yield [true, 'smsapi://host?test=1'];
yield [true, 'smsapi://host?from=testFrom'];
yield [true, 'smsapi://host?from=testFrom&fast=1'];
yield [true, 'smsapi://host?from=testFrom&test=1'];
Expand All @@ -82,14 +90,8 @@ public function incompleteDsnProvider(): iterable
yield 'missing token' => ['smsapi://host.test?from=testFrom'];
}

public function missingRequiredOptionProvider(): iterable
{
yield 'missing option: from' => ['smsapi://token@host'];
}

public function unsupportedSchemeProvider(): iterable
{
yield ['somethingElse://token@host?from=testFrom'];
yield ['somethingElse://token@host']; // missing "from" option
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@

final class SmsapiTransportTest extends TransportTestCase
{
public function createTransport(HttpClientInterface $client = null, bool $fast = false, bool $test = false): SmsapiTransport
public function createTransport(HttpClientInterface $client = null, string $from = '', bool $fast = false, bool $t 79CC est = false): SmsapiTransport
{
return (new SmsapiTransport('testToken', 'testFrom', $client ?? $this->createMock(HttpClientInterface::class)))->setHost('test.host')->setFast($fast)->setTest($test);
return (new SmsapiTransport('testToken', $from, $client ?? $this->createMock(HttpClientInterface::class)))->setHost('test.host')->setFast($fast)->setTest($test);
}

public function toStringProvider(): iterable
{
yield ['smsapi://test.host?from=testFrom', $this->createTransport()];
yield ['smsapi://test.host?from=testFrom&fast=1', $this->createTransport(null, true)];
yield ['smsapi://test.host?from=testFrom&test=1', $this->createTransport(null, false, true)];
yield ['smsapi://test.host?from=testFrom&fast=1&test=1', $this->createTransport(null, true, true)];
yield ['smsapi://test.host', $this->createTransport()];
yield ['smsapi://test.host?fast=1', $this->createTransport(null, '', true)];
yield ['smsapi://test.host?test=1', $this->createTransport(null, '', false, true)];
yield ['smsapi://test.host?fast=1&test=1', $this->createTransport(null, '', true, true)];
yield ['smsapi://test.host?from=testFrom', $this->createTransport(null, 'testFrom')];
yield ['smsapi://test.host?from=testFrom&fast=1', $this->createTransport(null, 'testFrom', true)];
yield ['smsapi://test.host?from=testFrom&test=1', $this->createTransport(null, 'testFrom', false, true)];
yield ['smsapi://test.host?from=testFrom&fast=1&test=1', $this->createTransport(null, 'testFrom', true, true)];
}

public function supportedMessagesProvider(): iterable
Expand Down
0