diff --git a/src/Symfony/Component/Notifier/Bridge/AllMySms/AllMySmsOptions.php b/src/Symfony/Component/Notifier/Bridge/AllMySms/AllMySmsOptions.php new file mode 100644 index 0000000000000..72d71d57879df --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/AllMySms/AllMySmsOptions.php @@ -0,0 +1,145 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\AllMySms; + +use Symfony\Component\Notifier\Message\MessageOptionsInterface; + +/** + * @author gnito-org + */ +final class AllMySmsOptions implements MessageOptionsInterface +{ + private array $options; + + public function __construct(array $options = []) + { + $this->options = $options; + } + + public function getAlerting(): ?int + { + return $this->options['alerting'] ?? null; + } + + public function getCampaignName(): ?string + { + return $this->options['campaign_name'] ?? null; + } + + public function getCliMsgId(): ?string + { + return $this->options['cli_msg_id'] ?? null; + } + + public function getDate(): ?string + { + return $this->options['date'] ?? null; + } + + public function getFrom(): ?string + { + return $this->options['from'] ?? null; + } + + public function getRecipientId(): ?string + { + return $this->options['recipient_id'] ?? null; + } + + public function getSimulate(): ?int + { + return $this->options['simulate'] ?? null; + } + + public function getUniqueIdentifier(): ?string + { + return $this->options['unique_identifier'] ?? null; + } + + public function getVerbose(): ?int + { + return $this->options['verbose'] ?? null; + } + + public function setAlerting(int $alerting): self + { + $this->options['alerting'] = $alerting; + + return $this; + } + + public function setCampaignName(string $campaignName): self + { + $this->options['campaign_name'] = $campaignName; + + return $this; + } + + public function setCliMsgId(string $cliMsgId): self + { + $this->options['cli_msg_id'] = $cliMsgId; + + return $this; + } + + public function setDate(string $date): self + { + $this->options['date'] = $date; + + 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 setSimulate(int $simulate): self + { + $this->options['simulate'] = $simulate; + + return $this; + } + + public function setUniqueIdentifier(string $uniqueIdentifier): self + { + $this->options['unique_identifier'] = $uniqueIdentifier; + + return $this; + } + + public function setVerbose(int $verbose): self + { + $this->options['verbose'] = $verbose; + + 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/AllMySms/AllMySmsTransport.php b/src/Symfony/Component/Notifier/Bridge/AllMySms/AllMySmsTransport.php index 65353d68bc7bd..63c3fc7906d5e 100644 --- a/src/Symfony/Component/Notifier/Bridge/AllMySms/AllMySmsTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/AllMySms/AllMySmsTransport.php @@ -52,7 +52,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 AllMySmsOptions); } protected function doSend(MessageInterface $message): SentMessage @@ -63,14 +63,31 @@ protected function doSend(MessageInterface $message): SentMessage $from = $message->getFrom() ?: $this->from; + $opts = $message->getOptions(); + $options = $opts ? $opts->toArray() : []; + $options['from'] = $options['from'] ?? $from; + $options['to'] = $message->getPhone(); + $options['text'] = $message->getSubject(); + + if (isset($options['campaign_name'])) { + $options['campaignName'] = $options['campaign_name']; + unset($options['campaign_name']); + } + + if (isset($options['cli_msg_id'])) { + $options['cliMsgId'] = $options['cli_msg_id']; + unset($options['cli_msg_id']); + } + + if (isset($options['unique_identifier'])) { + $options['uniqueIdentifier'] = $options['unique_identifier']; + unset($options['unique_identifier']); + } + $endpoint = sprintf('https://%s/sms/send/', $this->getEndpoint()); $response = $this->client->request('POST', $endpoint, [ 'auth_basic' => $this->login.':'.$this->apiKey, - 'json' => [ - 'from' => $from, - 'to' => $message->getPhone(), - 'text' => $message->getSubject(), - ], + 'json' => array_filter($options), ]); try { diff --git a/src/Symfony/Component/Notifier/Bridge/AllMySms/CHANGELOG.md b/src/Symfony/Component/Notifier/Bridge/AllMySms/CHANGELOG.md index 5092c668a1f20..a076263236d12 100644 --- a/src/Symfony/Component/Notifier/Bridge/AllMySms/CHANGELOG.md +++ b/src/Symfony/Component/Notifier/Bridge/AllMySms/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.3 +--- + + * Use `AllMySmsOptions` class + 6.2 --- diff --git a/src/Symfony/Component/Notifier/Bridge/AllMySms/Tests/AllMySmsOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/AllMySms/Tests/AllMySmsOptionsTest.php new file mode 100644 index 0000000000000..4bef488eb6bc5 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/AllMySms/Tests/AllMySmsOptionsTest.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\AllMySms\Tests; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsOptions; + +class AllMySmsOptionsTest extends TestCase +{ + public function testAllMySmsOptions() + { + $allMySmsOptions = (new AllMySmsOptions())->setFrom('test_from')->setAlerting(1)->setDate('test_date')->setCampaignName('test_campaign_name')->setRecipientId('test_recipient')->setCliMsgId('test_cli_msg_id')->setSimulate(1)->setUniqueIdentifier('test_unique_identifier')->setVerbose(1); + + self::assertSame([ + 'from' => 'test_from', + 'alerting' => 1, + 'date' => 'test_date', + 'campaign_name' => 'test_campaign_name', + 'cli_msg_id' => 'test_cli_msg_id', + 'simulate' => 1, + 'unique_identifier' => 'test_unique_identifier', + 'verbose' => 1, + ], $allMySmsOptions->toArray()); + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/AllMySms/Tests/AllMySmsTransportTest.php b/src/Symfony/Component/Notifier/Bridge/AllMySms/Tests/AllMySmsTransportTest.php index 314fdeb2f9442..3ca13b792e900 100644 --- a/src/Symfony/Component/Notifier/Bridge/AllMySms/Tests/AllMySmsTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/AllMySms/Tests/AllMySmsTransportTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Notifier\Bridge\AllMySms\Tests; use Symfony\Component\HttpClient\MockHttpClient; +use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsOptions; use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransport; use Symfony\Component\Notifier\Message\ChatMessage; use Symfony\Component\Notifier\Message\SmsMessage; @@ -35,6 +36,7 @@ public static function toStringProvider(): iterable public static function supportedMessagesProvider(): iterable { yield [new SmsMessage('0611223344', 'Hello!')]; + yield [new SmsMessage('0611223344', 'Hello!', 'from', new AllMySmsOptions(['from' => 'foo']))]; } public static function unsupportedMessagesProvider(): iterable