diff --git a/src/Symfony/Component/Notifier/Bridge/Clickatell/CHANGELOG.md b/src/Symfony/Component/Notifier/Bridge/Clickatell/CHANGELOG.md index 5092c668a1f20..ff34c9a19a880 100644 --- a/src/Symfony/Component/Notifier/Bridge/Clickatell/CHANGELOG.md +++ b/src/Symfony/Component/Notifier/Bridge/Clickatell/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.3 +--- + + * Add `ClickatellOptions` class + 6.2 --- diff --git a/src/Symfony/Component/Notifier/Bridge/Clickatell/ClickatellOptions.php b/src/Symfony/Component/Notifier/Bridge/Clickatell/ClickatellOptions.php new file mode 100644 index 0000000000000..d4af576bcd19f --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Clickatell/ClickatellOptions.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\Clickatell; + +use Symfony\Component\Notifier\Message\MessageOptionsInterface; + +/** + * @author gnito-org + */ +final class ClickatellOptions 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/Clickatell/ClickatellTransport.php b/src/Symfony/Component/Notifier/Bridge/Clickatell/ClickatellTransport.php index b9861517546f4..d52e54c4a3e72 100644 --- a/src/Symfony/Component/Notifier/Bridge/Clickatell/ClickatellTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Clickatell/ClickatellTransport.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 ClickatellOptions); } protected function doSend(MessageInterface $message): SentMessage @@ -61,7 +61,13 @@ protected function doSend(MessageInterface $message): SentMessage $endpoint = sprintf('https://%s/rest/message', $this->getEndpoint()); - $from = $message->getFrom() ?: $this->from; + $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(); $response = $this->client->request('POST', $endpoint, [ 'headers' => [ @@ -70,11 +76,7 @@ protected function doSend(MessageInterface $message): SentMessage 'Content-Type' => 'application/json', 'X-Version' => 1, ], - 'json' => [ - 'from' => $from ?? '', - 'to' => [$message->getPhone()], - 'text' => $message->getSubject(), - ], + 'json' => array_filter($options), ]); try { diff --git a/src/Symfony/Component/Notifier/Bridge/Clickatell/Tests/ClickatellOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/Clickatell/Tests/ClickatellOptionsTest.php new file mode 100644 index 0000000000000..7b84423df7604 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Clickatell/Tests/ClickatellOptionsTest.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\Clickatell\Tests; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellOptions; + +class ClickatellOptionsTest extends TestCase +{ + public function testClickatellOptions() + { + $clickatellOptions = (new ClickatellOptions())->setFrom('test_from')->setRecipientId('test_recipient'); + + self::assertSame(['from' => 'test_from'], $clickatellOptions->toArray()); + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/Clickatell/Tests/ClickatellTransportTest.php b/src/Symfony/Component/Notifier/Bridge/Clickatell/Tests/ClickatellTransportTest.php index 7d3553b3ac85d..8a9ac297567b0 100644 --- a/src/Symfony/Component/Notifier/Bridge/Clickatell/Tests/ClickatellTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Clickatell/Tests/ClickatellTransportTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Notifier\Bridge\Clickatell\Tests; use Symfony\Component\HttpClient\MockHttpClient; +use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellOptions; use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransport; use Symfony\Component\Notifier\Exception\LogicException; use Symfony\Component\Notifier\Exception\TransportException; @@ -38,6 +39,7 @@ public function toStringProvider(): iterable public function supportedMessagesProvider(): iterable { yield [new SmsMessage('+33612345678', 'Hello!')]; + yield [new SmsMessage('+33612345678', 'Hello!', 'from', new ClickatellOptions(['from' => 'foo']))]; } public function unsupportedMessagesProvider(): iterable