diff --git a/src/Symfony/Component/Notifier/Bridge/GatewayApi/CHANGELOG.md b/src/Symfony/Component/Notifier/Bridge/GatewayApi/CHANGELOG.md index 5092c668a1f20..d87c05f80f0f6 100644 --- a/src/Symfony/Component/Notifier/Bridge/GatewayApi/CHANGELOG.md +++ b/src/Symfony/Component/Notifier/Bridge/GatewayApi/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.3 +--- + + * Use `GatewayApiOptions` class + 6.2 --- diff --git a/src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiOptions.php b/src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiOptions.php new file mode 100644 index 0000000000000..d983b6bf92cd1 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiOptions.php @@ -0,0 +1,97 @@ + + * + * 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 + */ +final class GatewayApiOptions implements MessageOptionsInterface +{ + private array $options; + + public function __construct(array $options = []) + { + $this->options = $options; + } + + public function getClass(): ?int + { + return $this->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; + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiTransport.php b/src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiTransport.php index 543decc040dab..f714df587efb1 100644 --- a/src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiTransport.php @@ -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 @@ -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 { diff --git a/src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiOptionsTest.php new file mode 100644 index 0000000000000..b0c40fad55e4f --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiOptionsTest.php @@ -0,0 +1,30 @@ + + * + * 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()); + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiTransportTest.php b/src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiTransportTest.php index 2d0b222e96f12..d3b67bc85c2b2 100644 --- a/src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiTransportTest.php @@ -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; @@ -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