diff --git a/src/Symfony/Component/Notifier/Bridge/Esendex/CHANGELOG.md b/src/Symfony/Component/Notifier/Bridge/Esendex/CHANGELOG.md index 99c9f9287316f..33792585479a4 100644 --- a/src/Symfony/Component/Notifier/Bridge/Esendex/CHANGELOG.md +++ b/src/Symfony/Component/Notifier/Bridge/Esendex/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.3 +--- + + * Add `EsendexOptions` class + 6.2 --- diff --git a/src/Symfony/Component/Notifier/Bridge/Esendex/EsendexOptions.php b/src/Symfony/Component/Notifier/Bridge/Esendex/EsendexOptions.php new file mode 100644 index 0000000000000..8832a0117ec9d --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Esendex/EsendexOptions.php @@ -0,0 +1,73 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\Esendex; + +use Symfony\Component\Notifier\Message\MessageOptionsInterface; + +/** + * @author gnito-org + */ +final class EsendexOptions implements MessageOptionsInterface +{ + private array $options; + + public function __construct(array $options = []) + { + $this->options = $options; + } + + public function getAccountReference(): ?string + { + return $this->options['account_reference'] ?? null; + } + + public function getFrom(): ?string + { + return $this->options['from'] ?? null; + } + + public function getRecipientId(): ?string + { + return $this->options['recipient_id'] ?? null; + } + + public function setAccountReference(string $accountReference): self + { + $this->options['account_reference'] = $accountReference; + + 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/Esendex/EsendexTransport.php b/src/Symfony/Component/Notifier/Bridge/Esendex/EsendexTransport.php index 8f63e781dc435..66d7bbef27a60 100644 --- a/src/Symfony/Component/Notifier/Bridge/Esendex/EsendexTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Esendex/EsendexTransport.php @@ -49,7 +49,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 EsendexOptions); } protected function doSend(MessageInterface $message): SentMessage @@ -58,26 +58,24 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - $messageData = [ + $from = $message->getFrom() ?: $this->from; + + $opts = $message->getOptions(); + $options = $opts ? $opts->toArray() : []; + $options['from'] = $options['from'] ?? $from; + $options['messages'] = [ 'to' => $message->getPhone(), 'body' => $message->getSubject(), ]; - - if ('' !== $message->getFrom()) { - $messageData['from'] = $message->getFrom(); - } elseif (null !== $this->from) { - $messageData['from'] = $this->from; - } + $options['accountreference'] = $options['account_reference'] ?? $this->accountReference; + unset($options['account_reference']); $response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/v1.0/messagedispatcher', [ 'auth_basic' => sprintf('%s:%s', $this->email, $this->password), 'headers' => [ 'Accept' => 'application/json', ], - 'json' => [ - 'accountreference' => $this->accountReference, - 'messages' => [$messageData], - ], + 'json' => array_filter($options), ]); try { diff --git a/src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexOptionsTest.php new file mode 100644 index 0000000000000..cea33875e2c63 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexOptionsTest.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\Esendex\Tests; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Notifier\Bridge\Esendex\EsendexOptions; + +class EsendexOptionsTest extends TestCase +{ + public function testEsendexOptions() + { + $esendexOptions = (new EsendexOptions())->setFrom('test_from')->setAccountReference('test_account_reference')->setRecipientId('test_recipient'); + + self::assertSame([ + 'from' => 'test_from', + 'account_reference' => 'test_account_reference', + ], $esendexOptions->toArray()); + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexTransportTest.php b/src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexTransportTest.php index cde836ae55607..b1a11ea7f8f64 100644 --- a/src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexTransportTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Notifier\Bridge\Esendex\Tests; use Symfony\Component\HttpClient\MockHttpClient; +use Symfony\Component\Notifier\Bridge\Esendex\EsendexOptions; use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransport; use Symfony\Component\Notifier\Exception\TransportException; 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 EsendexOptions(['from' => 'foo']))]; } public function unsupportedMessagesProvider(): iterable