Skip to content

Commit

Permalink
feature #48575 [Notifier] Add SMS options to Esendex notifier (gnito-…
Browse files Browse the repository at this point in the history
…org)

This PR was merged into the 6.3 branch.

Discussion
----------

[Notifier] Add SMS options to Esendex notifier

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       |
| License       | MIT
| Doc PR        | N/A
<!--
Replace this notice by a short README for your feature/bugfix.
This will help reviewers and should be a good start for the documentation.

Additionally (see https://symfony.com/releases):
 - Always add tests and ensure they pass.
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against the latest branch.
 - For new features, provide some code snippets to help understand usage.
 - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry
 - Never break backward compatibility (see https://symfony.com/bc).
-->

Commits
-------

c25cea767c [Notifier] Add SMS options to Esendex notifier
  • Loading branch information
nicolas-grekas committed May 12, 2023
2 parents 564c785 + 3adb4c6 commit 87b5c1f
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 12 deletions.
5 changes: 5 additions & 0 deletions 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
73 changes: 73 additions & 0 deletions EsendexOptions.php
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 <[email protected]>
*
* 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;
}
}
22 changes: 10 additions & 12 deletions EsendexTransport.php
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
28 changes: 28 additions & 0 deletions Tests/EsendexOptionsTest.php
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 <[email protected]>
*
* 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());
}
}
2 changes: 2 additions & 0 deletions Tests/EsendexTransportTest.php
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 static function toStringProvider(): iterable
public static function supportedMessagesProvider(): iterable
{
yield [new SmsMessage('0611223344', 'Hello!')];
yield [new SmsMessage('0611223344', 'Hello!', 'from', new EsendexOptions(['from' => 'foo']))];
}

public static function unsupportedMessagesProvider(): iterable
Expand Down

0 comments on commit 87b5c1f

Please sign in to comment.