Skip to content

[Notifier] Add SMS options to Esendex notifier #48575

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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Esendex/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Add `EsendexOptions` class

6.2
---

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

use Symfony\Component\Notifier\Message\MessageOptionsInterface;

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

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

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

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

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

public function setAccountReference(string $accountReference): self
{
$this->options['account_reference'] = $accountReference;

return $this;
}

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

return $this;
}

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

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 @@ -49,7 +49,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 EsendexOptions);
}

protected function doSend(MessageInterface $message): SentMessage
Expand All @@ -58,26 +58,24 @@ protected function doSend(MessageInterface $message): SentMessage
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
}

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

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

if ('' !== $message->getFrom()) {
$messageData['from'] = $message->getFrom();
} elseif (null !== $this->from) {
$messageData['from'] = $this->from;
}
$options['accountreference'] = $options['account_reference'] ?? $this->accountReference;
unset($options['account_reference']);

$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/v1.0/messagedispatcher', [
'auth_basic' => sprintf('%s:%s', $this->email, $this->password),
'headers' => [
'Accept' => 'application/json',
],
'json' => [
'accountreference' => $this->accountReference,
'messages' => [$messageData],
],
'json' => array_filter($options),
]);

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

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Esendex\EsendexOptions;

class EsendexOptionsTest extends TestCase
{
public function testEsendexOptions()
{
$esendexOptions = (new EsendexOptions())->setFrom('test_from')->setAccountReference('test_account_reference')->setRecipientId('test_recipient');

self::assertSame([
'from' => 'test_from',
'account_reference' => 'test_account_reference',
], $esendexOptions->toArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Notifier\Bridge\Esendex\Tests;

use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Notifier\Bridge\Esendex\EsendexOptions;
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransport;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\ChatMessage;
Expand All @@ -36,6 +37,7 @@ public function toStringProvider(): iterable
public function supportedMessagesProvider(): iterable
{
yield [new SmsMessage('0611223344', 'Hello!')];
yield [new SmsMessage('0611223344', 'Hello!', 'from', new EsendexOptions(['from' => 'foo']))];
}

public function unsupportedMessagesProvider(): iterable
Expand Down