10000 [Notifier] Add SMS options to GatewayApi notifier by gnito-org · Pull Request #48579 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Notifier] Add SMS options to GatewayApi notifier #48579

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
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
8000 Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Notifier] Add SMS options to GatewayApi notifier
  • Loading branch information
gnito-org authored and nicolas-grekas committed May 12, 2023
commit 4a8cb6845e06d777bda16bdfb07d39d2c5312b92
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Use `GatewayApiOptions` class

6.2
---

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\GatewayApi;

use Symfony\Component\Notifier\Message\MessageOptionsInterface;

/**
* @author gnito-org <https://github.com/gnito-org>
*/
final class GatewayApiOptions implements MessageOptionsInterface
{
private array $options;

public function __construct(array $options = [])
{
$this->options = $options;
}

public function getClass(): ?int
{
return $th 8000 is->options['class'] ?? null;
}

public function getUserRef(): ?string
{
return $this->options['user_ref'] ?? null;
}

public function getCallbackUrl(): ?string
{
return $this->options['callback_url'] ?? null;
}

public function getFrom(): ?string
{
return $this->options['from'] ?? null;
}

public function getRecipientId(): ?string
{
return $this->options['recipient_id'] ?? null;
}

public function setClass(int $class): self
{
$this->options['class'] = $class;

return $this;
}

public function setUserRef(string $userRef): self
{
$this->options['user_ref'] = $userRef;

return $this;
}

public function setCallbackUrl(string $callbackUrl): self
{
$this->options['callback_url'] = $callbackUrl;

return $this;
}

public function setFrom(string $from): self
{
$this->options['from'] = $from;

return $this;
}

public function setRecipientId(string $id): self
{
$this->options['recipient_id'] = $id;

return $this;
}

public function toArray(): array
{
$options = $this->options;
if (isset($options['recipient_id'])) {
unset($options['recipient_id']);
}

return $options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __toString(): string

public function supports(MessageInterface $message): bool
{
return $message instanceof SmsMessage;
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof GatewayApiOptions);
}

protected function doSend(MessageInterface $message): SentMessage
Expand All @@ -57,15 +57,22 @@ protected function doSend(MessageInterface $message): SentMessage

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

$opts = $message->getOptions();
$options = $opts ? $opts->toArray() : [];
$options['sender'] = $options['from'] ?? $from;
$options['recipients'] = [['msisdn' => $message->getPhone()]];
$options['message'] = $message->getSubject();

if (isset($options['user_ref'])) {
$options['userref'] = $options['user_ref'];
unset($options['user_ref']);
}

$endpoint = sprintf('https://%s/rest/mtsms', $this->getEndpoint());

$response = $this->client->request('POST', $endpoint, [
'auth_basic' => [$this->authToken, ''],
'json' => [
'sender' => $from,
'recipients' => [['msisdn' => $message->getPhone()]],
'message' => $message->getSubject(),
],
'json' => array_filter($options),
]);

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\GatewayApi\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiOptions;

class GatewayApiOptionsTest extends TestCase
{
public function testGatewayApiOptions()
{
$gatewayApiOptions = (new GatewayApiOptions())->setFrom('test_from')->setClass('test_class')->setCallbackUrl('test_callback_url')->setUserRef('test_user_ref')->setRecipientId('test_recipient');

self::assertSame([
'from' => 'test_from',
'class' => 'test_class',
'callback_url' => 'test_callback_url',
'user_ref' => 'test_user_ref',
], $gatewayApiOptions->toArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Notifier\Bridge\GatewayApi\Tests;

use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiOptions;
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransport;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
Expand Down Expand Up @@ -40,6 +41,7 @@ public function toStringProvider(): iterable
public function supportedMessagesProvider(): iterable
{
yield [new SmsMessage('0611223344', 'Hello!')];
yield [new SmsMessage('0611223344', 'Hello!', 'from', new GatewayApiOptions(['from' => 'foo']))];
}

public function unsupportedMessagesProvider(): iterable
Expand Down
0