8000 feature #48584 [Notifier] Add SMS options to ContactEveryone notifier… · symfony/symfony@912aca4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 912aca4

Browse files
feature #48584 [Notifier] Add SMS options to ContactEveryone notifier (gnito-org)
This PR was merged into the 6.3 branch. Discussion ---------- [Notifier] Add SMS options to ContactEveryone 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 8000 .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 ------- 9fd8dfd [Notifier] Ass SMS options to ContactEveryone notifier
2 parents 7aa40b2 + 9fd8dfd commit 912aca4

File tree

5 files changed

+131
-8
lines changed

5 files changed

+131
-8
lines changed
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
CHANGELOG
22
=========
33

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

712
* Add the bridge
8-
* Throw exception when `SmsMessage->from` defined
13+
* Throw exception when `SmsMessage->from` defined
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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\ContactEveryone;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author gnito-org <https://github.com/gnito-org>
18+
*/
19+
final class ContactEveryoneOptions implements MessageOptionsInterface
20+
{
21+
private array $options;
22+
23+
public function __construct(array $options = [])
24+
{
25+
$this->options = $options;
26+
}
27+
28+
public function getDiffusionName(): ?int
29+
{
30+
return $this->options['diffusion_name'] ?? null;
31+
}
32+
33+
public function getCategory(): ?string
34+
{
35+
return $this->options['category'] ?? null;
36+
}
37+
38+
public function getFrom(): ?string
39+
{
40+
return $this->options['from'] ?? null;
41+
}
42+
43+
public function getRecipientId(): ?string
44+
{
45+
return $this->options['recipient_id'] ?? null;
46+
}
47+
48+
public function setDiffusionName(int $diffusionName): self
49+
{
50+
$this->options['diffusion_name'] = $diffusionName;
51+
52+
return $this;
53+
}
54+
55+
public function setCategory(string $category): self
56+
{
57+
$this->options['category'] = $category;
58+
59+
return $this;
60+
}
61+
62+
public function setFrom(string $from): self
63+
{
64+
$this->options['from'] = $from;
65+
66+
return $this;
67+
}
68+
69+
public function setRecipientId(string $id): self
70+
{
71+
$this->options['recipient_id'] = $id;
72+
73+
return $this;
74+
}
75+
76+
public function toArray(): array
77+
{
78+
$options = $this->options;
79+
if (isset($options['recipient_id'])) {
80+
unset($options['recipient_id']);
81+
}
82+
83+
return $options;
84+
}
85+
}

src/Symfony/Component/Notifier/Bridge/ContactEveryone/ContactEveryoneTransport.php

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

6060
public function supports(MessageInterface $message): bool
6161
{
62-
return $message instanceof SmsMessage;
62+
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof ContactEveryoneOptions);
6363
}
6464

6565
protected function doSend(MessageInterface $message): SentMessage
@@ -72,14 +72,16 @@ protected function doSend(MessageInterface $message): SentMessage
7272
throw new InvalidArgumentException(sprintf('The "%s" transport does not support "from" in "%s".', __CLASS__, SmsMessage::class));
7373
}
7474

75+
$opts = $message->getOptions();
76+
$options = $opts ? $opts->toArray() : [];
77+
$options['xcharset'] = 'true';
78+
$options['token'] = $this->token;
79+
$options['to'] = $message->getPhone();
80+
$options['msg'] = $message->getSubject();
81+
7582
$endpoint = sprintf('https://%s/api/light/diffusions/sms', self::HOST);
7683
$response = $this->client->request('POST', $endpoint, [
77-
'query' => [
78-
'xcharset' => 'true',
79-
'token' => $this->token,
80-
'to' => $message->getPhone(),
81-
'msg' => $message->getSubject(),
82-
],
84+
'query' => array_filter($options),
8385
]);
8486

8587
try {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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\ContactEveryone\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneOptions;
16+
17+
class ContactEveryoneOptionsTest extends TestCase
18+
{
19+
public function testContactEveryoneOptions()
20+
{
21+
$contactEveryoneOptions = (new ContactEveryoneOptions())->setFrom('test_from')->setCategory('test_category')->setDiffusionName('test_diffusion_name')->setRecipientId('test_recipient');
22+
23+
self::assertSame([
24+
'from' => 'test_from',
25+
'category' => 'test_category',
26+
'diffusion_name' => 'test_diffusion_name',
27+
], $contactEveryoneOptions->toArray());
28+
}
29+
}

src/Symfony/Component/Notifier/Bridge/ContactEveryone/Tests/ContactEveryoneTransportTest.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\ContactEveryone\Tests;
1313

1414
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneOptions;
1516
use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneTransport;
1617
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
1718
use Symfony\Component\Notifier\Message\ChatMessage;
@@ -36,6 +37,7 @@ public static function toStringProvider(): iterable
3637
public static function supportedMessagesProvider(): iterable
3738
{
3839
yield [new SmsMessage('0611223344', 'Hello!')];
40+
yield [new SmsMessage('0611223344', 'Hello!', 'from', new ContactEveryoneOptions(['from' => 'foo']))];
3941
}
4042

4143
public static function unsupportedMessagesProvider(): iterable

0 commit comments

Comments
 (0)
0