Skip to content

[Notifier] Add SMS options to MessageBird notifier #48585

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

Merged
merged 1 commit into from
May 12, 2023
Merged
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
=========

6.3
---

* Add `MessageBirdOptions` class

6.2
---

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
<?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 gnito-org <https://github.com/gnito-org>
*/
final class MessageBirdOptions implements MessageOptionsInterface
{
private array $options;

public function __construct(array $options = [])
{
$this->options = $options;
}

public function getCreatedDatetime(): ?string
{
return $this->options['created_datetime'] ?? null;
}

public function getDataCoding(): ?string
{
return $this->options['data_coding'] ?? null;
}

public function getFrom(): ?string
{
return $this->options['from'] ?? null;
}

public function getGateway(): ?int
{
return $this->options['gateway'] ?? null;
}

public function getGroupIds(): ?array
{
return $this->options['group_ids'] ?? null;
}

public function getMClass(): ?int
{
return $this->options['m_class'] ?? null;
}

public function getRecipientId(): ?string
{
return $this->options['recipient_id'] ?? null;
}

public function getReference(): ?string
{
return $this->options['reference'] ?? null;
}

public function getReportUrl(): ?string
{
return $this->options['report_url'] ?? null;
}

public function getScheduledDatetime(): ?string
{
return $this->options['scheduled_datetime'] ?? null;
}

public function getShortenUrls(): ?bool
{
return $this->options['shorten_urls'] ?? null;
}

public function getType(): ?string
{
return $this->options['type'] ?? null;
}

public function getTypeDetails(): ?string
{
return $this->options['type_details'] ?? null;
}

public function getValidity(): ?int
{
return $this->options['validity'] ?? null;
}

public function setCreatedDatetime(string $createdDatetime): self
{
$this->options['created_datetime'] = $createdDatetime;

return $this;
}

public function setDataCoding(string $dataCoding): self
{
$this->options['data_coding'] = $dataCoding;

return $this;
}

public function setFrom(string $from): self
{
$this->options['from'] = $from;

return $this;
}

public function setGateway(int $gateway): self
{
$this->options['gateway'] = $gateway;

return $this;
}

public function setGroupIds(array $groupIds): self
{
$this->options['group_ids'] = $groupIds;

return $this;
}

public function setMClass(int $mClass): self
{
$this->options['m_class'] = $mClass;

return $this;
}

public function setRecipientId(string $id): self
{
$this->options['recipient_id'] = $id;

return $this;
}

public function setReference(string $reference): self
{
$this->options['reference'] = $reference;

return $this;
}

public function setReportUrl(string $reportUrl): self
{
$this->options['report_url'] = $reportUrl;

return $this;
}

public function setScheduledDatetime(string $scheduledDatetime): self
{
$this->options['scheduled_datetime'] = $scheduledDatetime;

return $this;
}

public function setShortenUrls(bool $shortenUrls): self
{
$this->options['shorten_urls'] = $shortenUrls;

return $this;
}

public function setType(string $type): self
{
$this->options['type'] = $type;

return $this;
}

public function setTypeDetails(string $typeDetails): self
{
$this->options['type_details'] = $typeDetails;

return $this;
}

public function setValidity(int $validity): self
{
$this->options['validity'] = $validity;

return $this;
}

public function toArray(): array
{
$options = $this->options;
if (isset($options['recipient_id'])) {
unset($options['recipient_id']);
}

return $options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,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 MessageBirdOptions);
}

protected function doSend(MessageInterface $message): SentMessage
Expand All @@ -57,14 +57,56 @@ protected function doSend(MessageInterface $message): SentMessage

$from = $message->getFrom() ?: $this->from;

$opts = $message->getOptions();
$options = $opts ? $opts->toArray() : [];
$options['originator'] = $options['from'] ?? $from;
$options['recipients'] = [$message->getPhone()];
$options['body'] = $message->getSubject();

if (isset($options['group_ids'])) {
$options['groupIds'] = $options['group_ids'];
unset($options['group_ids']);
}

if (isset($options['report_url'])) {
$options['reportUrl'] = $options['report_url'];
unset($options['report_url']);
}

if (isset($options['type_details'])) {
$options['typeDetails'] = $options['type_details'];
unset($options['type_details']);
}

if (isset($options['data_coding'])) {
$options['datacoding'] = $options['data_coding'];
unset($options['data_coding']);
}

if (isset($options['m_class'])) {
$options['mclass'] = $options['m_class'];
unset($options['m_class']);
}

if (isset($options['shorten_urls'])) {
$options['shortenUrls'] = $options['shorten_urls'];
unset($options['shorten_urls']);
}

if (isset($options['scheduled_datetime'])) {
$options['scheduledDatetime'] = $options['scheduled_datetime'];
unset($options['scheduled_datetime']);
}

if (isset($options['created_datetime'])) {
$options['createdDatetime'] = $options['created_datetime'];
unset($options['created_datetime']);
}

$endpoint = sprintf('https://%s/messages', $this->getEndpoint());
$response = $this->client->request('POST', $endpoint, [
'auth_basic' => 'AccessKey:'.$this->token,
'body' => [
'originator' => $from,
'recipients' => $message->getPhone(),
'body' => $message->getSubject(),
],
'body' => array_filter($options),
]);

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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;

class MessageBirdOptionsTest extends TestCase
{
public function testMessageBirdOptions()
{
$messageBirdOptions = (new MessageBirdOptions())->setFrom('test_from')->setType('test_type')->setScheduledDatetime('test_scheduled_datetime')->setCreatedDatetime('test_created_datetime')->setRecipientId('test_recipient')->setDataCoding('test_data_coding')->setGateway(999)->setGroupIds(['test_group_ids'])->setMClass(888)->setReference('test_reference')->setReportUrl('test_report_url')->setShortenUrls(true)->setTypeDetails('test_type_details')->setValidity(777);

self::assertSame([
'from' => 'test_from',
'type' => 'test_type',
'scheduled_datetime' => 'test_scheduled_datetime',
'created_datetime' => 'test_created_datetime',
'data_coding' => 'test_data_coding',
'gateway' => 999,
'group_ids' => ['test_group_ids'],
'm_class' => 888,
'reference' => 'test_reference',
'report_url' => 'test_report_url',
'shorten_urls' => true,
'type_details' => 'test_type_details',
'validity' => 777,
], $messageBirdOptions->toArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Notifier\Bridge\MessageBird\Tests;

use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Notifier\Bridge\MessageBird\MessageBirdOptions;
use Symfony\Component\Notifier\Bridge\MessageBird\MessageBirdTransport;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
Expand All @@ -34,6 +35,7 @@ public static function toStringProvider(): iterable
public static function supportedMessagesProvider(): iterable
{
yield [new SmsMessage('0611223344', 'Hello!')];
yield [new SmsMessage('0611223344', 'Hello!', 'from', new MessageBirdOptions(['from' => 'foo']))];
}

public static function unsupportedMessagesProvider(): iterable
Expand Down