Skip to content

[Notifier] Added MessageBirdOptions to the message-bird-notifier bridge #42265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.4
---

* Add `MessageBirdOptions`

5.3
---

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <evertjan@hakvoort.io>
*
* @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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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']);
}
}