From 4a8cb6845e06d777bda16bdfb07d39d2c5312b92 Mon Sep 17 00:00:00 2001 From: gnito-org <70450336+gnito-org@users.noreply.github.com> Date: Fri, 9 Dec 2022 10:36:33 -0400 Subject: [PATCH 1/2] [Notifier] Add SMS options to GatewayApi notifier --- .../Notifier/Bridge/GatewayApi/CHANGELOG.md | 5 + .../Bridge/GatewayApi/GatewayApiOptions.php | 97 +++++++++++++++++++ .../Bridge/GatewayApi/GatewayApiTransport.php | 19 ++-- .../Tests/GatewayApiOptionsTest.php | 30 ++++++ .../Tests/GatewayApiTransportTest.php | 2 + 5 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiOptions.php create mode 100644 src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiOptionsTest.php 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 From ccfe220282095fff189576a6d91e840b69a04bce Mon Sep 17 00:00:00 2001 From: gnito-org <70450336+gnito-org@users.noreply.github.com> Date: Sat, 10 Dec 2022 10:36:21 -0400 Subject: [PATCH 2/2] [Notifier] Add SMS options to OrangeSms notifier --- .../Notifier/Bridge/OrangeSms/CHANGELOG.md | 5 ++ .../Bridge/OrangeSms/OrangeSmsOptions.php | 61 +++++++++++++++++++ .../Bridge/OrangeSms/OrangeSmsTransport.php | 10 ++- .../OrangeSms/Tests/OrangeSmsOptionsTest.php | 27 ++++++++ .../Tests/OrangeSmsTransportTest.php | 2 + 5 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 src/Symfony/Component/Notifier/Bridge/OrangeSms/OrangeSmsOptions.php create mode 100644 src/Symfony/Component/Notifier/Bridge/OrangeSms/Tests/OrangeSmsOptionsTest.php diff --git a/src/Symfony/Component/Notifier/Bridge/OrangeSms/CHANGELOG.md b/src/Symfony/Component/Notifier/Bridge/OrangeSms/CHANGELOG.md index 4662c26671e03..d03cb3eda36e3 100644 --- a/src/Symfony/Component/Notifier/Bridge/OrangeSms/CHANGELOG.md +++ b/src/Symfony/Component/Notifier/Bridge/OrangeSms/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.3 +--- + + * Add `OrangeSmsOptions` class + 6.2 --- diff --git a/src/Symfony/Component/Notifier/Bridge/OrangeSms/OrangeSmsOptions.php b/src/Symfony/Component/Notifier/Bridge/OrangeSms/OrangeSmsOptions.php new file mode 100644 index 0000000000000..772be4aaac76b --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/OrangeSms/OrangeSmsOptions.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\OrangeSms; + +use Symfony\Component\Notifier\Message\MessageOptionsInterface; + +/** + * @author gnito-org + */ +final class OrangeSmsOptions implements MessageOptionsInterface +{ + private array $options; + + public function __construct(array $options = []) + { + $this->options = $options; + } + + public function getFrom(): ?string + { + return $this->options['from'] ?? null; + } + + public function getRecipientId(): ?string + { + return $this->options['recipient_id'] ?? null; + } + + 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/OrangeSms/OrangeSmsTransport.php b/src/Symfony/Component/Notifier/Bridge/OrangeSms/OrangeSmsTransport.php index 6af71e0641c0a..c10687b761e83 100644 --- a/src/Symfony/Component/Notifier/Bridge/OrangeSms/OrangeSmsTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/OrangeSms/OrangeSmsTransport.php @@ -50,7 +50,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 OrangeSmsOptions); } public function doSend(MessageInterface $message): SentMessage @@ -61,7 +61,11 @@ public function doSend(MessageInterface $message): SentMessage $from = $message->getFrom() ?: $this->from; - $url = 'https://'.$this->getEndpoint().'/smsmessaging/v1/outbound/'.urlencode('tel:'.$from).'/requests'; + $opts = $message->getOptions(); + $options = $opts ? $opts->toArray() : []; + $options['from'] = $options['from'] ?? $from; + + $url = 'https://'.$this->getEndpoint().'/smsmessaging/v1/outbound/'.urlencode('tel:'.$options['from']).'/requests'; $headers = [ 'Authorization' => 'Bearer '.$this->getAccessToken(), 'Content-Type' => 'application/json', @@ -70,7 +74,7 @@ public function doSend(MessageInterface $message): SentMessage $payload = [ 'outboundSMSMessageRequest' => [ 'address' => 'tel:'.$message->getPhone(), - 'senderAddress' => 'tel:'.$from, + 'senderAddress' => 'tel:'.$options['from'], 'outboundSMSTextMessage' => [ 'message' => $message->getSubject(), ], diff --git a/src/Symfony/Component/Notifier/Bridge/OrangeSms/Tests/OrangeSmsOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/OrangeSms/Tests/OrangeSmsOptionsTest.php new file mode 100644 index 0000000000000..717cbc29e9034 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/OrangeSms/Tests/OrangeSmsOptionsTest.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\OrangeSms\Tests; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Notifier\Bridge\OrangeSms\OrangeSmsOptions; + +class OrangeSmsOptionsTest extends TestCase +{ + public function testOrangeSmsOptions() + { + $orangeSmsOptions = (new OrangeSmsOptions())->setFrom('test_from')->setRecipientId('test_recipient'); + + self::assertSame([ + 'from' => 'test_from', + ], $orangeSmsOptions->toArray()); + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/OrangeSms/Tests/OrangeSmsTransportTest.php b/src/Symfony/Component/Notifier/Bridge/OrangeSms/Tests/OrangeSmsTransportTest.php index 62d6a9f247dd2..2a869218ba02d 100644 --- a/src/Symfony/Component/Notifier/Bridge/OrangeSms/Tests/OrangeSmsTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/OrangeSms/Tests/OrangeSmsTransportTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Notifier\Bridge\OrangeSms\Tests; use Symfony\Component\HttpClient\MockHttpClient; +use Symfony\Component\Notifier\Bridge\OrangeSms\OrangeSmsOptions; use Symfony\Component\Notifier\Bridge\OrangeSms\OrangeSmsTransport; use Symfony\Component\Notifier\Message\ChatMessage; use Symfony\Component\Notifier\Message\SmsMessage; @@ -34,6 +35,7 @@ public static function toStringProvider(): iterable public static function supportedMessagesProvider(): iterable { yield [new SmsMessage('+243899999999', 'Hello World!')]; + yield [new SmsMessage('+243899999999', 'Hello World!'), 'from', new OrangeSmsOptions(['from' => 'foo'])]; } public static function unsupportedMessagesProvider(): iterable