diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/CHANGELOG.md b/src/Symfony/Component/Notifier/Bridge/MessageBird/CHANGELOG.md index 1f2b652ac20ea..ca59e807d5bf8 100644 --- a/src/Symfony/Component/Notifier/Bridge/MessageBird/CHANGELOG.md +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +5.4 +--- + + * Add `MessageBirdOptions` + 5.3 --- 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..724622e7352ab --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdOptions.php @@ -0,0 +1,59 @@ + + * + * 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 Evert Jan Hakvoort + * + * @see https://developers.messagebird.com/api/sms-messaging/#sms-api + */ +final class MessageBirdOptions implements MessageOptionsInterface +{ + private $options; + + public function __construct(array $options = []) + { + $this->options = $options; + } + + public function toArray(): array + { + return $this->options; + } + + public function getRecipientId(): ?string + { + return $this->options['recipients'] ?? null; + } + + /** + * @return $this + */ + public function validity(int $validity): self + { + $this->options['validity'] = $validity; + + return $this; + } + + /** + * @return $this + */ + public function reference(string $reference): self + { + $this->options['reference'] = $reference; + + return $this; + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdTransport.php b/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdTransport.php index cb56f6a843c0c..2d6716a0706a8 100644 --- a/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdTransport.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Notifier\Bridge\MessageBird; +use Symfony\Component\Notifier\Exception\LogicException; use Symfony\Component\Notifier\Exception\TransportException; use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; use Symfony\Component\Notifier\Message\MessageInterface; @@ -55,14 +56,19 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } + $options = $message->getOptions(); + if (null !== $options && !$options instanceof MessageBirdOptions) { + throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, MessageBirdOptions::class)); + } + $endpoint = sprintf('https://%s/messages', $this->getEndpoint()); $response = $this->client->request('POST', $endpoint, [ 'auth_basic' => 'AccessKey:'.$this->token, - 'body' => [ + 'body' => array_merge([ 'originator' => $this->from, 'recipients' => $message->getPhone(), 'body' => $message->getSubject(), - ], + ], null !== $options ? $options->toArray() : []), ]); 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..db9965e088c89 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdOptionsTest.php @@ -0,0 +1,50 @@ + + * + * 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; + +final class MessageBirdOptionsTest extends TestCase +{ + public function testGetRecipientIdWhenSet() + { + $messagebirdOptions = new MessageBirdOptions([ + 'recipients' => 'foo', + ]); + + $this->assertSame('foo', $messagebirdOptions->getRecipientId()); + } + + public function testGetRecipientIdWhenNotSet() + { + $this->assertNull((new MessageBirdOptions())->getRecipientId()); + } + + public function testSetValidity() + { + $messagebirdOptions = new MessageBirdOptions(); + + $messagebirdOptions->validity(500); + + $this->assertSame(500, $messagebirdOptions->toArray()['validity']); + } + + public function testSetReference() + { + $messagebirdOptions = new MessageBirdOptions(); + + $messagebirdOptions->reference('foo'); + + $this->assertSame('foo', $messagebirdOptions->toArray()['reference']); + } +}