Skip to content

[Notifier] Add SMS options to MessageMedia notifier #48586

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 `MessageMediaOptions` class

6.2
---

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?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\MessageMedia;

use Symfony\Component\Notifier\Message\MessageOptionsInterface;

/**
* @author gnito-org <https://github.com/gnito-org>
*/
final class MessageMediaOptions implements MessageOptionsInterface
{
private array $options;

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

public function getCallbackUrl(): ?string
Copy link
Member

@nicolas-grekas nicolas-grekas Dec 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we remove all getters? I don't think it's worth exposing them. toArray() works fine for the purpose.
Can you consider this for all your PRs please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing all the getters is inconsistent with the MessageOptionsInterface.

{
return $this->options['callback_url'] ?? null;
}

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

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

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

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

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

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

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

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

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

public function setCallbackUrl(string $callbackUrl): self
{
$this->options['callback_url'] = $callbackUrl;

return $this;
}

public function setDeliveryReport(bool $deliveryReport): self
{
$this->options['delivery_report'] = $deliveryReport;

return $this;
}

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

return $this;
}

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

return $this;
}

public function setMedia(array $media): self
{
$this->options['media'] = $media;

return $this;
}

public function setMessageExpiryTimestamp(int $messageExpiryTimestamp): self
{
$this->options['message_expiry_timestamp'] = $messageExpiryTimestamp;

return $this;
}

public function setMetadata(array $metadata): self
{
$this->options['metadata'] = $metadata;

return $this;
}

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

return $this;
}

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

return $this;
}

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

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 @@ -53,7 +53,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 MessageMediaOptions);
}

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

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

$opts = $message->getOptions();
$options = $opts ? $opts->toArray() : [];
$options['source_number'] = $options['from'] ?? $from;
$options['destination_number'] = $message->getPhone();
$options['content'] = $message->getSubject();

unset($options['from']);

$endpoint = sprintf('https://%s/v1/messages', $this->getEndpoint());
$response = $this->client->request(
'POST',
Expand All @@ -72,11 +80,7 @@ protected function doSend(MessageInterface $message): SentMessage
'auth_basic' => $this->apiKey.':'.$this->apiSecret,
'json' => [
'messages' => [
[
'destination_number' => $message->getPhone(),
'source_number' => $from,
'content' => $message->getSubject(),
],
array_filter($options),
],
],
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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\MessageMedia\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\MessageMedia\MessageMediaOptions;

class MessageMediaOptionsTest extends TestCase
{
public function testMessageMediaOptions()
{
$messageMediaOptions = (new MessageMediaOptions())->setFrom('test_from')->setMedia(['test_media'])->setCallbackUrl('test_callback_url')->setFormat('test_format')->setRecipientId('test_recipient')->setDeliveryReport(true)->setMessageExpiryTimestamp(999)->setMetadata(['test_metadata'])->setScheduled('test_scheduled')->setSubject('test_subject');

self::assertSame([
'from' => 'test_from',
'media' => ['test_media'],
'callback_url' => 'test_callback_url',
'format' => 'test_format',
'delivery_report' => true,
'message_expiry_timestamp' => 999,
'metadata' => ['test_metadata'],
'scheduled' => 'test_scheduled',
'subject' => 'test_subject',
], $messageMediaOptions->toArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Notifier\Bridge\MessageMedia\Tests;

use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Notifier\Bridge\MessageMedia\MessageMediaOptions;
use Symfony\Component\Notifier\Bridge\MessageMedia\MessageMediaTransport;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\TransportExceptionInterface;
Expand All @@ -38,6 +39,7 @@ public function toStringProvider(): iterable
public function supportedMessagesProvider(): iterable
{
yield [new SmsMessage('0491570156', 'Hello!')];
yield [new SmsMessage('0491570156', 'Hello!', 'from', new MessageMediaOptions(['from' => 'foo']))];
}

public function unsupportedMessagesProvider(): iterable
Expand Down