diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/CHANGELOG.md b/src/Symfony/Component/Notifier/Bridge/MessageBird/CHANGELOG.md index 5092c668a1f20..26d0b1ad01301 100644 --- a/src/Symfony/Component/Notifier/Bridge/MessageBird/CHANGELOG.md +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.3 +--- + + * Add `MessageBirdOptions` class + 6.2 --- diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdOptions.php b/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdOptions.php new file mode 100644 index 0000000000000..a7b270e00ab68 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdOptions.php @@ -0,0 +1,205 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\MessageBird; + +use Symfony\Component\Notifier\Message\MessageOptionsInterface; + +/** + * @author gnito-org + */ +final class MessageBirdOptions implements MessageOptionsInterface +{ + private array $options; + + public function __construct(array $options = []) + { + $this->options = $options; + } + + public function getCreatedDatetime(): ?string + { + return $this->options['created_datetime'] ?? null; + } + + public function getDataCoding(): ?string + { + return $this->options['data_coding'] ?? null; + } + + public function getFrom(): ?string + { + return $this->options['from'] ?? null; + } + + public function getGateway(): ?int + { + return $this->options['gateway'] ?? null; + } + + public function getGroupIds(): ?array + { + return $this->options['group_ids'] ?? null; + } + + public function getMClass(): ?int + { + return $this->options['m_class'] ?? null; + } + + public function getRecipientId(): ?string + { + return $this->options['recipient_id'] ?? null; + } + + public function getReference(): ?string + { + return $this->options['reference'] ?? null; + } + + public function getReportUrl(): ?string + { + return $this->options['report_url'] ?? null; + } + + public function getScheduledDatetime(): ?string + { + return $this->options['scheduled_datetime'] ?? null; + } + + public function getShortenUrls(): ?bool + { + return $this->options['shorten_urls'] ?? null; + } + + public function getType(): ?string + { + return $this->options['type'] ?? null; + } + + public function getTypeDetails(): ?string + { + return $this->options['type_details'] ?? null; + } + + public function getValidity(): ?int + { + return $this->options['validity'] ?? null; + } + + public function setCreatedDatetime(string $createdDatetime): self + { + $this->options['created_datetime'] = $createdDatetime; + + return $this; + } + + public function setDataCoding(string $dataCoding): self + { + $this->options['data_coding'] = $dataCoding; + + return $this; + } + + public function setFrom(string $from): self + { + $this->options['from'] = $from; + + return $this; + } + + public function setGateway(int $gateway): self + { + $this->options['gateway'] = $gateway; + + return $this; + } + + public function setGroupIds(array $groupIds): self + { + $this->options['group_ids'] = $groupIds; + + return $this; + } + + public function setMClass(int $mClass): self + { + $this->options['m_class'] = $mClass; + + return $this; + } + + public function setRecipientId(string $id): self + { + $this->options['recipient_id'] = $id; + + return $this; + } + + public function setReference(string $reference): self + { + $this->options['reference'] = $reference; + + return $this; + } + + public function setReportUrl(string $reportUrl): self + { + $this->options['report_url'] = $reportUrl; + + return $this; + } + + public function setScheduledDatetime(string $scheduledDatetime): self + { + $this->options['scheduled_datetime'] = $scheduledDatetime; + + return $this; + } + + public function setShortenUrls(bool $shortenUrls): self + { + $this->options['shorten_urls'] = $shortenUrls; + + return $this; + } + + public function setType(string $type): self + { + $this->options['type'] = $type; + + return $this; + } + + public function setTypeDetails(string $typeDetails): self + { + $this->options['type_details'] = $typeDetails; + + return $this; + } + + public function setValidity(int $validity): self + { + $this->options['validity'] = $validity; + + 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/MessageBird/MessageBirdTransport.php b/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdTransport.php index fbb74cb9eb7cf..84a39bc771a84 100644 --- a/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdTransport.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 MessageBirdOptions); } protected function doSend(MessageInterface $message): SentMessage @@ -57,14 +57,56 @@ protected function doSend(MessageInterface $message): SentMessage $from = $message->getFrom() ?: $this->from; + $opts = $message->getOptions(); + $options = $opts ? $opts->toArray() : []; + $options['originator'] = $options['from'] ?? $from; + $options['recipients'] = [$message->getPhone()]; + $options['body'] = $message->getSubject(); + + if (isset($options['group_ids'])) { + $options['groupIds'] = $options['group_ids']; + unset($options['group_ids']); + } + + if (isset($options['report_url'])) { + $options['reportUrl'] = $options['report_url']; + unset($options['report_url']); + } + + if (isset($options['type_details'])) { + $options['typeDetails'] = $options['type_details']; + unset($options['type_details']); + } + + if (isset($options['data_coding'])) { + $options['datacoding'] = $options['data_coding']; + unset($options['data_coding']); + } + + if (isset($options['m_class'])) { + $options['mclass'] = $options['m_class']; + unset($options['m_class']); + } + + if (isset($options['shorten_urls'])) { + $options['shortenUrls'] = $options['shorten_urls']; + unset($options['shorten_urls']); + } + + if (isset($options['scheduled_datetime'])) { + $options['scheduledDatetime'] = $options['scheduled_datetime']; + unset($options['scheduled_datetime']); + } + + if (isset($options['created_datetime'])) { + $options['createdDatetime'] = $options['created_datetime']; + unset($options['created_datetime']); + } + $endpoint = sprintf('https://%s/messages', $this->getEndpoint()); $response = $this->client->request('POST', $endpoint, [ 'auth_basic' => 'AccessKey:'.$this->token, - 'body' => [ - 'originator' => $from, - 'recipients' => $message->getPhone(), - 'body' => $message->getSubject(), - ], + 'body' => array_filter($options), ]); try { diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdOptionsTest.php new file mode 100644 index 0000000000000..ab74f3220c992 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdOptionsTest.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\MessageBird\Tests; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Notifier\Bridge\MessageBird\MessageBirdOptions; + +class MessageBirdOptionsTest extends TestCase +{ + public function testMessageBirdOptions() + { + $messageBirdOptions = (new MessageBirdOptions())->setFrom('test_from')->setType('test_type')->setScheduledDatetime('test_scheduled_datetime')->setCreatedDatetime('test_created_datetime')->setRecipientId('test_recipient')->setDataCoding('test_data_coding')->setGateway(999)->setGroupIds(['test_group_ids'])->setMClass(888)->setReference('test_reference')->setReportUrl('test_report_url')->setShortenUrls(true)->setTypeDetails('test_type_details')->setValidity(777); + + self::assertSame([ + 'from' => 'test_from', + 'type' => 'test_type', + 'scheduled_datetime' => 'test_scheduled_datetime', + 'created_datetime' => 'test_created_datetime', + 'data_coding' => 'test_data_coding', + 'gateway' => 999, + 'group_ids' => ['test_group_ids'], + 'm_class' => 888, + 'reference' => 'test_reference', + 'report_url' => 'test_report_url', + 'shorten_urls' => true, + 'type_details' => 'test_type_details', + 'validity' => 777, + ], $messageBirdOptions->toArray()); + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdTransportTest.php b/src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdTransportTest.php index 3d638d493bb8e..8aad64c83d4a0 100644 --- a/src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdTransportTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Notifier\Bridge\MessageBird\Tests; use Symfony\Component\HttpClient\MockHttpClient; +use Symfony\Component\Notifier\Bridge\MessageBird\MessageBirdOptions; use Symfony\Component\Notifier\Bridge\MessageBird\MessageBirdTransport; 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('0611223344', 'Hello!')]; + yield [new SmsMessage('0611223344', 'Hello!', 'from', new MessageBirdOptions(['from' => 'foo']))]; } public static function unsupportedMessagesProvider(): iterable