8000 feature #48573 [Notifier] Add SMS options to Clickatell notifier (gni… · symfony/symfony@35a4b64 · GitHub
[go: up one dir, main page]

Skip to content

Commit 35a4b64

Browse files
feature #48573 [Notifier] Add SMS options to Clickatell notifier (gnito-org)
This PR was merged into the 6.3 branch. Discussion ---------- [Notifier] Add SMS options to Clickatell 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 ------- 1dc0bc4 [Notifier] Add SMS options to Clickatell notifier
2 parents c7247f3 + 1dc0bc4 commit 35a4b64

File tree

5 files changed

+102
-7
lines changed

5 files changed

+102
-7
lines changed

src/Symfony/Component/Notifier/Bridge/Clickatell/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Add `ClickatellOptions` class
8+
49
6.2
510
---
611

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Clickatell;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author gnito-org <https://github.com/gnito-org>
18+
*/
19+
final class ClickatellOptions implements MessageOptionsInterface
20+
{
21+
private array $options;
22+
23+
public function __construct(array $options = [])
24+
{
25+
$this->options = $options;
26+
}
27+
28+
public function getFrom(): ?string
29+
{
30+
return $this->options['from'] ?? null;
31+
}
32+
33+
public function getRecipientId(): ?string
34+
{
35+
return $this->options['recipient_id'] ?? null;
36+
}
37+
38+
public function setFrom(string $from): self
39+
{
40+
$this->options['from'] = $from;
41+
42+
return $this;
43+
}
44+
45+
public function setRecipientId(string $id): self
46+
{
47+
$this->options['recipient_id'] = $id;
48+
49+
return $this;
50+
}
51+
52+
public function toArray(): array
53+
{
54+
$options = $this->options;
55+
if (isset($options['recipient_id'])) {
56+
unset($options['recipient_id']);
57+
}
58+
59+
return $options;
60+
}
61+
}

src/Symfony/Component/Notifier/Bridge/Clickatell/ClickatellTransport.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __toString(): string
5050

5151
public function supports(MessageInterface $message): bool
5252
{
53-
return $message instanceof SmsMessage;
53+
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof ClickatellOptions);
5454
}
5555

5656
protected function doSend(MessageInterface $message): SentMessage
@@ -61,7 +61,13 @@ protected function doSend(MessageInterface $message): SentMessage
6161

6262
$endpoint = sprintf('https://%s/rest/message', $this->getEndpoint());
6363

64-
$from = $message->getFrom() ?: $this->from;
64+
$from = $message->getFrom() ?: $this->from ?: '';
65+
66+
$opts = $message->getOptions();
67+
$options = $opts ? $opts->toArray() : [];
68+
$options['from'] = $options['from'] ?? $from;
69+
$options['to'] = $message->getPhone();
70+
$options['text'] = $message->getSubject();
6571

6672
$response = $this->client->request('POST', $endpoint, [
6773
'headers' => [
@@ -70,11 +76,7 @@ protected function doSend(MessageInterface $message): SentMessage
7076
'Content-Type' => 'application/json',
7177
'X-Version' => 1,
7278
],
73-
'json' => [
74-
'from' => $from ?? '',
75-
'to' => [$message->getPhone()],
76-
'text' => $message->getSubject(),
77-
],
79+
'json' => array_filter($options),
7880
]);
7981

8082
try {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Clickatell\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellOptions;
16+
17+
class ClickatellOptionsTest extends TestCase
18+
{
19+
public function testClickatellOptions()
20+
{
21+
$clickatellOptions = (new ClickatellOptions())->setFrom('test_from')->setRecipientId('test_recipient');
22+
23+
self::assertSame(['from' => 'test_from'], $clickatellOptions->toArray());
24+
}
25+
}

src/Symfony/Component/Notifier/Bridge/Clickatell/Tests/ClickatellTransportTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Notifier\Bridge\Clickatell\Tests;
1313

1414
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellOptions;
1516
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransport;
1617
use Symfony\Component\Notifier\Exception\LogicException;
1718
use Symfony\Component\Notifier\Exception\TransportException;
@@ -39,6 +40,7 @@ public static function toStringProvider(): iterable
3940
public static function supportedMessagesProvider(): iterable
4041
{
4142
yield [new SmsMessage('+33612345678', 'Hello!')];
43+
yield [new SmsMessage('+33612345678', 'Hello!', 'from', new ClickatellOptions(['from' => 'foo']))];
4244
}
4345

4446
public static function unsupportedMessagesProvider(): iterable

0 commit comments

Comments
 (0)
0