8000 feature #48572 [Notifier] Add SMS options to AllMySms notifier (gnito… · symfony/symfony@0801f41 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0801f41

Browse files
feature #48572 [Notifier] Add SMS options to AllMySms notifier (gnito-org)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [Notifier] Add SMS options to AllMySms notifier | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | 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 ------- a3d4fb2 [Notifier] Add SMS options to AllMySms notifier
2 parents 5083945 + a3d4fb2 commit 0801f41

File tree

5 files changed

+209
-6
lines changed

5 files changed

+209
-6
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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\AllMySms;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author gnito-org <https://github.com/gnito-org>
18+
*/
19+
final class AllMySmsOptions implements MessageOptionsInterface
20+
{
21+
private array $options;
22+
23+
public function __construct(array $options = [])
24+
{
25+
$this->options = $options;
26+
}
27+
28+
public function getAlerting(): ?int
29+
{
30+
return $this->options['alerting'] ?? null;
31+
}
32+
33+
public function getCampaignName(): ?string
34+
{
35+
return $this->options['campaign_name'] ?? null;
36+
}
37+
38+
public function getCliMsgId(): ?string
39+
{
40+
return $this->options['cli_msg_id'] ?? null;
41+
}
42+
43+
public function getDate(): ?string
44+
{
45+
return $this->options['date'] ?? null;
46+
}
47+
48+
public function getFrom(): ?string
49+
{
50+
return $this->options['from'] ?? null;
51+
}
52+
53+
public function getRecipientId(): ?string
54+
{
55+
return $this->options['recipient_id'] ?? null;
56+
}
57+
58+
public function getSimulate(): ?int
59+
{
60+
return $this->options['simulate'] ?? null;
61+
}
62+
63+
public function getUniqueIdentifier(): ?string
64+
{
65+
return $this->options['unique_identifier'] ?? null;
66+
}
67+
68+
public function getVerbose(): ?int
69+
{
70+
return $this->options['verbose'] ?? null;
71+
}
72+
73+
public function setAlerting(int $alerting): self
74+
{
75+
$this->options['alerting'] = $alerting;
76+
77+
return $this;
78+
}
79+
80+
public function setCampaignName(string $campaignName): self
81+
{
82+
$this->options['campaign_name'] = $campaignName;
83+
84+
return $this;
85+
}
86+
87+
public function setCliMsgId(string $cliMsgId): self
88+
{
89+
$this->options['cli_msg_id'] = $cliMsgId;
90+
91+
return $this;
92+
}
93+
94+
public function setDate(string $date): self
95+
{
96+
$this->options['date'] = $date;
97+
98+
return $this;
99+
}
100+
101+
public function setFrom(string $from): self
102+
{
103+
$this->options['from'] = $from;
104+
105+
return $this;
106+
}
107+
108+
public function setRecipientId(string $id): self
109+
{
110+
$this->options['recipient_id'] = $id;
111+
112+
return $this;
113+
}
114+
115+
public function setSimulate(int $simulate): self
116+
{
117+
$this->options['simulate'] = $simulate;
118+
119+
return $this;
120+
}
121+
122+
public function setUniqueIdentifier(string $uniqueIdentifier): self
123+
{
124+
$this->options['unique_identifier'] = $uniqueIdentifier;
125+
126+
return $this;
127+
}
128+
129+
public function setVerbose(int $verbose): self
130+
{
131+
$this->options['verbose'] = $verbose;
132+
133+
return $this;
134+
}
135+
136+
public function toArray(): array
137+
{
138+
$options = $this->options;
139+
if (isset($options['recipient_id'])) {
140+
unset($options['recipient_id']);
141+
}
142+
143+
return $options;
144+
}
145+
}

src/Symfony/Component/Notifier/Bridge/AllMySms/AllMySmsTransport.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __toString(): string
5252

5353
public function supports(MessageInterface $message): bool
5454
{
55-
return $message instanceof SmsMessage;
55+
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof AllMySmsOptions);
5656
}
5757

5858
protected function doSend(MessageInterface $message): SentMessage
@@ -63,14 +63,31 @@ protected function doSend(MessageInterface $message): SentMessage
6363

6464
$from = $message->getFrom() ?: $this->from;
6565

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();
71+
72+
if (isset($options['campaign_name'])) {
73+
$options['campaignName'] = $options['campaign_name'];
74+
unset($options['campaign_name']);
75+
}
76+
77+
if (isset($options['cli_msg_id'])) {
78+
$options['cliMsgId'] = $options['cli_msg_id'];
79+
unset($options['cli_msg_id']);
80+
}
81+
82+
if (isset($options['unique_identifier'])) {
83+
$options['uniqueIdentifier'] = $options['unique_identifier'];
84+
unset($options['unique_identifier']);
85+
}
86+
6687
$endpoint = sprintf('https://%s/sms/send/', $this->getEndpoint());
6788
$response = $this->client->request('POST', $endpoint, [
6889
'auth_basic' => $this->login.':'.$this->apiKey,
69-
'json' => [
70-
'from' => $from,
71-
'to' => $message->getPhone(),
72-
'text' => $message->getSubject(),
73-
],
90+
'json' => array_filter($options),
7491
]);
7592

7693
try {

src/Symfony/Component/Notifier/Bridge/AllMySms/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+
* Use `AllMySmsOptions` class
8+
49
6.2
510
---
611

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\AllMySms\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsOptions;
16+
17+
class AllMySmsOptionsTest extends TestCase
18+
{
19+
public function testAllMySmsOptions()
20+
{
21+
$allMySmsOptions = (new AllMySmsOptions())->setFrom('test_from')->setAlerting(1)->setDate('test_date')->setCampaignName('test_campaign_name')->setRecipientId('test_recipient')->setCliMsgId('test_cli_msg_id')->setSimulate(1)->setUniqueIdentifier('test_unique_identifier')->setVerbose(1);
22+
23+
self::assertSame([
24+
'from' => 'test_from',
25+
'alerting' => 1,
26+
'date' => 'test_date',
27+
'campaign_name' => 'test_campaign_name',
28+
'cli_msg_id' => 'test_cli_msg_id',
29+
'simulate' => 1,
30+
'unique_identifier' => 'test_unique_identifier',
31+
'verbose' => 1,
32+
], $allMySmsOptions->toArray());
33+
}
34+
}

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

1414
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsOptions;
1516
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransport;
1617
use Symfony\Component\Notifier\Message\ChatMessage;
1718
use Symfony\Component\Notifier\Message\SmsMessage;
@@ -35,6 +36,7 @@ public static function toStringProvider(): iterable
3536
public static function supportedMessagesProvider(): iterable
3637
{
3738
yield [new SmsMessage('0611223344', 'Hello!')];
39+
yield [new SmsMessage('0611223344', 'Hello!', 'from', new AllMySmsOptions(['from' => 'foo']))];
3840
}
3941

4042
public static function unsupportedMessagesProvider(): iterable

0 commit comments

Comments
 (0)
0