Skip to content

[Notifier] Added ability to initialize sms options #39774

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
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
16 changes: 14 additions & 2 deletions src/Symfony/Component/Notifier/Message/SmsMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ final class SmsMessage implements MessageInterface
private $transport;
private $subject;
private $phone;
private $options;

public function __construct(string $phone, string $subject)
public function __construct(string $phone, string $subject, MessageOptionsInterface $options = null)
{
if ('' === $phone) {
throw new InvalidArgumentException(sprintf('"%s" needs a phone number, it cannot be empty.', static::class));
}

$this->subject = $subject;
$this->phone = $phone;
$this->options = $options;
}

public static function fromNotification(Notification $notification, SmsRecipientInterface $recipient): self
Expand Down Expand Up @@ -93,8 +95,18 @@ public function getTransport(): ?string
return $this->transport;
}

/**
* @return $this
*/
public function options(MessageOptionsInterface $options): self
{
$this->options = $options;

return $this;
}

public function getOptions(): ?MessageOptionsInterface
{
return null;
return $this->options;
}
}
27 changes: 27 additions & 0 deletions src/Symfony/Component/Notifier/Tests/Message/SmsMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Notifier\Tests\Message;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
use Symfony\Component\Notifier\Message\SmsMessage;

/**
Expand Down Expand Up @@ -57,4 +58,30 @@ public function testEnsureNonEmptyPhoneOnSet()

$message->phone('');
}

public function testOptionsDefaultsToNull()
{
$message = new SmsMessage('+3715673920', 'subject');

$this->assertNull($message->getOptions());
}

public function testSetOptions()
{
$options = new class() implements MessageOptionsInterface {
public function toArray(): array
{
return [];
}

public function getRecipientId(): ?string
{
return 'id';
}
};

$message = new SmsMessage('+3715673920', 'subject', $options);

$this->assertInstanceOf(MessageOptionsInterface::class, $message->getOptions());
}
}