Skip to content

Commit 9fd8dfd

Browse files
gnito-orgnicolas-grekas
authored andcommitted
[Notifier] Ass SMS options to ContactEveryone notifier
1 parent 45113ba commit 9fd8dfd

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 function toStringProvider(): iterable
3637
public 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 function unsupportedMessagesProvider(): iterable

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy