From 9fd8dfd5e8ff0070a0a7ee0438545a3d3073516f Mon Sep 17 00:00:00 2001 From: gnito-org <70450336+gnito-org@users.noreply.github.com> Date: Fri, 9 Dec 2022 14:04:00 -0400 Subject: [PATCH] [Notifier] Ass SMS options to ContactEveryone notifier --- .../Bridge/ContactEveryone/CHANGELOG.md | 7 +- .../ContactEveryoneOptions.php | 85 +++++++++++++++++++ .../ContactEveryoneTransport.php | 16 ++-- .../Tests/ContactEveryoneOptionsTest.php | 29 +++++++ .../Tests/ContactEveryoneTransportTest.php | 2 + 5 files changed, 131 insertions(+), 8 deletions(-) create mode 100644 src/Symfony/Component/Notifier/Bridge/ContactEveryone/ContactEveryoneOptions.php create mode 100644 src/Symfony/Component/Notifier/Bridge/ContactEveryone/Tests/ContactEveryoneOptionsTest.php diff --git a/src/Symfony/Component/Notifier/Bridge/ContactEveryone/CHANGELOG.md b/src/Symfony/Component/Notifier/Bridge/ContactEveryone/CHANGELOG.md index 04168c04c9bc7..541e59c47fa15 100644 --- a/src/Symfony/Component/Notifier/Bridge/ContactEveryone/CHANGELOG.md +++ b/src/Symfony/Component/Notifier/Bridge/ContactEveryone/CHANGELOG.md @@ -1,8 +1,13 @@ CHANGELOG ========= +6.3 +--- + + * Add the `ContactEveryoneOptions` class + 6.2 --- * Add the bridge - * Throw exception when `SmsMessage->from` defined \ No newline at end of file + * Throw exception when `SmsMessage->from` defined diff --git a/src/Symfony/Component/Notifier/Bridge/ContactEveryone/ContactEveryoneOptions.php b/src/Symfony/Component/Notifier/Bridge/ContactEveryone/ContactEveryoneOptions.php new file mode 100644 index 0000000000000..4891c06e4a2ab --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/ContactEveryone/ContactEveryoneOptions.php @@ -0,0 +1,85 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\ContactEveryone; + +use Symfony\Component\Notifier\Message\MessageOptionsInterface; + +/** + * @author gnito-org + */ +final class ContactEveryoneOptions implements MessageOptionsInterface +{ + private array $options; + + public function __construct(array $options = []) + { + $this->options = $options; + } + + public function getDiffusionName(): ?int + { + return $this->options['diffusion_name'] ?? null; + } + + public function getCategory(): ?string + { + return $this->options['category'] ?? null; + } + + public function getFrom(): ?string + { + return $this->options['from'] ?? null; + } + + public function getRecipientId(): ?string + { + return $this->options['recipient_id'] ?? null; + } + + public function setDiffusionName(int $diffusionName): self + { + $this->options['diffusion_name'] = $diffusionName; + + return $this; + } + + public function setCategory(string $category): self + { + $this->options['category'] = $category; + + 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/ContactEveryone/ContactEveryoneTransport.php b/src/Symfony/Component/Notifier/Bridge/ContactEveryone/ContactEveryoneTransport.php index 9a00388e2d837..3b807cabc7b15 100644 --- a/src/Symfony/Component/Notifier/Bridge/ContactEveryone/ContactEveryoneTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/ContactEveryone/ContactEveryoneTransport.php @@ -59,7 +59,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 ContactEveryoneOptions); } protected function doSend(MessageInterface $message): SentMessage @@ -72,14 +72,16 @@ protected function doSend(MessageInterface $message): SentMessage throw new InvalidArgumentException(sprintf('The "%s" transport does not support "from" in "%s".', __CLASS__, SmsMessage::class)); } + $opts = $message->getOptions(); + $options = $opts ? $opts->toArray() : []; + $options['xcharset'] = 'true'; + $options['token'] = $this->token; + $options['to'] = $message->getPhone(); + $options['msg'] = $message->getSubject(); + $endpoint = sprintf('https://%s/api/light/diffusions/sms', self::HOST); $response = $this->client->request('POST', $endpoint, [ - 'query' => [ - 'xcharset' => 'true', - 'token' => $this->token, - 'to' => $message->getPhone(), - 'msg' => $message->getSubject(), - ], + 'query' => array_filter($options), ]); try { diff --git a/src/Symfony/Component/Notifier/Bridge/ContactEveryone/Tests/ContactEveryoneOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/ContactEveryone/Tests/ContactEveryoneOptionsTest.php new file mode 100644 index 0000000000000..a272c0bbe0709 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/ContactEveryone/Tests/ContactEveryoneOptionsTest.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\ContactEveryone\Tests; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneOptions; + +class ContactEveryoneOptionsTest extends TestCase +{ + public function testContactEveryoneOptions() + { + $contactEveryoneOptions = (new ContactEveryoneOptions())->setFrom('test_from')->setCategory('test_category')->setDiffusionName('test_diffusion_name')->setRecipientId('test_recipient'); + + self::assertSame([ + 'from' => 'test_from', + 'category' => 'test_category', + 'diffusion_name' => 'test_diffusion_name', + ], $contactEveryoneOptions->toArray()); + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/ContactEveryone/Tests/ContactEveryoneTransportTest.php b/src/Symfony/Component/Notifier/Bridge/ContactEveryone/Tests/ContactEveryoneTransportTest.php index 97194efdd87be..3f0c020fc14ea 100644 --- a/src/Symfony/Component/Notifier/Bridge/ContactEveryone/Tests/ContactEveryoneTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/ContactEveryone/Tests/ContactEveryoneTransportTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Notifier\Bridge\ContactEveryone\Tests; use Symfony\Component\HttpClient\MockHttpClient; +use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneOptions; use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneTransport; use Symfony\Component\Notifier\Exception\InvalidArgumentException; use Symfony\Component\Notifier\Message\ChatMessage; @@ -36,6 +37,7 @@ public function toStringProvider(): iterable public function supportedMessagesProvider(): iterable { yield [new SmsMessage('0611223344', 'Hello!')]; + yield [new SmsMessage('0611223344', 'Hello!', 'from', new ContactEveryoneOptions(['from' => 'foo']))]; } public function unsupportedMessagesProvider(): iterable